Search in sources :

Example 11 with IllegalCmdArgumentException

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

the class TransferCoinsCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    if (context.getMessage().getMentions().isEmpty()) {
        throw new MissingArgumentException();
    }
    List<String> splitCmd = StringUtils.split(context.getArg(), 2);
    if (splitCmd.size() != 2) {
        throw new MissingArgumentException();
    }
    IUser receiverUser = context.getMessage().getMentions().get(0);
    IUser senderUser = context.getAuthor();
    if (receiverUser.equals(senderUser)) {
        throw new IllegalCmdArgumentException("You cannot transfer coins to yourself.");
    }
    Integer coins = CastUtils.asPositiveInt(splitCmd.get(0));
    if (coins == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid amount for coins.", splitCmd.get(0)));
    }
    if (Database.getDBUser(context.getGuild(), senderUser).getCoins() < coins) {
        BotUtils.sendMessage(TextUtils.notEnoughCoins(context.getAuthor()), context.getChannel());
        return;
    }
    if (Database.getDBUser(context.getGuild(), receiverUser).getCoins() + coins >= Config.MAX_COINS) {
        BotUtils.sendMessage(String.format(Emoji.BANK + " This transfer cannot be done because %s would exceed the maximum coins cap.", receiverUser.getName()), context.getChannel());
        return;
    }
    Database.getDBUser(context.getGuild(), senderUser).addCoins(-coins);
    Database.getDBUser(context.getGuild(), receiverUser).addCoins(coins);
    BotUtils.sendMessage(String.format(Emoji.BANK + " %s has transfered **%s** to %s", senderUser.mention(), FormatUtils.formatCoins(coins), receiverUser.mention()), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) IUser(sx.blah.discord.handle.obj.IUser)

Example 12 with IllegalCmdArgumentException

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

the class HolidaysCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        throw new MissingArgumentException();
    }
    Zone zone = Utils.getValueOrNull(Zone.class, context.getArg());
    if (zone == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid zone. %s", context.getArg(), FormatUtils.formatOptions(Zone.class)));
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading holiday information...", context.getChannel());
    loadingMsg.send();
    try {
        String holidays = StringUtils.remove(TwitterUtils.getLastTweet("Vacances_Zone" + zone), "#");
        loadingMsg.edit(Emoji.BEACH + " " + holidays);
    } catch (TwitterException err) {
        loadingMsg.delete();
        Utils.handle("getting holidays information", context, err.getCause());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) TwitterException(twitter4j.TwitterException)

Example 13 with IllegalCmdArgumentException

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

the class LottoCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    if (!context.hasArg()) {
        this.show(context);
        return;
    }
    DBUser dbUser = Database.getDBUser(context.getGuild(), context.getAuthor());
    if (dbUser.getCoins() < PAID_COST) {
        BotUtils.sendMessage(TextUtils.notEnoughCoins(context.getAuthor()), context.getChannel());
        return;
    }
    LottoPlayer player = LottoManager.getPlayers().stream().filter(lottoPlayer -> lottoPlayer.getUserID() == context.getAuthor().getLongID()).findAny().orElse(null);
    if (player != null) {
        throw new IllegalCmdArgumentException("You're already participating.");
    }
    Integer num = CastUtils.asIntBetween(context.getArg(), MIN_NUM, MAX_NUM);
    if (num == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid number, it must be between %d and %d.", context.getArg(), MIN_NUM, MAX_NUM));
    }
    dbUser.addCoins(-PAID_COST);
    LottoManager.addPlayer(context.getGuild(), context.getAuthor(), num);
    BotUtils.sendMessage(String.format(Emoji.TICKET + " You bought a lottery ticket and bet on number **%d**. Good luck !", num), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) LottoPlayer(me.shadorc.shadbot.data.lotto.LottoPlayer) DBUser(me.shadorc.shadbot.data.db.DBUser)

Example 14 with IllegalCmdArgumentException

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

the class HangmanCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException, IllegalCmdArgumentException {
    Difficulty difficulty = Utils.getValueOrNull(Difficulty.class, context.getArg());
    if (context.hasArg() && difficulty == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid difficulty. %s", context.getArg(), FormatUtils.formatOptions(Difficulty.class)));
    }
    if (difficulty == null) {
        difficulty = Difficulty.EASY;
    }
    if (HARD_WORDS.isEmpty() || EASY_WORDS.isEmpty()) {
        LoadingMessage loadingMsg = new LoadingMessage("Loading word...", context.getChannel());
        loadingMsg.send();
        try {
            this.load();
        } catch (JSONException | IOException err) {
            Utils.handle("getting words list", context, err);
        }
        loadingMsg.delete();
    }
    HangmanManager hangmanManager = MANAGERS.get(context.getChannel().getLongID());
    if (hangmanManager == null) {
        hangmanManager = new HangmanManager(this, context.getPrefix(), context.getChannel(), context.getAuthor(), difficulty);
        if (MANAGERS.putIfAbsent(context.getChannel().getLongID(), hangmanManager) == null) {
            hangmanManager.start();
        }
    } else {
        BotUtils.sendMessage(String.format(Emoji.INFO + " A Hangman game has already been started by **%s**. Please, wait for him to finish.", hangmanManager.getAuthor().getName()), context.getChannel());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) JSONException(org.json.JSONException) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException)

Example 15 with IllegalCmdArgumentException

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

the class VolumeCmd 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;
    }
    TrackScheduler scheduler = guildMusic.getScheduler();
    if (!context.hasArg()) {
        BotUtils.sendMessage(String.format(Emoji.SOUND + " Current volume level: **%d%%**", scheduler.getAudioPlayer().getVolume()), context.getChannel());
        return;
    }
    Integer volume = CastUtils.asPositiveInt(context.getArg());
    if (volume == null) {
        throw new IllegalCmdArgumentException(String.format("`%s` is not a valid volume.", context.getArg()));
    }
    scheduler.setVolume(volume);
    BotUtils.sendMessage(String.format(Emoji.SOUND + " Volume level set to **%s%%**", scheduler.getAudioPlayer().getVolume()), context.getChannel());
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) GuildMusic(me.shadorc.shadbot.music.GuildMusic) TrackScheduler(me.shadorc.shadbot.music.TrackScheduler)

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