Search in sources :

Example 26 with MissingArgumentException

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

the class CalcCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    try {
        ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
        String expression = context.getArg();
        BotUtils.sendMessage(Emoji.TRIANGULAR_RULER + String.format(" %s = %s", expression.replace("*", "\\*"), engine.eval(expression)), context.getChannel());
    } catch (ScriptException err) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid expression.", context.getArg()));
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) ScriptException(javax.script.ScriptException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 27 with MissingArgumentException

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

the class TranslateCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    List<String> args = StringUtils.split(context.getArg(), 3);
    if (args.size() < 2) {
        throw new MissingArgumentException();
    }
    if (args.size() == 2) {
        args.add(0, "auto");
    }
    String langFrom = this.toISO(args.get(0));
    String langTo = this.toISO(args.get(1));
    if (langFrom == null || langTo == null) {
        throw new IllegalCmdArgumentException(String.format("One of the specified language doesn't exist. " + "Use `%shelp %s` to see a complete list of supported languages.", context.getPrefix(), this.getName()));
    }
    String sourceText = args.get(2);
    try {
        String url = String.format("https://translate.googleapis.com/translate_a/single?client=gtx&sl=%s&tl=%s&dt=t&q=%s", NetUtils.encode(langFrom), NetUtils.encode(langTo), NetUtils.encode(sourceText));
        JSONArray result = new JSONArray(NetUtils.getBody(url));
        if (!(result.get(0) instanceof JSONArray)) {
            throw new IllegalCmdArgumentException(String.format("One of the specified language isn't supported. " + "Use `%shelp %s` to see a complete list of supported languages.", context.getPrefix(), this.getName()));
        }
        String translatedText = ((JSONArray) ((JSONArray) result.get(0)).get(0)).get(0).toString();
        BotUtils.sendMessage(Emoji.MAP + String.format(" **%s** (%s) <=> **%s** (%s)", sourceText, StringUtils.capitalize(LANG_ISO_MAP.inverse().get(langFrom)), translatedText, StringUtils.capitalize(LANG_ISO_MAP.inverse().get(langTo))), context.getChannel());
    } catch (JSONException | IOException err) {
        Utils.handle("getting translation", context, err);
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 28 with MissingArgumentException

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

the class UrbanCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    try {
        String url = String.format("https://api.urbandictionary.com/v0/define?term=%s", NetUtils.encode(context.getArg()));
        JSONObject mainObj = new JSONObject(NetUtils.getBody(url));
        if (mainObj.getString("result_type").equals("no_results")) {
            BotUtils.sendMessage(TextUtils.noResult(context.getArg()), context.getChannel());
            return;
        }
        JSONObject resultObj = mainObj.getJSONArray("list").getJSONObject(0);
        String definition = StringUtils.truncate(resultObj.getString("definition"), EmbedBuilder.DESCRIPTION_CONTENT_LIMIT);
        String example = StringUtils.truncate(resultObj.getString("example"), EmbedBuilder.FIELD_CONTENT_LIMIT);
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName("Urban Dictionary: " + resultObj.getString("word")).withAuthorUrl(resultObj.getString("permalink")).withThumbnail("http://www.packal.org/sites/default/files/public/styles/icon_large/public/workflow-files/florianurban/icon/icon.png").appendDescription(definition).appendField("Example", example, false);
        BotUtils.sendMessage(embed.build(), context.getChannel());
    } catch (JSONException | IOException err) {
        Utils.handle("getting Urban Dictionary definition", context, err);
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) JSONObject(org.json.JSONObject) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 29 with MissingArgumentException

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

the class CommandManager method execute.

public static void execute(Context context) {
    AbstractCommand cmd = COMMANDS_MAP.get(context.getCommandName());
    if (cmd == null) {
        return;
    }
    if (!BotUtils.isCommandAllowed(context.getGuild(), cmd)) {
        return;
    }
    CommandPermission authorPermission = context.getAuthorPermission();
    if (cmd.getPermission().isSuperior(authorPermission)) {
        BotUtils.sendMessage(Emoji.ACCESS_DENIED + " You do not have the permission to execute this command.", context.getChannel());
        return;
    }
    if (cmd.getRateLimiter() != null && cmd.getRateLimiter().isLimited(context.getChannel(), context.getAuthor())) {
        CommandStatsManager.log(CommandEnum.COMMAND_LIMITED, cmd);
        return;
    }
    try {
        cmd.execute(context);
        CommandStatsManager.log(CommandEnum.COMMAND_USED, cmd);
        VariousStatsManager.log(VariousEnum.COMMANDS_EXECUTED);
    } catch (IllegalCmdArgumentException err) {
        BotUtils.sendMessage(Emoji.GREY_EXCLAMATION + err.getMessage(), context.getChannel());
        CommandStatsManager.log(CommandEnum.COMMAND_ILLEGAL_ARG, cmd);
    } catch (MissingArgumentException err) {
        BotUtils.sendMessage(new MessageBuilder(context.getClient()).withChannel(context.getChannel()).withContent(TextUtils.MISSING_ARG).withEmbed(cmd.getHelp(context.getPrefix())));
        CommandStatsManager.log(CommandEnum.COMMAND_MISSING_ARG, cmd);
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MessageBuilder(sx.blah.discord.util.MessageBuilder) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException)

Example 30 with MissingArgumentException

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

the class ChannelSetting 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);
    if (splitArgs.size() < 2) {
        throw new MissingArgumentException();
    }
    List<IChannel> mentionedChannels = context.getMessage().getChannelMentions();
    if (mentionedChannels.isEmpty()) {
        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)));
    }
    DBGuild dbGuild = Database.getDBGuild(context.getGuild());
    List<Long> allowedChannelsList = dbGuild.getAllowedChannels();
    if (Action.ADD.equals(action)) {
        if (allowedChannelsList.isEmpty() && mentionedChannels.stream().noneMatch(channel -> channel.getLongID() == context.getChannel().getLongID())) {
            BotUtils.sendMessage(Emoji.WARNING + " You did not mentioned this channel. " + "I will not reply here until this channel is added to the list of allowed channels.", context.getChannel());
        }
        allowedChannelsList.addAll(mentionedChannels.stream().map(IChannel::getLongID).filter(channelID -> !allowedChannelsList.contains(channelID)).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Channel %s added to allowed channels.", FormatUtils.format(mentionedChannels, IChannel::mention, ", ")), context.getChannel());
    } else {
        allowedChannelsList.removeAll(mentionedChannels.stream().map(IChannel::getLongID).collect(Collectors.toList()));
        BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " Channel %s removed from allowed channels.", FormatUtils.format(mentionedChannels, IChannel::mention, ", ")), context.getChannel());
    }
    dbGuild.setSetting(this.getSetting(), new JSONArray(allowedChannelsList));
}
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) 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) IChannel(sx.blah.discord.handle.obj.IChannel) 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) IChannel(sx.blah.discord.handle.obj.IChannel) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) JSONArray(org.json.JSONArray)

Aggregations

MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)36 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)22 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)15 IOException (java.io.IOException)11 Context (me.shadorc.shadbot.core.command.Context)10 LoadingMessage (me.shadorc.shadbot.utils.object.LoadingMessage)10 List (java.util.List)9 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)9 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)8 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)8 Command (me.shadorc.shadbot.core.command.annotation.Command)8 BotUtils (me.shadorc.shadbot.utils.BotUtils)8 StringUtils (me.shadorc.shadbot.utils.StringUtils)8 HelpBuilder (me.shadorc.shadbot.utils.embed.HelpBuilder)8 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)8 DBGuild (me.shadorc.shadbot.data.db.DBGuild)7 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)7 Emoji (me.shadorc.shadbot.utils.object.Emoji)7 JSONArray (org.json.JSONArray)7 JSONException (org.json.JSONException)7