Search in sources :

Example 6 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.

the class AutoRoleSetting method execute.

@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!BotUtils.hasPermissions(context.getChannel(), Permissions.MANAGE_ROLES)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.MANAGE_ROLES), context.getChannel());
        LogUtils.infof("{Guild ID: %d} Shadbot wasn't allowed to manage roles.", context.getGuild().getLongID());
        return;
    }
    if (arg == null) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(arg);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
    if (action == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
    }
    List<IRole> mentionedRoles = context.getMessage().getRoleMentions();
    if (mentionedRoles.isEmpty()) {
        throw new MissingArgumentException();
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<Long> roles = dbGuild.getAutoRoles();
    if (Action.ADD.equals(action)) {
        for (IRole role : mentionedRoles) {
            if (!PermissionUtils.hasHierarchicalPermissions(context.getGuild(), context.getOurUser(), Arrays.asList(role))) {
                throw new IllegalCmdArgumentException(String.format("%s is a higher role in the role hierarchy than mine, I can't auto-assign it.", role.mention()));
            }
        }
        roles.addAll(mentionedRoles.stream().map(IRole::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format("New comers will now have role(s): %s", FormatUtils.format(roles, role -> context.getGuild().getRoleByID(role).mention(), ", ")), context.getChannel());
    } else {
        roles.removeAll(mentionedRoles.stream().map(IRole::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format("%s removed from auto-assigned roles.", FormatUtils.format(mentionedRoles, IRole::mention, ", ")), context.getChannel());
    }
    dbGuild.setSetting(SettingEnum.AUTO_ROLE, new JSONArray(roles));
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) IRole(sx.blah.discord.handle.obj.IRole) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 7 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.

the class BlacklistSettingCmd method execute.

@Override
public void execute(Context context, String arg) throws MissingArgumentException, IllegalCmdArgumentException {
    if (arg == null) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(arg, 2);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Action action = Utils.getValueOrNull(Action.class, splitArgs.get(0));
    if (action == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid action. %s", splitArgs.get(0), FormatUtils.formatOptions(Action.class)));
    }
    List<String> commands = StringUtils.split(splitArgs.get(1).toLowerCase());
    List<String> unknownCmds = commands.stream().filter(cmd -> CommandManager.getCommand(cmd) == null).collect(Collectors.toList());
    if (!unknownCmds.isEmpty()) {
        throw new IllegalCmdArgumentException(String.format("Command %s doesn't exist.", FormatUtils.format(unknownCmds, cmd -> String.format("`%s`", cmd), ", ")));
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<String> blacklist = dbGuild.getBlacklistedCmd();
    if (Action.ADD.equals(action)) {
        blacklist.addAll(commands);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Command(s) `%s` added to the blacklist.", FormatUtils.format(commands, cmd -> String.format("`%s`", cmd), ", ")), context.getChannel());
    } else if (Action.REMOVE.equals(action)) {
        blacklist.removeAll(commands);
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Command(s) `%s` removed from the blacklist.", FormatUtils.format(commands, cmd -> String.format("`%s`", cmd), ", ")), context.getChannel());
    }
    dbGuild.setSetting(this.getSetting(), new JSONArray(blacklist));
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) SettingEnum(me.shadorc.shadbot.command.admin.setting.core.SettingEnum) DBGuild(me.shadorc.shadbot.data.db.DBGuild) AbstractSetting(me.shadorc.shadbot.command.admin.setting.core.AbstractSetting) CommandManager(me.shadorc.shadbot.core.command.CommandManager) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) List(java.util.List) Context(me.shadorc.shadbot.core.command.Context) Database(me.shadorc.shadbot.data.db.Database) Setting(me.shadorc.shadbot.command.admin.setting.core.Setting) Emoji(me.shadorc.shadbot.utils.object.Emoji) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) JSONArray(org.json.JSONArray) Utils(me.shadorc.shadbot.utils.Utils) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Example 8 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.

the class TriviaCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (categories.isEmpty()) {
        this.load();
    }
    if (context.getArg().equals("categories")) {
        EmbedObject embed = EmbedUtils.getDefaultEmbed().withAuthorName("Trivia categories").appendField("ID", FormatUtils.format(categories.keySet().toArray(), Object::toString, "\n"), true).appendField("Name", FormatUtils.format(categories.keySet().toArray(), categories::get, "\n"), true).build();
        BotUtils.sendMessage(embed, context.getChannel());
        return;
    }
    Integer categoryID = CastUtils.asPositiveInt(context.getArg());
    if (context.hasArg() && (categoryID == null || !categories.containsKey(categoryID))) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid ID. Use `%s%s categories` to see the complete list " + "of categories.", context.getArg(), context.getPrefix(), this.getName()));
    }
    TriviaManager triviaManager = MANAGERS.get(context.getChannel().getLongID());
    if (triviaManager == null) {
        triviaManager = new TriviaManager(this, context.getPrefix(), context.getChannel(), context.getAuthor(), categoryID);
    }
    if (MANAGERS.putIfAbsent(context.getChannel().getLongID(), triviaManager) == null) {
        try {
            triviaManager.start();
        } catch (ParseException err) {
            BotUtils.sendMessage(Emoji.RED_FLAG + " I can't get a question right now, please try again later.", context.getChannel());
            LogUtils.infof("{%s} Empty body.", this.getClass().getSimpleName());
            MANAGERS.remove(context.getChannel().getLongID());
        } catch (JSONException | IOException err) {
            Utils.handle("getting a question", context, err);
            MANAGERS.remove(context.getChannel().getLongID());
        }
    } else {
        BotUtils.sendMessage(Emoji.INFO + " A Trivia game has already been started.", context.getChannel());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) JSONException(org.json.JSONException) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject)

Example 9 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.

the class DiabloCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(context.getArg(), 2);
    if (splitArgs.size() != 2) {
        throw new MissingArgumentException();
    }
    Region region = Utils.getValueOrNull(Region.class, splitArgs.get(0));
    if (region == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid Region. %s", splitArgs.get(0), FormatUtils.formatOptions(Region.class)));
    }
    String battletag = splitArgs.get(1);
    if (!battletag.matches("(\\p{L}*)#[0-9]*")) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid Battletag.", splitArgs.get(1)));
    }
    battletag = battletag.replaceAll("#", "-");
    LoadingMessage loadingMsg = new LoadingMessage("Loading Diablo 3 stats...", context.getChannel());
    loadingMsg.send();
    try {
        String url = String.format("https://%s.api.battle.net/d3/profile/%s/?locale=en_GB&apikey=%s", region, NetUtils.encode(battletag), APIKeys.get(APIKey.BLIZZARD_API_KEY));
        JSONObject playerObj = new JSONObject(NetUtils.getBody(url));
        if (playerObj.has("code") && playerObj.getString("code").equals("NOTFOUND")) {
            loadingMsg.edit(Emoji.MAGNIFYING_GLASS + " This user doesn't play Diablo 3 or doesn't exist.");
            return;
        }
        TreeMap<Double, String> heroesMap = new TreeMap<>(Collections.reverseOrder());
        JSONArray heroesArray = playerObj.getJSONArray("heroes");
        for (int i = 0; i < heroesArray.length(); i++) {
            JSONObject heroObj = heroesArray.getJSONObject(i);
            String name = heroObj.getString("name");
            String heroClass = StringUtils.capitalize(heroObj.getString("class").replace("-", " "));
            url = String.format("https://%s.api.battle.net/d3/profile/%s/hero/%d?locale=en_GB&apikey=%s", region, NetUtils.encode(battletag), heroObj.getLong("id"), APIKeys.get(APIKey.BLIZZARD_API_KEY));
            JSONObject statsHeroObj = new JSONObject(NetUtils.getBody(url));
            Double dps = statsHeroObj.has("code") ? Double.NaN : statsHeroObj.getJSONObject("stats").getDouble("damage");
            heroesMap.put(dps, String.format("**%s** (*%s*)", name, heroClass));
        }
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Diablo 3 Stats").withThumbnail("http://osx.wdfiles.com/local--files/icon:d3/D3.png").appendDescription(String.format("Stats for **%s** (Guild: **%s**)" + "%n%nParangon level: **%s** (*Normal*) / **%s** (*Hardcore*)" + "%nSeason Parangon level: **%s** (*Normal*) / **%s** (*Hardcore*)", playerObj.getString("battleTag"), playerObj.getString("guildName"), playerObj.getInt("paragonLevel"), playerObj.getInt("paragonLevelSeasonHardcore"), playerObj.getInt("paragonLevelSeason"), playerObj.getInt("paragonLevelSeasonHardcore"))).appendField("Heroes", FormatUtils.format(heroesMap.values().stream(), Object::toString, "\n"), true).appendField("Damage", FormatUtils.format(heroesMap.keySet().stream(), dps -> String.format("%s DPS", FormatUtils.formatNum(dps)), "\n"), true);
        loadingMsg.edit(embed.build());
    } catch (FileNotFoundException err) {
        loadingMsg.delete();
        BotUtils.sendMessage(Emoji.MAGNIFYING_GLASS + " This user doesn't play Diablo 3 or doesn't exist.", context.getChannel());
    } catch (JSONException | IOException err) {
        loadingMsg.delete();
        Utils.handle("getting Diablo 3 stats", context, err);
    }
}
Also used : HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Utils(me.shadorc.shadbot.utils.Utils) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) APIKeys(me.shadorc.shadbot.data.APIKeys) IOException(java.io.IOException) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) FileNotFoundException(java.io.FileNotFoundException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) List(java.util.List) NetUtils(me.shadorc.shadbot.utils.NetUtils) TreeMap(java.util.TreeMap) Context(me.shadorc.shadbot.core.command.Context) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Emoji(me.shadorc.shadbot.utils.object.Emoji) APIKey(me.shadorc.shadbot.data.APIKeys.APIKey) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) Collections(java.util.Collections) JSONArray(org.json.JSONArray) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage)

Example 10 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException in project Shadbot by Shadorc.

the class HelpCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (context.hasArg()) {
        AbstractCommand cmd = CommandManager.getCommand(context.getArg());
        if (cmd == null) {
            return;
        }
        BotUtils.sendMessage(cmd.getHelp(context.getPrefix()), context.getChannel());
        CommandStatsManager.log(CommandEnum.COMMAND_HELPED, cmd);
        return;
    }
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Shadbot Help").appendDescription(String.format("Any issues, questions or suggestions ? Join the [support server.](%s)" + "%nGet more information by using `%s%s <command>`.", Config.SUPPORT_SERVER, context.getPrefix(), this.getName()));
    for (CommandCategory category : CommandCategory.values()) {
        if (category.equals(CommandCategory.HIDDEN)) {
            continue;
        }
        String commands = CommandManager.getCommands().values().stream().distinct().filter(cmd -> cmd.getCategory().equals(category) && !cmd.getPermission().isSuperior(context.getAuthorPermission()) && (context.getGuild() == null || BotUtils.isCommandAllowed(context.getGuild(), cmd))).map(cmd -> String.format("`%s%s`", context.getPrefix(), cmd.getName())).collect(Collectors.joining(" "));
        embed.appendField(String.format("%s Commands", category.toString()), commands, false);
    }
    BotUtils.sendMessage(embed.build(), context.getChannel());
}
Also used : RateLimited(me.shadorc.shadbot.core.command.annotation.RateLimited) HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) CommandManager(me.shadorc.shadbot.core.command.CommandManager) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) Collectors(java.util.stream.Collectors) BotUtils(me.shadorc.shadbot.utils.BotUtils) CommandStatsManager(me.shadorc.shadbot.data.stats.CommandStatsManager) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) Command(me.shadorc.shadbot.core.command.annotation.Command) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Context(me.shadorc.shadbot.core.command.Context) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) CommandEnum(me.shadorc.shadbot.data.stats.CommandStatsManager.CommandEnum) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) Config(me.shadorc.shadbot.Config) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory)

Aggregations

IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)27 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)22 Context (me.shadorc.shadbot.core.command.Context)8 List (java.util.List)7 BotUtils (me.shadorc.shadbot.utils.BotUtils)7 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)7 StringUtils (me.shadorc.shadbot.utils.StringUtils)7 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)7 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)6 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)6 Command (me.shadorc.shadbot.core.command.annotation.Command)6 GuildMusic (me.shadorc.shadbot.music.GuildMusic)6 HelpBuilder (me.shadorc.shadbot.utils.embed.HelpBuilder)6 Emoji (me.shadorc.shadbot.utils.object.Emoji)6 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)5 JSONArray (org.json.JSONArray)5 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)5 IOException (java.io.IOException)4 DBGuild (me.shadorc.shadbot.data.db.DBGuild)4 TextUtils (me.shadorc.shadbot.utils.TextUtils)4