Search in sources :

Example 31 with MissingArgumentException

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

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

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

Example 34 with MissingArgumentException

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

the class SettingsCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    List<String> splitArgs = StringUtils.split(context.getArg(), 2);
    if (splitArgs.isEmpty()) {
        throw new MissingArgumentException();
    }
    SettingEnum settingEnum = Utils.getValueOrNull(SettingEnum.class, splitArgs.get(0));
    if (settingEnum == null || !SETTINGS_MAP.containsKey(settingEnum)) {
        throw new IllegalCmdArgumentException(String.format("Setting `%s` does not exist. Use `%shelp %s` to see all available settings.", splitArgs.get(0), context.getPrefix(), this.getName()));
    }
    AbstractSetting setting = SETTINGS_MAP.get(settingEnum);
    String arg = splitArgs.size() == 2 ? splitArgs.get(1) : null;
    if ("help".equals(arg)) {
        BotUtils.sendMessage(this.getHelp(context.getPrefix(), setting), context.getChannel());
        return;
    }
    try {
        setting.execute(context, arg);
    } catch (MissingArgumentException e) {
        BotUtils.sendMessage(new MessageBuilder(context.getClient()).withChannel(context.getChannel()).withContent(TextUtils.MISSING_ARG).withEmbed(this.getHelp(context.getPrefix(), setting)));
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) AbstractSetting(me.shadorc.shadbot.command.admin.setting.core.AbstractSetting) MessageBuilder(sx.blah.discord.util.MessageBuilder) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) SettingEnum(me.shadorc.shadbot.command.admin.setting.core.SettingEnum)

Example 35 with MissingArgumentException

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

the class SoftBanCmd 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.BAN)) {
        throw new IllegalArgumentException("You don't have permission to ban.");
    }
    if (!BotUtils.hasPermissions(context.getChannel(), Permissions.BAN)) {
        BotUtils.sendMessage(TextUtils.missingPerm(Permissions.BAN), context.getChannel());
        return;
    }
    if (mentionedUsers.contains(context.getAuthor())) {
        throw new IllegalCmdArgumentException("You cannot softban yourself.");
    }
    for (IUser mentionedUser : mentionedUsers) {
        if (!PermissionUtils.isUserHigher(context.getGuild(), context.getAuthor(), mentionedUser)) {
            throw new IllegalCmdArgumentException(String.format("You can't softban **%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 softban **%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 softbanned from the server **%s** by **%s**. Reason: `%s`", context.getGuild().getName(), context.getAuthorName(), reason), user.getOrCreatePMChannel());
        }
        RequestBuffer.request(() -> {
            context.getGuild().banUser(user, reason.toString(), 7);
        });
        RequestBuffer.request(() -> {
            context.getGuild().pardonUser(user.getLongID());
        });
    }
    BotUtils.sendMessage(String.format(Emoji.INFO + " (Requested by **%s**) **%s** got softbanned. 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

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