Search in sources :

Example 1 with SelectorBuilder

use of st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.SelectorBuilder in project TheLighterBot by PhotonBursted.

the class AccesslistModificationCommand method execute.

@Override
protected void execute() {
    // Check if the input actually had enough arguments
    if (input.size() < 1) {
        handleError(String.format("You didn't supply the ID of the entity to %s!\nPlease use `+%s <idTo%s>`.", getActionDescription("%s", "remove from the %s"), primaryCommandName, StringUtils.capitalize(targetAccesslist)));
        return;
    }
    // Get the input after the arguments as one string representation
    String target = Utils.drainQueueToString(input);
    String[] targetParts = target.split(":", 2);
    String targetType = targetParts[0];
    String targetName = targetParts.length > 1 ? targetParts[1] : null;
    // Identify what the input was targeting
    switch(targetType) {
        case "user":
            {
                // Retrieve a list of users which could be targeted by the search
                List<Member> candidates = ev.getGuild().getMembersByEffectiveName(targetName, true);
                // See if there were any search results
                if (candidates.size() <= 0) {
                    handleError(String.format("No user was found in this server having name **%s**!", targetName));
                    return;
                }
                // Otherwise, generate a selector and let the user decide what the target was
                if (candidates.size() == 1) {
                    performAccesslistModification(new BannableUser(candidates.get(0).getUser()));
                } else {
                    LinkedHashMap<String, BannableEntity> candidateMap = new LinkedHashMap<>();
                    candidates.forEach(c -> candidateMap.put(Utils.userAsString(c.getUser()), new BannableUser(c.getUser())));
                    new SelectorBuilder<>(this).setOptionMap(candidateMap).build();
                }
                break;
            }
        case "role":
            {
                // Retrieve a list of roles which could be targeted by the search
                List<Role> candidates = ev.getGuild().getRolesByName(targetName, true);
                // See if there were any search results
                if (candidates.size() <= 0) {
                    handleError(String.format("No role was found in this server having name **%s**!", targetName));
                    return;
                }
                // Otherwise, generate a selector and let the user decide what the target was
                if (candidates.size() == 1) {
                    performAccesslistModification(new BannableRole(candidates.get(0)));
                } else {
                    LinkedHashMap<String, BannableEntity> candidateMap = new LinkedHashMap<>();
                    candidates.forEach(c -> candidateMap.put(c.getName(), new BannableRole(c)));
                    new SelectorBuilder<>(this).setOptionMap(candidateMap).build();
                }
                break;
            }
        default:
            BannableEntity targetEntity = null;
            // Test if the id was targeting a role or user. If not, throw an error, otherwise whitelist the target
            if (ev.getGuild().getRoles().stream().anyMatch(role -> role.getId().equals(target))) {
                targetEntity = new BannableRole(target);
            }
            if (ev.getGuild().getMembers().stream().anyMatch(member -> member.getUser().getId().equals(target))) {
                targetEntity = new BannableUser(target);
            }
            if (targetEntity == null) {
                handleError(String.format("The ID you supplied (`%s`) was neither a role or user in this server!", target));
                return;
            }
            // Detect if the id specified is already blacklisted
            if (performActionCheck(ev.getGuild(), targetEntity)) {
                handleError(String.format("The entity you tried to %s is already %sed for this server!", getActionDescription("%s", "remove from the %s"), targetAccesslist));
                return;
            }
            performAccesslistModification(targetEntity);
            break;
    }
}
Also used : Role(net.dv8tion.jda.core.entities.Role) Logger(org.slf4j.Logger) DiscordController(st.photonbur.Discord.Bot.lightbotv3.controller.DiscordController) BannableRole(st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableRole) StringUtils(st.photonbur.Discord.Bot.lightbotv3.misc.StringUtils) Member(net.dv8tion.jda.core.entities.Member) Utils(st.photonbur.Discord.Bot.lightbotv3.misc.Utils) Command(st.photonbur.Discord.Bot.lightbotv3.command.Command) SelectionEvent(st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.SelectionEvent) LinkedHashMap(java.util.LinkedHashMap) Guild(net.dv8tion.jda.core.entities.Guild) BannableEntity(st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableEntity) List(java.util.List) BannableUser(st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableUser) Permission(net.dv8tion.jda.core.Permission) User(net.dv8tion.jda.core.entities.User) LoggerUtils(st.photonbur.Discord.Bot.lightbotv3.main.LoggerUtils) SelectorBuilder(st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.SelectorBuilder) Selector(st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.Selector) CommandAliasCollectionBuilder(st.photonbur.Discord.Bot.lightbotv3.command.alias.CommandAliasCollectionBuilder) BannableUser(st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableUser) BannableEntity(st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableEntity) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) BannableRole(st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableRole)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Permission (net.dv8tion.jda.core.Permission)1 Guild (net.dv8tion.jda.core.entities.Guild)1 Member (net.dv8tion.jda.core.entities.Member)1 Role (net.dv8tion.jda.core.entities.Role)1 User (net.dv8tion.jda.core.entities.User)1 Logger (org.slf4j.Logger)1 Command (st.photonbur.Discord.Bot.lightbotv3.command.Command)1 CommandAliasCollectionBuilder (st.photonbur.Discord.Bot.lightbotv3.command.alias.CommandAliasCollectionBuilder)1 DiscordController (st.photonbur.Discord.Bot.lightbotv3.controller.DiscordController)1 BannableEntity (st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableEntity)1 BannableRole (st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableRole)1 BannableUser (st.photonbur.Discord.Bot.lightbotv3.entity.bannable.BannableUser)1 LoggerUtils (st.photonbur.Discord.Bot.lightbotv3.main.LoggerUtils)1 StringUtils (st.photonbur.Discord.Bot.lightbotv3.misc.StringUtils)1 Utils (st.photonbur.Discord.Bot.lightbotv3.misc.Utils)1 SelectionEvent (st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.SelectionEvent)1 Selector (st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.Selector)1 SelectorBuilder (st.photonbur.Discord.Bot.lightbotv3.misc.menu.selector.SelectorBuilder)1