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