Search in sources :

Example 1 with DBUser

use of me.shadorc.shadbot.data.db.DBUser 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 2 with DBUser

use of me.shadorc.shadbot.data.db.DBUser 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());
    }
}
Also used : IllegalCmdArgumentException(me.shadorc.shadbot.exception.IllegalCmdArgumentException) DBGuild(me.shadorc.shadbot.data.db.DBGuild) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) DBUser(me.shadorc.shadbot.data.db.DBUser) IGuild(sx.blah.discord.handle.obj.IGuild)

Example 3 with DBUser

use of me.shadorc.shadbot.data.db.DBUser in project Shadbot by Shadorc.

the class LeaderboardCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    Map<String, Integer> unsortedUsersMap = new HashMap<>();
    for (DBUser dbUser : Database.getDBGuild(context.getGuild()).getUsers()) {
        int userCoin = dbUser.getCoins();
        if (userCoin > 0) {
            IUser user = context.getGuild().getUserByID(dbUser.getUserID());
            if (user != null) {
                unsortedUsersMap.put(user.getName(), userCoin);
            }
        }
    }
    Map<String, Integer> sortedUsersMap = Utils.sortByValue(unsortedUsersMap);
    List<String> usersList = new ArrayList<>(sortedUsersMap.keySet());
    String leaderboard = FormatUtils.numberedList(10, sortedUsersMap.size(), count -> String.format("%d. **%s** - %s", count, usersList.get(count - 1), FormatUtils.formatCoins(sortedUsersMap.get(usersList.get(count - 1)))));
    unsortedUsersMap.clear();
    sortedUsersMap.clear();
    if (leaderboard.isEmpty()) {
        leaderboard = "\nEveryone is poor here.";
    }
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Leaderboard").appendDescription(leaderboard);
    BotUtils.sendMessage(embed.build(), context.getChannel());
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IUser(sx.blah.discord.handle.obj.IUser) DBUser(me.shadorc.shadbot.data.db.DBUser)

Aggregations

DBUser (me.shadorc.shadbot.data.db.DBUser)3 IllegalCmdArgumentException (me.shadorc.shadbot.exception.IllegalCmdArgumentException)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DBGuild (me.shadorc.shadbot.data.db.DBGuild)1 LottoPlayer (me.shadorc.shadbot.data.lotto.LottoPlayer)1 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)1 IGuild (sx.blah.discord.handle.obj.IGuild)1 IUser (sx.blah.discord.handle.obj.IUser)1 EmbedBuilder (sx.blah.discord.util.EmbedBuilder)1