Search in sources :

Example 16 with IllegalCmdArgumentException

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

the class DatabaseCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<String> splitArgs = StringUtils.split(context.getArg());
    if (splitArgs.size() > 2) {
        throw new MissingArgumentException();
    }
    Long guildID = CastUtils.asPositiveLong(splitArgs.get(0));
    if (guildID == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid guild ID.", splitArgs.get(0)));
    }
    IGuild guild = context.getClient().getGuildByID(guildID);
    if (guild == null) {
        throw new IllegalCmdArgumentException("Guild not found.");
    }
    String json = null;
    if (splitArgs.size() == 1) {
        DBGuild dbGuild = Database.getDBGuild(guild);
        json = dbGuild.toJSON().toString(Config.JSON_INDENT_FACTOR);
    } else if (splitArgs.size() == 2) {
        Long userID = CastUtils.asPositiveLong(splitArgs.get(1));
        if (userID == null) {
            throw new IllegalCmdArgumentException(String.format("`%s` is not a valid user ID.", splitArgs.get(0)));
        }
        DBUser dbUser = new DBUser(guild, userID);
        json = dbUser.toJSON().toString(Config.JSON_INDENT_FACTOR);
    }
    if (json == null || json.length() == 2) {
        BotUtils.sendMessage(Emoji.MAGNIFYING_GLASS + " Nothing found.", context.getChannel());
    } else {
        BotUtils.sendMessage(json, context.getChannel());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) DBUser(me.shadorc.shadbot.data.db.DBUser) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 17 with IllegalCmdArgumentException

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

the class GenerateRelicCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    RelicType type = Utils.getValueOrNull(RelicType.class, context.getArg());
    if (type == null) {
        throw new IllegalCmdArgumentException(String.format("`%s`in not a valid type. %s", context.getArg(), FormatUtils.formatOptions(RelicType.class)));
    }
    Relic relic = PremiumManager.generateRelic(type);
    BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " %s relic generated: **%s**", StringUtils.capitalize(type.toString()), relic.getRelicID()), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) Relic(me.shadorc.shadbot.data.premium.Relic) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) RelicType(me.shadorc.shadbot.data.premium.RelicType)

Example 18 with IllegalCmdArgumentException

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

the class SendMessageCmd 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();
    }
    Long userID = CastUtils.asPositiveLong(splitArgs.get(0));
    if (userID == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid user ID.", splitArgs.get(0)));
    }
    IUser user = Shadbot.getClient().getUserByID(userID);
    if (user == null) {
        BotUtils.sendMessage(Emoji.GREY_EXCLAMATION + " User not found.", context.getChannel());
        return;
    }
    if (user.equals(context.getOurUser())) {
        throw new IllegalCmdArgumentException("I can't send a private message to myself.");
    }
    if (user.isBot()) {
        throw new IllegalCmdArgumentException("I can't send private message to other bots.");
    }
    context.getClient().getOrCreatePMChannel(user).sendMessage(splitArgs.get(1));
    BotUtils.sendMessage(Emoji.CHECK_MARK + " Message sent.", context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IUser(sx.blah.discord.handle.obj.IUser)

Example 19 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException 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 20 with IllegalCmdArgumentException

use of me.shadorc.shadbot.exception.IllegalCmdArgumentException 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)

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