Search in sources :

Example 21 with IllegalCmdArgumentException

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

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

Example 23 with IllegalCmdArgumentException

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

the class BackwardCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    GuildMusic guildMusic = GuildMusicManager.GUILD_MUSIC_MAP.get(context.getGuild().getLongID());
    if (guildMusic == null || guildMusic.getScheduler().isStopped()) {
        BotUtils.sendMessage(TextUtils.NO_PLAYING_MUSIC, context.getChannel());
        return;
    }
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    Long num = CastUtils.asPositiveLong(context.getArg());
    if (num == null) {
        try {
            num = TimeUtils.parseTime(context.getArg());
        } catch (IllegalArgumentException err) {
            throw new IllegalCmdArgumentException(String.format("`%s` is not a valid number / time.", context.getArg()));
        }
    }
    long newPosition = guildMusic.getScheduler().changePosition(-TimeUnit.SECONDS.toMillis(num));
    BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " New position: **%s**", FormatUtils.formatShortDuration(newPosition)), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) GuildMusic(me.shadorc.shadbot.music.GuildMusic)

Example 24 with IllegalCmdArgumentException

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

the class ForwardCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    GuildMusic guildMusic = GuildMusicManager.GUILD_MUSIC_MAP.get(context.getGuild().getLongID());
    if (guildMusic == null || guildMusic.getScheduler().isStopped()) {
        BotUtils.sendMessage(TextUtils.NO_PLAYING_MUSIC, context.getChannel());
        return;
    }
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    Long num = CastUtils.asPositiveLong(context.getArg());
    if (num == null) {
        try {
            num = TimeUtils.parseTime(context.getArg());
        } catch (IllegalArgumentException err) {
            throw new IllegalCmdArgumentException(String.format("`%s` is not a valid number / time.", context.getArg()));
        }
    }
    long newPosition = guildMusic.getScheduler().changePosition(TimeUnit.SECONDS.toMillis(num));
    BotUtils.sendMessage(String.format(Emoji.CHECK_MARK + " New position: **%s**", FormatUtils.formatShortDuration(newPosition)), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) GuildMusic(me.shadorc.shadbot.music.GuildMusic)

Example 25 with IllegalCmdArgumentException

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

the class KickCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    List<IUser> mentionedUsers = context.getMessage().getMentions();
    if (mentionedUsers.isEmpty()) {
        throw new MissingArgumentException();
    }
    if (!PermissionUtils.hasPermissions(context.getChannel(), context.getAuthor(), Permissions.KICK)) {
        throw new IllegalArgumentException("You don't have permission to kick.");
    }
    if (!BotUtils.hasPermissions(context.getChannel(), Permissions.KICK)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.KICK), context.getChannel());
        return;
    }
    if (mentionedUsers.contains(context.getAuthor())) {
        throw new IllegalCmdArgumentException("You cannot kick yourself.");
    }
    for (IUser mentionedUser : mentionedUsers) {
        if (!PermissionUtils.isUserHigher(context.getGuild(), context.getAuthor(), mentionedUser)) {
            throw new IllegalCmdArgumentException(String.format("You can't kick **%s** because he has the same or a higher role " + "position than you in the role hierarchy.", mentionedUser.getName()));
        }
        if (!BotUtils.canInteract(context.getGuild(), mentionedUser)) {
            throw new IllegalCmdArgumentException(String.format("I cannot kick **%s** because he has the same or a higher role " + "position than me in the role hierarchy.", mentionedUser.getName()));
        }
    }
    StringBuilder reason = new StringBuilder();
    reason.append(StringUtils.remove(context.getArg(), FormatUtils.format(mentionedUsers, user -> user.mention(false), " ")).trim());
    if (reason.length() > Ban.MAX_REASON_LENGTH) {
        throw new IllegalCmdArgumentException(String.format("Reason cannot exceed **%d characters**.", Ban.MAX_REASON_LENGTH));
    }
    if (reason.length() == 0) {
        reason.append("Reason not specified.");
    }
    for (IUser user : mentionedUsers) {
        if (!user.isBot()) {
            BotUtils.sendMessage(String.format(Emoji.INFO + " You were kicked from the server **%s** by **%s**. Reason: `%s`", context.getGuild().getName(), context.getAuthorName(), reason), user.getOrCreatePMChannel());
        }
        RequestBuffer.request(() -> {
            context.getGuild().kickUser(user, reason.toString());
        }).get();
    }
    BotUtils.sendMessage(String.format(Emoji.INFO + " (Requested by **%s**) **%s** got kicked. Reason: `%s`", context.getAuthorName(), FormatUtils.format(mentionedUsers, IUser::getName, ", "), reason), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) Ban(sx.blah.discord.util.Ban) RequestBuffer(sx.blah.discord.util.RequestBuffer) HelpBuilder(me.shadorc.shadbot.utils.embed.HelpBuilder) CommandCategory(me.shadorc.shadbot.core.command.CommandCategory) PermissionUtils(sx.blah.discord.util.PermissionUtils) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) BotUtils(me.shadorc.shadbot.utils.BotUtils) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) StringUtils(me.shadorc.shadbot.utils.StringUtils) CommandPermission(me.shadorc.shadbot.core.command.CommandPermission) Command(me.shadorc.shadbot.core.command.annotation.Command) List(java.util.List) IUser(sx.blah.discord.handle.obj.IUser) Context(me.shadorc.shadbot.core.command.Context) TextUtils(me.shadorc.shadbot.utils.TextUtils) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Emoji(me.shadorc.shadbot.utils.object.Emoji) Permissions(sx.blah.discord.handle.obj.Permissions) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IUser(sx.blah.discord.handle.obj.IUser)

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