Search in sources :

Example 6 with ScheduleEntry

use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.

the class ListCommand method verify.

@Override
public String verify(String prefix, String[] args, MessageReceivedEvent event) {
    String head = prefix + this.name();
    if (args.length == 0) {
        return "That's not enough arguments! Use ``" + head + " <ID> [filters]``";
    }
    int index = 0;
    ScheduleEntry entry;
    if (VerifyUtilities.verifyEntryID(args[index])) {
        Integer entryId = ParsingUtilities.encodeIDToInt(args[index]);
        entry = Main.getEntryManager().getEntryFromGuild(entryId, event.getGuild().getId());
        if (entry == null) {
            return "The requested entry does not exist!";
        }
        if (!Main.getScheduleManager().isRSVPEnabled(entry.getChannelId())) {
            return "The schedule that the entry is on is not rsvp enabled!";
        }
    } else {
        return "Argument *" + args[index] + "* is not a valid entry ID!";
    }
    index++;
    boolean mobileFlag = false;
    boolean IdFlag = false;
    for (; index < args.length; index++) {
        if (args[index].equalsIgnoreCase("mobile") || args[index].equalsIgnoreCase("m")) {
            mobileFlag = true;
            if (IdFlag) {
                return "Both 'mobile' and 'id' modes cannot be active at the same time!\n" + "Include either the 'id' or 'mobile' argument in the command, not both.";
            }
            continue;
        }
        if (args[index].equalsIgnoreCase("id") || args[index].equalsIgnoreCase("i")) {
            IdFlag = true;
            if (mobileFlag) {
                return "Both 'mobile' and 'id' modes cannot be active at the same time!\n" + "Include either the 'id' or 'mobile' argument in the command, not both.";
            }
            continue;
        }
        String[] filter = args[index].split(":");
        if (filter.length != 2) {
            return "Invalid filter ``" + args[index] + "``!\nFilters must be of the form \"r: @role\" or \"u: @user\"";
        }
        String filterType = filter[0].toLowerCase().trim();
        String filterValue = filter[1].trim();
        switch(filterType.toLowerCase()) {
            case "r":
            case "role":
                break;
            case "u":
            case "user":
                break;
            case "t":
            case "type":
                if (filterValue.equalsIgnoreCase("no-input"))
                    break;
                Map<String, String> options = Main.getScheduleManager().getRSVPOptions(entry.getChannelId());
                if (!options.values().contains(filterValue)) {
                    return "Invalid ``[type]`` for type filter!" + "``[type]`` must be either an rsvp group used on that event or \"no-input\".";
                }
                break;
            default:
                return "Invalid filter ``" + args[index] + "``!" + "\nFilters must be of the form \"r: @role\", \"u: @user\", or \"t: [type]\"";
        }
    }
    return "";
}
Also used : ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry)

Example 7 with ScheduleEntry

use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.

the class ListCommand method action.

@Override
public void action(String prefix, String[] args, MessageReceivedEvent event) {
    int index = 0;
    Integer entryId = ParsingUtilities.encodeIDToInt(args[index++]);
    ScheduleEntry se = Main.getEntryManager().getEntryFromGuild(entryId, event.getGuild().getId());
    String titleUrl = se.getTitleUrl() == null ? "https://nnmathe.ws/saber" : se.getTitleUrl();
    String title = se.getTitle() + " [" + ParsingUtilities.intToEncodedID(entryId) + "]";
    String content = "";
    List<String> userFilters = new ArrayList<>();
    List<String> roleFilters = new ArrayList<>();
    boolean filterByType = false;
    Set<String> typeFilters = new HashSet<>();
    boolean mobileFlag = false;
    boolean IdFlag = false;
    for (; index < args.length; index++) {
        if (args[index].equalsIgnoreCase("mobile") || args[index].equalsIgnoreCase("m")) {
            mobileFlag = true;
            continue;
        }
        if (args[index].equalsIgnoreCase("id") || args[index].equalsIgnoreCase("i")) {
            IdFlag = true;
            continue;
        }
        String filterType = args[index].split(":")[0].toLowerCase().trim();
        String filterValue = args[index].split(":")[1].trim();
        switch(filterType.toLowerCase()) {
            case "r":
            case "role":
                roleFilters.add(filterValue.replace("<@&", "").replace(">", ""));
                break;
            case "u":
            case "user":
                userFilters.add(filterValue.replace("<@", "").replace(">", ""));
                break;
            case "t":
            case "type":
                filterByType = true;
                typeFilters.add(filterValue);
                break;
        }
    }
    // maximum number of characters before creating a new message
    int lengthCap = 1900;
    // maximum number of lines until new message, in mobile mode
    int mobileLineCap = 25;
    Set<String> uniqueMembers = new HashSet<>();
    Map<String, String> options = Main.getScheduleManager().getRSVPOptions(se.getChannelId());
    for (String type : options.values()) {
        if (!filterByType || typeFilters.contains(type)) {
            content += "**\"" + type + "\"\n======================**\n";
            List<String> members = se.getRsvpMembersOfType(type);
            for (String id : members) {
                // if the message is nearing maximum length, or if in mobile mode and the max lines have been reached
                if (content.length() > lengthCap || (mobileFlag && StringUtils.countMatches(content, "\n") > mobileLineCap)) {
                    // build and send the embedded message object
                    Message message = (new MessageBuilder()).setEmbed((new EmbedBuilder()).setDescription(content).setTitle(title, titleUrl).build()).build();
                    MessageUtilities.sendMsg(message, event.getChannel(), null);
                    // clear the content sting
                    content = "*continued. . .* \n";
                }
                if (id.matches("\\d+")) {
                    // cases in which the id is most likely a valid discord user's ID
                    Member member = event.getGuild().getMemberById(id);
                    if (// if the user is still a member of the guild, add to the list
                    member != null) {
                        uniqueMembers.add(member.getUser().getId());
                        content += this.getNameDisplay(mobileFlag, IdFlag, member);
                    } else // otherwise, remove the member from the event and update
                    {
                        se.getRsvpMembersOfType(type).remove(id);
                        Main.getEntryManager().updateEntry(se, false);
                    }
                } else {
                    // handles cases in which a non-discord user was added by an admin
                    uniqueMembers.add(id);
                    content += "*" + id + "*\n";
                }
            }
        }
        content += "\n";
    }
    if (!filterByType || typeFilters.contains("no-input")) {
        // generate a list of all members of the guild who pass the filter and map to their ID
        List<String> noInput = event.getGuild().getMembers().stream().filter(member -> checkMember(member, userFilters, roleFilters)).map(member -> member.getUser().getId()).collect(Collectors.toList());
        for (String type : options.values()) {
            noInput.removeAll(se.getRsvpMembersOfType(type));
        }
        content += "**No input\n======================\n**";
        if (!filterByType & noInput.size() > 10) {
            content += " Too many users to show: " + noInput.size() + " users with no rsvp\n";
        } else
            for (String id : noInput) {
                if (content.length() > lengthCap || (mobileFlag && StringUtils.countMatches(content, "\n") > mobileLineCap)) {
                    // build and send the embedded message object
                    Message message = (new MessageBuilder()).setEmbed((new EmbedBuilder()).setDescription(content).setTitle(title, titleUrl).build()).build();
                    MessageUtilities.sendMsg(message, event.getChannel(), null);
                    // clear the content sting
                    content = "*continued. . .* \n";
                }
                Member member = event.getGuild().getMemberById(id);
                content += this.getNameDisplay(mobileFlag, IdFlag, member);
            }
    }
    String footer = uniqueMembers.size() + " unique member(s) appear in this search";
    // build and send the embedded message object
    Message message = (new MessageBuilder()).setEmbed((new EmbedBuilder()).setDescription(content).setTitle(title, titleUrl).setFooter(footer, null).build()).build();
    MessageUtilities.sendMsg(message, event.getChannel(), null);
}
Also used : Command(ws.nmathe.saber.commands.Command) java.util(java.util) CommandInfo(ws.nmathe.saber.commands.CommandInfo) Member(net.dv8tion.jda.core.entities.Member) Logging(ws.nmathe.saber.utils.Logging) StringUtils(org.apache.commons.lang3.StringUtils) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) Collectors(java.util.stream.Collectors) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) ISnowflake(net.dv8tion.jda.core.entities.ISnowflake) MessageReceivedEvent(net.dv8tion.jda.core.events.message.MessageReceivedEvent) MessageUtilities(ws.nmathe.saber.utils.MessageUtilities) Main(ws.nmathe.saber.Main) VerifyUtilities(ws.nmathe.saber.utils.VerifyUtilities) ParsingUtilities(ws.nmathe.saber.utils.ParsingUtilities) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) Member(net.dv8tion.jda.core.entities.Member)

Example 8 with ScheduleEntry

use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.

the class EventListener method onMessageReactionAdd.

@Override
@SuppressWarnings("unchecked")
public void onMessageReactionAdd(MessageReactionAddEvent event) {
    // stop processing if the event is not from a guild text channel
    if (!event.isFromType(ChannelType.TEXT))
        return;
    // don't process reactions added on non RSVP channels
    if (!Main.getScheduleManager().isRSVPEnabled(event.getChannel().getId()))
        return;
    // don't process reactions added by the bot
    if (event.getUser().getId().equals(event.getJDA().getSelfUser().getId()))
        return;
    if (reactionLimiter.isOnCooldown(event.getUser().getId()))
        return;
    // add the user to the appropriate rsvp list and remove the emoji
    try {
        Document doc = Main.getDBDriver().getEventCollection().find(eq("messageId", event.getMessageId())).first();
        if (doc != null) {
            ScheduleEntry se = new ScheduleEntry(doc);
            boolean removeReaction = se.handleRSVPReaction(event);
            if (removeReaction) {
                // attempt to remove the reaction
                Consumer<Throwable> errorProcessor = e -> {
                    if (!(e instanceof PermissionException)) {
                        Logging.exception(this.getClass(), e);
                    }
                };
                event.getReaction().removeReaction(event.getUser()).queue(null, errorProcessor);
            }
        }
    } catch (PermissionException ignored) {
    } catch (Exception e) {
        Logging.exception(this.getClass(), e);
    }
}
Also used : MessageReactionAddEvent(net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent) Document(org.bson.Document) ZonedDateTime(java.time.ZonedDateTime) ListenerAdapter(net.dv8tion.jda.core.hooks.ListenerAdapter) ws.nmathe.saber.utils(ws.nmathe.saber.utils) StringUtils(org.apache.commons.lang3.StringUtils) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) GuildLeaveEvent(net.dv8tion.jda.core.events.guild.GuildLeaveEvent) Matcher(java.util.regex.Matcher) Map(java.util.Map) JDA(net.dv8tion.jda.core.JDA) Filters.eq(com.mongodb.client.model.Filters.eq) ReadyEvent(net.dv8tion.jda.core.events.ReadyEvent) MessageDeleteEvent(net.dv8tion.jda.core.events.message.MessageDeleteEvent) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Collection(java.util.Collection) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) GuildJoinEvent(net.dv8tion.jda.core.events.guild.GuildJoinEvent) TextChannelDeleteEvent(net.dv8tion.jda.core.events.channel.text.TextChannelDeleteEvent) Consumer(java.util.function.Consumer) List(java.util.List) GuildSettingsManager(ws.nmathe.saber.core.settings.GuildSettingsManager) MessageReceivedEvent(net.dv8tion.jda.core.events.message.MessageReceivedEvent) Main(ws.nmathe.saber.Main) Pattern(java.util.regex.Pattern) GuildMemberLeaveEvent(net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) Document(org.bson.Document) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException)

Example 9 with ScheduleEntry

use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.

the class TestCommand 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++;
    String format = Main.getScheduleManager().getStartAnnounceFormat(entry.getChannelId());
    if (args.length == 2) {
        switch(args[index].toLowerCase()) {
            case "e":
            case "end":
                format = Main.getScheduleManager().getEndAnnounceFormat(entry.getChannelId());
                break;
            case "r":
            case "remind":
                format = Main.getScheduleManager().getReminderFormat(entry.getChannelId());
                break;
        }
    }
    String remindMsg = ParsingUtilities.parseMessageFormat(format, entry, true);
    MessageUtilities.sendMsg(remindMsg, event.getChannel(), null);
}
Also used : ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry) Message(net.dv8tion.jda.core.entities.Message)

Example 10 with ScheduleEntry

use of ws.nmathe.saber.core.schedule.ScheduleEntry in project Saber-Bot by notem.

the class TestCommand method verify.

@Override
public String verify(String prefix, String[] args, MessageReceivedEvent event) {
    String head = prefix + this.name();
    int index = 0;
    // length checks
    if (args.length < 1) {
        return "That's not enough arguments! Use ``" + head + " <ID>``";
    }
    if (args.length > 2) {
        return "That's too many arguments! Use ``" + head + " <ID> [<type>]``";
    }
    // check for a valid entry ID
    if (!VerifyUtilities.verifyEntryID(args[index])) {
        return "``" + args[index] + "`` is not a valid entry ID!";
    }
    // check to see if event with the provided ID exists for the guild
    Integer Id = ParsingUtilities.encodeIDToInt(args[index]);
    ScheduleEntry entry = Main.getEntryManager().getEntryFromGuild(Id, event.getGuild().getId());
    if (entry == null) {
        return "I could not find an entry with that ID!";
    }
    // next argument is optional
    index++;
    if (args.length == 2) {
        switch(args[index].toLowerCase()) {
            case "start":
            case "end":
            case "remind":
            case "reminder":
            case "e":
            case "r":
            case "s":
                break;
            default:
                return "*" + args[index] + "* is not an announcement type!\n" + "Please use either ``start``, ``end``, or ``remind`` for this argument!";
        }
    }
    // return valid
    return "";
}
Also used : ScheduleEntry(ws.nmathe.saber.core.schedule.ScheduleEntry)

Aggregations

ScheduleEntry (ws.nmathe.saber.core.schedule.ScheduleEntry)21 JDA (net.dv8tion.jda.core.JDA)6 Main (ws.nmathe.saber.Main)6 ZonedDateTime (java.time.ZonedDateTime)5 java.util (java.util)5 Message (net.dv8tion.jda.core.entities.Message)5 ZoneId (java.time.ZoneId)4 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 Permission (net.dv8tion.jda.core.Permission)4 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)4 StringUtils (org.apache.commons.lang3.StringUtils)4 DateTimeFormatter (java.time.format.DateTimeFormatter)3 ChronoUnit (java.time.temporal.ChronoUnit)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3 MessageReceivedEvent (net.dv8tion.jda.core.events.message.MessageReceivedEvent)3 Document (org.bson.Document)3 MessageUtilities (ws.nmathe.saber.utils.MessageUtilities)3 ParsingUtilities (ws.nmathe.saber.utils.ParsingUtilities)3