Search in sources :

Example 16 with Message

use of net.dv8tion.jda.api.entities.Message in project Saber-Bot by notem.

the class SchedulesCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    Guild guild = event.getGuild();
    List<String> scheduleIds = Main.getScheduleManager().getSchedulesForGuild(guild.getId());
    // build output main body
    StringBuilder content = new StringBuilder();
    for (String sId : scheduleIds) {
        content.append("<#").append(sId).append("> - has ").append(Main.getEntryManager().getEntriesFromChannel(sId).size()).append(" events\n");
    }
    // title for embed
    String title = "Schedules on " + guild.getName();
    // footer for embed
    String footer = scheduleIds.size() + " schedule(s)";
    // build embed
    MessageEmbed embed = new EmbedBuilder().setDescription(content.toString()).setTitle(title).setFooter(footer, null).build();
    // build message
    Message message = new MessageBuilder().setEmbed(embed).build();
    // send message
    MessageUtilities.sendMsg(message, event.getTextChannel(), null);
}
Also used : EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) Message(net.dv8tion.jda.api.entities.Message) MessageBuilder(net.dv8tion.jda.api.MessageBuilder) Guild(net.dv8tion.jda.api.entities.Guild)

Example 17 with Message

use of net.dv8tion.jda.api.entities.Message in project Saber-Bot by notem.

the class EditCommand method action.

@Override
public void action(String head, String[] args, MessageReceivedEvent event) {
    int index = 0;
    Integer entryId = ParsingUtilities.encodeIDToInt(args[index]);
    ScheduleEntry se = Main.getEntryManager().getEntry(entryId);
    Message msg = se.getMessageObject();
    if (msg == null)
        return;
    // otherwise skip this and print out the event configuration
    if (args.length > 1) {
        // 1
        index++;
        boolean limitsChanged = false;
        while (index < args.length) {
            ZoneId zone = Main.getScheduleManager().getTimeZone(se.getChannelId());
            ArrayList<String> originalComments = se.getComments();
            ArrayList<String> comments;
            switch(args[index++].toLowerCase()) {
                case "c":
                case "comment":
                case "comments":
                    comments = se.getComments();
                    switch(args[index++]) {
                        case "a":
                        case "add":
                            comments.add(args[index]);
                            se.setComments(comments);
                            index++;
                            break;
                        case "r":
                        case "remove":
                            if (VerifyUtilities.verifyInteger(args[index])) {
                                String comment = originalComments.get(Integer.parseInt(args[index]) - 1);
                                comments.remove(comment);
                            } else {
                                comments.remove(args[index]);
                            }
                            se.setComments(comments);
                            index++;
                            break;
                        case "s":
                        case "swap":
                            int i1 = Integer.parseInt(args[index]) - 1;
                            int i2 = Integer.parseInt(args[index + 1]) - 1;
                            if (i1 < comments.size() && i2 < comments.size()) {
                                String a = comments.get(i1);
                                String b = comments.get(i2);
                                comments.set(i1, b);
                                comments.set(i2, a);
                                se.setComments(comments);
                            }
                            index += 2;
                            break;
                        case "m":
                        case "modify":
                        case "replace":
                        case "set":
                            int no = Integer.parseInt(args[index]) - 1;
                            comments.set(no, args[index + 1]);
                            se.setComments(comments);
                            index += 2;
                            break;
                    }
                    break;
                case "desc":
                case "description":
                    String description = args[index].toLowerCase().matches("(off)|(default)") ? "%g" : args[index];
                    se.setDescription(description);
                    index++;
                    break;
                case "s":
                case "starts":
                case "start":
                    // create new datetime and update the schedule entry object
                    ZonedDateTime newStart = ZonedDateTime.of(se.getStart().toLocalDate(), ParsingUtilities.parseTime(args[index], se.getStart().getZone()), se.getStart().getZone());
                    se.setStart(newStart);
                    // do some final processing on start/end times
                    if (ZonedDateTime.now().isAfter(se.getStart())) {
                        // add a day if the time has already passed
                        se.setStart(se.getStart().plusDays(1));
                    }
                    if (se.getStart().isAfter(se.getEnd())) {
                        // add a day to end if end is after start
                        se.setEnd(se.getEnd().plusDays(1));
                        // reload end reminders
                        se.reloadEndReminders(Main.getScheduleManager().getEndReminders(se.getChannelId()));
                    }
                    // reload start reminders
                    se.reloadReminders(Main.getScheduleManager().getReminders(se.getChannelId())).regenerateAnnouncementOverrides();
                    index++;
                    break;
                case "e":
                case "ends":
                case "end":
                    // if off, use start
                    if (args[index].equalsIgnoreCase("off")) {
                        se.setEnd(se.getStart());
                    } else {
                        // otherwise parse the input for a time
                        se.setEnd(ZonedDateTime.of(se.getEnd().toLocalDate(), ParsingUtilities.parseTime(args[index], se.getEnd().getZone()), se.getEnd().getZone()));
                    }
                    // add a day if the time has already passed
                    if (ZonedDateTime.now().isAfter(se.getEnd()))
                        se.setEnd(se.getEnd().plusDays(1));
                    // add a day to end if end is after start
                    if (se.getStart().isAfter(se.getEnd()))
                        se.setEnd(se.getEnd().plusDays(1));
                    // reload end reminders
                    se.reloadEndReminders(Main.getScheduleManager().getEndReminders(se.getChannelId())).regenerateAnnouncementOverrides();
                    index++;
                    break;
                case "t":
                case "title":
                    se.setTitle(args[index]);
                    index++;
                    break;
                case "d":
                case "date":
                    LocalDate date = ParsingUtilities.parseDate(args[index].toLowerCase(), zone);
                    se.setStart(ZonedDateTime.of(date, se.getStart().toLocalTime(), zone));
                    se.setEnd(ZonedDateTime.of(date, se.getEnd().toLocalTime(), zone));
                    // update the reminders and announcements to appropriate datetimes
                    se.reloadReminders(Main.getScheduleManager().getReminders(se.getChannelId())).reloadEndReminders(Main.getScheduleManager().getEndReminders(se.getChannelId())).regenerateAnnouncementOverrides();
                    index++;
                    break;
                case "sd":
                case "start date":
                case "start-date":
                    LocalDate sdate = ParsingUtilities.parseDate(args[index].toLowerCase(), zone);
                    se.setStart(ZonedDateTime.of(sdate, se.getStart().toLocalTime(), zone));
                    // change end date to a valid datetime if necessary
                    if (se.getEnd().isBefore(se.getStart())) {
                        se.setEnd(se.getStart());
                        se.reloadEndReminders(Main.getScheduleManager().getEndReminders(se.getChannelId()));
                    }
                    // update the reminders and announcements to appropriate datetimes
                    se.reloadReminders(Main.getScheduleManager().getReminders(se.getChannelId())).regenerateAnnouncementOverrides();
                    index++;
                    break;
                case "ed":
                case "end date":
                case "end-date":
                    LocalDate edate = ParsingUtilities.parseDate(args[index].toLowerCase(), zone);
                    se.setEnd(ZonedDateTime.of(edate, se.getEnd().toLocalTime(), zone));
                    // change start date to a valid datetime if necessary
                    if (se.getEnd().isBefore(se.getStart())) {
                        se.setStart(se.getEnd());
                        se.reloadReminders(Main.getScheduleManager().getReminders(se.getChannelId()));
                    }
                    // update the reminders and announcements to appropriate datetimes
                    se.reloadEndReminders(Main.getScheduleManager().getEndReminders(se.getChannelId())).regenerateAnnouncementOverrides();
                    index++;
                    break;
                case "r":
                case "repeats":
                case "repeat":
                    se.setRepeat(EventRecurrence.parseRepeat(args[index].toLowerCase()));
                    index++;
                    break;
                case "u":
                case "url":
                    se.setTitleUrl(ParsingUtilities.parseUrl(args[index]));
                    index++;
                    break;
                case "im":
                case "image":
                    se.setImageUrl(ParsingUtilities.parseUrl(args[index]));
                    index++;
                    break;
                case "th":
                case "thumbnail":
                    se.setThumbnailUrl(ParsingUtilities.parseUrl(args[index]));
                    index++;
                    break;
                case "ex":
                case "expire":
                    LocalDate expireDate = ParsingUtilities.parseNullableDate(args[index], zone);
                    se.setExpire(expireDate == null ? null : ZonedDateTime.of(expireDate, LocalTime.MAX, zone));
                    index++;
                    break;
                case "deadline":
                case "dl":
                    LocalDate deadlineDate = ParsingUtilities.parseNullableDate(args[index], zone);
                    LocalTime deadlineTime = LocalTime.MAX;
                    // allow users in specify exact time RSVPs close
                    if (args[index].contains("@")) {
                        String[] a = args[index].split("@");
                        deadlineDate = ParsingUtilities.parseNullableDate(a[0], zone);
                        deadlineTime = ParsingUtilities.parseTime(a[1], zone);
                    } else if (args.length - index == 2 && VerifyUtilities.verifyTime(args[index + 1])) {
                        deadlineTime = ParsingUtilities.parseTime(args[index + 1], zone);
                        index++;
                    }
                    if (deadlineDate == null) {
                        // remove deadline
                        se.setRsvpDeadline(null);
                    } else {
                        // update deadline
                        se.setRsvpDeadline(ZonedDateTime.of(deadlineDate, deadlineTime, zone));
                    }
                    index++;
                    break;
                case "count":
                case "co":
                    Integer c = null;
                    if (!args[index].equalsIgnoreCase("off"))
                        c = Integer.parseInt(args[index]);
                    se.setCount(c);
                    // set the original start to the current start
                    se.setOriginalStart(se.getStart());
                    index++;
                    break;
                case "qs":
                case "quiet-start":
                    se.setQuietStart(!se.isQuietStart());
                    break;
                case "qe":
                case "quiet-end":
                    se.setQuietEnd(!se.isQuietEnd());
                    break;
                case "qr":
                case "quiet-remind":
                    se.setQuietRemind(!se.isQuietRemind());
                    break;
                case "qa":
                case "quiet-all":
                    if (se.isQuietRemind() && se.isQuietEnd() && se.isQuietStart())
                        se.setQuietRemind(false).setQuietEnd(false).setQuietStart(false);
                    else
                        se.setQuietRemind(true).setQuietEnd(true).setQuietStart(true);
                    break;
                case "limit":
                case "l":
                    Integer lim = null;
                    if (!args[index + 1].equalsIgnoreCase("off"))
                        lim = Integer.parseInt(args[index + 1]);
                    se.setRsvpLimit(args[index], lim);
                    limitsChanged = true;
                    index += 2;
                    break;
                case "lo":
                case "location":
                    if (args[index].equalsIgnoreCase("off"))
                        se.setLocation(null);
                    else
                        se.setLocation(args[index]);
                    index++;
                    break;
                case "an":
                case "announce":
                case "announcement":
                case "announcements":
                    switch(args[index++].toLowerCase()) {
                        case "a":
                        case "add":
                            String target = args[index].replaceAll("[^\\d]", "");
                            String time = args[index + 1];
                            String message = args[index + 2];
                            se.addAnnouncementOverride(target, time, message);
                            index += 3;
                            break;
                        case "r":
                        case "remove":
                            Integer id = Integer.parseInt(args[index].replaceAll("[^\\d]", "")) - 1;
                            se.removeAnnouncementOverride(id);
                            index++;
                            break;
                    }
                    break;
                case "a":
                case "add":
                    switch(args[index++].toLowerCase()) {
                        case "a":
                        case "an":
                        case "announce":
                        case "announcement":
                        case "announcements":
                            String target = args[index].replaceAll("[^\\d]", "");
                            String time = args[index + 1];
                            String message = args[index + 2];
                            se.addAnnouncementOverride(target, time, message);
                            index += 3;
                            break;
                        case "c":
                        case "comment":
                        case "comments":
                            comments = se.getComments();
                            comments.add(args[index]);
                            se.setComments(comments);
                            index++;
                            break;
                    }
                    break;
                case "re":
                case "remove":
                    switch(args[index++].toLowerCase()) {
                        case "a":
                        case "an":
                        case "announce":
                        case "announcement":
                        case "announcements":
                            Integer id = Integer.parseInt(args[index].replaceAll("[^\\d]", "")) - 1;
                            se.removeAnnouncementOverride(id);
                            index++;
                            break;
                        case "c":
                        case "comment":
                        case "comments":
                            comments = se.getComments();
                            if (VerifyUtilities.verifyInteger(args[index]))
                                comments.remove(Integer.parseInt(args[index]) - 1);
                            else
                                comments.remove(args[index]);
                            se.setComments(comments);
                            index++;
                            break;
                    }
                    break;
                case "color":
                    if (args[index].toLowerCase().matches("off||null||none"))
                        se.setColor(null);
                    else
                        se.setColor(args[index]);
                    index++;
                    break;
                case "n":
                case "nonembed":
                    if (args[index].toLowerCase().matches("off||null|none"))
                        se.setNonEmbededText(null);
                    else
                        se.setNonEmbededText(args[index]);
                    index++;
                    break;
            }
        }
        Main.getEntryManager().updateEntry(se, true);
        if (// if the limits on the event was changed, reload the reactions
        limitsChanged) {
            se.getMessageObject().clearReactions().queue(message -> {
                Map<String, String> options = Main.getScheduleManager().getRSVPOptions(se.getChannelId());
                String clearEmoji = Main.getScheduleManager().getRSVPClear(se.getChannelId());
                EntryManager.addRSVPReactions(options, clearEmoji, se.getMessageObject(), se);
            }, failure -> Logging.exception(this.getClass(), failure));
        }
    }
    // 
    // send the event summary to the command channel
    // 
    String body = "Updated event :id: **" + ParsingUtilities.intToEncodedID(se.getId()) + "** on <#" + se.getChannelId() + ">\n" + se.toString();
    MessageUtilities.sendMsg(body, event.getChannel(), null);
}
Also used : Message(net.dv8tion.jda.api.entities.Message) ZoneId(java.time.ZoneId) LocalTime(java.time.LocalTime) LocalDate(java.time.LocalDate) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) ZonedDateTime(java.time.ZonedDateTime)

Example 18 with Message

use of net.dv8tion.jda.api.entities.Message in project Saber-Bot by notem.

the class AnnouncementsCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    int index = 0;
    // get entry object
    Integer entryId = ParsingUtilities.encodeIDToInt(args[index]);
    ScheduleEntry entry = Main.getEntryManager().getEntry(entryId);
    // verify the entry's message exists
    Message msg = entry.getMessageObject();
    if (msg == null)
        return;
    index++;
    if (args.length > 2) {
        // if additional args have been provided. . .
        switch(args[index++].toLowerCase()) {
            case "a":
            case "add":
                String target = args[index].replaceAll("[^\\d]", "");
                String time = args[index + 1];
                String message = args[index + 2];
                entry.addAnnouncementOverride(target, time, message);
                break;
            case "r":
            case "remove":
                Integer id = Integer.parseInt(args[index].replaceAll("[^\\d]", "")) - 1;
                entry.removeAnnouncementOverride(id);
                break;
            case "qs":
            case "quiet-start":
                entry.setQuietStart(!entry.isQuietStart());
                break;
            case "qe":
            case "quiet-end":
                entry.setQuietEnd(!entry.isQuietEnd());
                break;
            case "qr":
            case "quiet-remind":
                entry.setQuietRemind(!entry.isQuietRemind());
                break;
            case "qa":
            case "quiet-all":
                if (entry.isQuietRemind() && entry.isQuietEnd() && entry.isQuietStart()) {
                    entry.setQuietRemind(false).setQuietEnd(false).setQuietStart(false);
                } else {
                    entry.setQuietRemind(true).setQuietEnd(true).setQuietStart(true);
                }
                break;
        }
        Main.getEntryManager().updateEntry(entry, false);
    }
    /*
         * generate output message
         */
    String content = "```js\n// Schedule Announcements\n";
    if (!entry.isQuietStart()) {
        String format = Main.getScheduleManager().getStartAnnounceFormat(entry.getChannelId());
        if (!format.isEmpty()) {
            String target = Main.getScheduleManager().getStartAnnounceChan(entry.getChannelId());
            if (target.matches("\\d+")) {
                JDA jda = Main.getShardManager().getJDA(entry.getGuildId());
                try {
                    target = jda.getTextChannelById(target).getName();
                } catch (Exception ignored) {
                }
            }
            content += "[s] \"" + format + "\" at \"START\" on \"#" + target + "\"\n";
        }
    }
    if (!entry.isQuietEnd()) {
        String format = Main.getScheduleManager().getEndAnnounceFormat(entry.getChannelId());
        if (!format.isEmpty()) {
            String target = Main.getScheduleManager().getEndAnnounceChan(entry.getChannelId());
            if (target.matches("\\d+")) {
                JDA jda = Main.getShardManager().getJDA(entry.getGuildId());
                try {
                    target = jda.getTextChannelById(target).getName();
                } catch (Exception ignored) {
                }
            }
            content += "[e] \"" + format + "\" at \"END\" on \"#" + target + "\"\n";
        }
    }
    if (!entry.isQuietRemind()) {
        String format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
        String target = Main.getScheduleManager().getReminderChan(entry.getChannelId());
        if (target.matches("\\d+")) {
            JDA jda = Main.getShardManager().getJDA(entry.getGuildId());
            try {
                target = jda.getTextChannelById(target).getName();
            } catch (Exception ignored) {
            }
        }
        for (Integer reminder : Main.getScheduleManager().getReminders(entry.getChannelId())) {
            content += "[r] \"" + format + "\" at \"START" + (reminder > 0 ? "-" + reminder : "+" + Math.abs(reminder)) + "m\" on \"#" + target + "\"\n";
        }
        format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
        for (Integer reminder : Main.getScheduleManager().getEndReminders(entry.getChannelId())) {
            content += "[r] \"" + format + "\" at \"END" + (reminder > 0 ? "-" + reminder : "+" + Math.abs(reminder)) + "m\" on \"#" + target + "\"\n";
        }
    }
    if (!entry.getAnnouncementTimes().values().isEmpty()) {
        content += entry.announcementsToString();
    }
    content += "```";
    MessageUtilities.sendMsg(content, event.getChannel(), null);
}
Also used : ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) Message(net.dv8tion.jda.api.entities.Message) JDA(net.dv8tion.jda.api.JDA)

Aggregations

Message (net.dv8tion.jda.api.entities.Message)18 Collectors (java.util.stream.Collectors)7 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)7 java.util (java.util)5 ScheduleEntry (ws.nmathe.saber.core.schedule.ScheduleEntry)5 JDA (net.dv8tion.jda.api.JDA)4 Permission (net.dv8tion.jda.api.Permission)4 Role (net.dv8tion.jda.api.entities.Role)4 TextChannel (net.dv8tion.jda.api.entities.TextChannel)4 java.time (java.time)3 TimeUnit (java.util.concurrent.TimeUnit)3 Consumer (java.util.function.Consumer)3 MessageBuilder (net.dv8tion.jda.api.MessageBuilder)3 Guild (net.dv8tion.jda.api.entities.Guild)3 Member (net.dv8tion.jda.api.entities.Member)3 MantaroData (net.kodehawa.mantarobot.data.MantaroData)3 EmoteReference (net.kodehawa.mantarobot.utils.commands.EmoteReference)3 Document (org.bson.Document)3 Main (ws.nmathe.saber.Main)3 Logging (ws.nmathe.saber.utils.Logging)3