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());
}
}
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());
}
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());
}
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()));
}
}
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);
}
}
Aggregations