Search in sources :

Example 1 with Permissions

use of sx.blah.discord.handle.obj.Permissions in project DiscordSailv2 by Vaerys-Dawn.

the class SubCommandObject method getHelpDesc.

public String getHelpDesc(CommandObject command) {
    StringHandler builder = new StringHandler();
    builder.append(description + "\n");
    builder.append("**Type: **" + type.toString() + ".");
    // display permissions
    if (permissions != null && permissions.length != 0) {
        builder.append("\n**Perms: **");
        ArrayList<String> permList = new ArrayList<>(permissions.length);
        for (Permissions p : permissions) {
            permList.add(Utility.enumToString(p));
        }
        builder.append(Utility.listFormatter(permList, true));
    }
    if (names.length > 1) {
        List<String> aliases = Arrays.asList(names).stream().map(s -> command.guild.config.getPrefixCommand() + s).collect(Collectors.toList());
        aliases.remove(0);
        builder.append("\n**Aliases:** " + Utility.listFormatter(aliases, true));
    }
    return builder.toString();
}
Also used : Arrays(java.util.Arrays) List(java.util.List) CommandObject(com.github.vaerys.commands.CommandObject) StringHandler(com.github.vaerys.handlers.StringHandler) SAILType(com.github.vaerys.enums.SAILType) Permissions(sx.blah.discord.handle.obj.Permissions) Pattern(java.util.regex.Pattern) GuildHandler(com.github.vaerys.handlers.GuildHandler) Utility(com.github.vaerys.main.Utility) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Permissions(sx.blah.discord.handle.obj.Permissions) StringHandler(com.github.vaerys.handlers.StringHandler)

Example 2 with Permissions

use of sx.blah.discord.handle.obj.Permissions in project DiscordSailv2 by Vaerys-Dawn.

the class Command method getCommandInfo.

/**
 * Creates a message used to fetch the command's documentations
 *
 * @param command
 * @return
 */
public XEmbedBuilder getCommandInfo(CommandObject command) {
    XEmbedBuilder infoEmbed = new XEmbedBuilder(command);
    // command info
    StringBuilder builder = new StringBuilder();
    builder.append(description(command) + "\n");
    builder.append("**Type: **" + type.toString() + ".");
    // display permissions
    if (perms != null && perms.length != 0) {
        builder.append("\n**Perms: **");
        ArrayList<String> permList = new ArrayList<>(perms.length);
        for (Permissions p : perms) {
            permList.add(Utility.enumToString(p));
        }
        builder.append(Utility.listFormatter(permList, true));
    }
    if (names.length > 1) {
        List<String> aliases = Arrays.asList(names).stream().map(s -> command.guild.config.getPrefixCommand() + s).collect(Collectors.toList());
        aliases.remove(0);
        builder.append("\n**Aliases:** " + Utility.listFormatter(aliases, true));
    }
    List<SubCommandObject> objectList = subCommands.stream().filter(subCommandObject -> GuildHandler.testForPerms(command, subCommandObject.getPermissions())).collect(Collectors.toList());
    if (objectList.size() != 0)
        builder.append("\n" + Command.spacer);
    infoEmbed.withTitle("> Help - " + names()[0]);
    infoEmbed.appendField("**" + getUsage(command) + "**    " + Command.spacer, builder.toString(), true);
    for (SubCommandObject s : objectList) {
        infoEmbed.appendField(s.getCommandUsage(command) + "    " + Command.spacer, s.getHelpDesc(command), true);
    }
    // Handle channels
    List<IChannel> channels = command.guild.getChannelsByType(channel);
    List<String> channelMentions = Utility.getChannelMentions(channels);
    // channel
    if (channelMentions.size() > 0) {
        if (channelMentions.size() == 1) {
            infoEmbed.appendField("Channel ", Utility.listFormatter(channelMentions, true), false);
        } else {
            infoEmbed.appendField("Channels ", Utility.listFormatter(channelMentions, true), false);
        }
    }
    return infoEmbed;
}
Also used : XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) Arrays(java.util.Arrays) CommandObject(com.github.vaerys.commands.CommandObject) Globals(com.github.vaerys.main.Globals) Logger(org.slf4j.Logger) ChannelSetting(com.github.vaerys.enums.ChannelSetting) LoggerFactory(org.slf4j.LoggerFactory) SAILType(com.github.vaerys.enums.SAILType) GuildHandler(com.github.vaerys.handlers.GuildHandler) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SubCommandObject(com.github.vaerys.objects.SubCommandObject) List(java.util.List) IChannel(sx.blah.discord.handle.obj.IChannel) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject) Permissions(sx.blah.discord.handle.obj.Permissions) Pattern(java.util.regex.Pattern) LinkedList(java.util.LinkedList) Utility(com.github.vaerys.main.Utility) XEmbedBuilder(com.github.vaerys.objects.XEmbedBuilder) IChannel(sx.blah.discord.handle.obj.IChannel) ArrayList(java.util.ArrayList) Permissions(sx.blah.discord.handle.obj.Permissions) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Example 3 with Permissions

use of sx.blah.discord.handle.obj.Permissions in project KaellyBot by Kaysoro.

the class MissingPermissionDiscordException method throwException.

public void throwException(IChannel channel, Language lg, MissingPermissionsException e) {
    StringBuilder st = new StringBuilder(Translator.getLabel(lg, "exception.missing_permission").replace("{channel.name}", channel.getName()));
    for (Permissions p : e.getMissingPermissions()) st.append(Translator.getLabel(lg, PERMISSION_PREFIX + p.name().toLowerCase())).append(", ");
    st.delete(st.length() - 2, st.length()).append(".");
    if (channel.getModifiedPermissions(ClientConfig.DISCORD().getOurUser()).contains(Permissions.SEND_MESSAGES))
        Message.sendText(channel, st.toString());
    else
        try {
            Message.sendText(channel.getGuild().getOwner().getOrCreatePMChannel(), st.toString());
        } catch (sx.blah.discord.util.DiscordException de) {
            LOG.warn("throwException", "Impossible de contacter l'administrateur de la guilde [" + channel.getGuild().getName() + "].");
        }
}
Also used : Permissions(sx.blah.discord.handle.obj.Permissions)

Example 4 with Permissions

use of sx.blah.discord.handle.obj.Permissions in project DiscordSailv2 by Vaerys-Dawn.

the class CosmeticRoles method execute.

@Override
public String execute(String args, CommandObject command) {
    if (args == null || args.isEmpty()) {
        return get(ListRoles.class).execute(args, command);
    }
    if (EDIT_ROLES.isSubCommand(command)) {
        boolean isAdding = args.split(" ")[0].equals("+");
        // test the permissions of the user to make sure they can modify the role list.
        IRole role = null;
        String subArgs = EDIT_ROLES.getArgs(command);
        try {
            role = command.guild.getRoleByID(Utility.stringLong(subArgs));
        } catch (NumberFormatException e) {
        // move on.
        }
        if (role == null)
            role = GuildHandler.getRoleFromName(subArgs, command.guild.get());
        if (role == null)
            return "> **" + subArgs + "** is not a valid Role Name.";
        // tests to see if the bot is allowed to mess with a role.
        if (!Utility.testUserHierarchy(command.client.bot.get(), role, command.guild.get())) {
            return "> I do not have permission to modify the **" + role.getName() + "** role.";
        }
        // test the user's hierarchy to make sure that the are allowed to mess with that role.
        if (Utility.testUserHierarchy(command.user.get(), role, command.guild.get())) {
            // do if modifier is true
            if (isAdding) {
                // check for the role and add if its not a cosmetic role.
                if (command.guild.config.isRoleCosmetic(role.getLongID())) {
                    return "> The **" + role.getName() + "** role is already listed as a cosmetic role.";
                } else {
                    command.guild.config.getCosmeticRoleIDs().add(role.getLongID());
                    return "> The **" + role.getName() + "** role was added to the cosmetic role list.";
                }
            // do if modifier is false
            } else {
                // check for the role and remove if it is a cosmetic role.
                if (command.guild.config.isRoleCosmetic(role.getLongID())) {
                    Iterator iterator = command.guild.config.getCosmeticRoleIDs().listIterator();
                    while (iterator.hasNext()) {
                        long id = (long) iterator.next();
                        if (role.getLongID() == id) {
                            iterator.remove();
                        }
                    }
                    return "> The **" + role.getName() + "** role was removed from the cosmetic role list.";
                } else {
                    return "> The **" + role.getName() + "** role is not listed as a cosmetic role.";
                }
            }
        } else {
            return "> You do not have permission to modify the **" + role.getName() + "** role.";
        }
    // do user role modification
    } else {
        // check to make sure that the user isn't including the args brackets or the /remove at the end;
        if (command.guild.config.getCosmeticRoleIDs().size() == 0)
            return "> No Cosmetic roles are set up right now. Come back later.";
        if (args.matches("[(|\\[].*[)|\\]]")) {
            return Constants.ERROR_BRACKETS + "\n" + Utility.getCommandInfo(this, command);
        }
        if (args.matches(".*/remove")) {
            return "> Did you mean `" + command.guild.config.getPrefixCommand() + names()[0] + " " + args.replaceAll("(?i)/remove", "") + "`?";
        }
        List<IRole> userRoles = command.user.roles;
        String response;
        // check if role is valid
        IRole role;
        role = GuildHandler.getRoleFromName(args, command.guild.get());
        if (role == null && args.length() > 3) {
            role = GuildHandler.getRoleFromName(args, command.guild.get(), true);
        }
        if (role == null && !args.equalsIgnoreCase("remove")) {
            RequestHandler.sendEmbedMessage("> **" + args + "** is not a valid Role Name.", ListRoles.getList(command), command.channel.get());
            return null;
        // if args = remove. remove the user's cosmetic role
        } else if (args.equalsIgnoreCase("remove")) {
            userRoles = userRoles.stream().filter(r -> !command.guild.config.isRoleCosmetic(r.getLongID())).collect(Collectors.toList());
            if (command.user.getCosmeticRoles(command).size() == 0)
                return "> You don't have a role to remove...";
            else
                response = "> You have had your cosmetic role removed.";
        } else {
            // check if role is cosmetic
            if (command.guild.config.isRoleCosmetic(role.getLongID())) {
                // check to see if roles are toggles
                if (command.guild.config.roleIsToggle) {
                    // if user has role, remove it.
                    if (userRoles.contains(role)) {
                        userRoles.remove(role);
                        response = "> You have had the **" + role.getName() + "** role removed.";
                    // else add that role.
                    } else {
                        userRoles.add(role);
                        response = "> You have been granted the **" + role.getName() + "** role.";
                    }
                // if roles arent toggles run this.
                } else {
                    // if they already have that role
                    if (userRoles.contains(role)) {
                        return "> You already have the **" + role.getName() + "** role.";
                    } else {
                        // remove all cosmetic role and add the new one
                        userRoles = userRoles.stream().filter(r -> !command.guild.config.isRoleCosmetic(r.getLongID())).collect(Collectors.toList());
                        userRoles.add(role);
                        response = "> You have selected the cosmetic role: **" + role.getName() + "**.";
                    }
                }
            } else {
                RequestHandler.sendEmbedMessage("> **" + args + "** is not a valid cosmetic role.", ListRoles.getList(command), command.channel.get());
                return null;
            }
        }
        // push the changes to the user's roles.
        if (RequestHandler.roleManagement(command.user.get(), command.guild.get(), userRoles).get()) {
            return response;
        } else {
            return Constants.ERROR_UPDATING_ROLE;
        }
    }
}
Also used : CommandObject(com.github.vaerys.commands.CommandObject) RequestHandler(com.github.vaerys.handlers.RequestHandler) Iterator(java.util.Iterator) IRole(sx.blah.discord.handle.obj.IRole) ChannelSetting(com.github.vaerys.enums.ChannelSetting) SAILType(com.github.vaerys.enums.SAILType) GuildHandler(com.github.vaerys.handlers.GuildHandler) Constants(com.github.vaerys.main.Constants) Collectors(java.util.stream.Collectors) SubCommandObject(com.github.vaerys.objects.SubCommandObject) List(java.util.List) Permissions(sx.blah.discord.handle.obj.Permissions) Command(com.github.vaerys.templates.Command) Utility(com.github.vaerys.main.Utility) IRole(sx.blah.discord.handle.obj.IRole) Iterator(java.util.Iterator)

Example 5 with Permissions

use of sx.blah.discord.handle.obj.Permissions in project DiscordSailv2 by Vaerys-Dawn.

the class Command method isVisibleInType.

public boolean isVisibleInType(CommandObject commandObject, SAILType type) {
    if (this.type == type) {
        return GuildHandler.testForPerms(commandObject, perms);
    } else {
        boolean hasPerms = false;
        for (SubCommandObject s : subCommands) {
            List<Permissions> allPerms = new ArrayList<>(Arrays.asList(perms));
            allPerms.addAll(Arrays.asList(s.getPermissions()));
            if (GuildHandler.testForPerms(commandObject, allPerms)) {
                hasPerms = true;
            }
        }
        return hasPerms;
    }
}
Also used : Permissions(sx.blah.discord.handle.obj.Permissions) ArrayList(java.util.ArrayList) SubCommandObject(com.github.vaerys.objects.SubCommandObject)

Aggregations

Permissions (sx.blah.discord.handle.obj.Permissions)5 CommandObject (com.github.vaerys.commands.CommandObject)3 SAILType (com.github.vaerys.enums.SAILType)3 GuildHandler (com.github.vaerys.handlers.GuildHandler)3 Utility (com.github.vaerys.main.Utility)3 SubCommandObject (com.github.vaerys.objects.SubCommandObject)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ChannelSetting (com.github.vaerys.enums.ChannelSetting)2 Arrays (java.util.Arrays)2 Pattern (java.util.regex.Pattern)2 StringUtils (org.apache.commons.lang3.StringUtils)2 RequestHandler (com.github.vaerys.handlers.RequestHandler)1 StringHandler (com.github.vaerys.handlers.StringHandler)1 Constants (com.github.vaerys.main.Constants)1 Globals (com.github.vaerys.main.Globals)1 SplitFirstObject (com.github.vaerys.objects.SplitFirstObject)1 XEmbedBuilder (com.github.vaerys.objects.XEmbedBuilder)1 Command (com.github.vaerys.templates.Command)1