Search in sources :

Example 11 with Subcommand

use of tk.ardentbot.core.executor.Subcommand in project Ardent by adamint.

the class RPGMoney method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("See who has the most money - globally!", "top", "top") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (args.length == 2) {
                HashMap<User, Double> moneyAmounts = new HashMap<>();
                Cursor<HashMap> top = r.db("data").table("profiles").orderBy().optArg("index", r.desc("money")).limit(20).run(connection);
                top.forEach(hashMap -> {
                    Profile profile = asPojo(hashMap, Profile.class);
                    assert profile.getUser() != null;
                    moneyAmounts.put(profile.getUser(), profile.getMoney());
                });
                Map<User, Double> sortedAmounts = MapUtils.sortByValue(moneyAmounts);
                String topMoney = "Global Richest Users";
                EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
                builder.setAuthor(topMoney, getShard().url, guild.getIconUrl());
                StringBuilder description = new StringBuilder();
                final int[] current = { 0 };
                sortedAmounts.forEach((u, money) -> {
                    if (u != null) {
                        description.append("\n#" + (current[0] + 1) + ": **" + u.getName() + "** " + RPGUtils.formatMoney(money));
                        current[0]++;
                    }
                });
                description.append("\n\nGet money by sending commands or asking questions on our support server ( https://ardentbot" + ".tk/guild )\n\nSee people's money by doing /money @User or see yours by just using /money");
                sendEmbed(builder.setDescription(description.toString()), channel, user);
                return;
            }
            try {
                int page = Integer.parseInt(args[2]) - 1;
                HashMap<User, Double> moneyAmounts = new HashMap<>();
                Cursor<HashMap> top = r.db("data").table("profiles").orderBy().optArg("index", r.desc("money")).slice((page * 20), (page * 20) + 11).limit(25).run(connection);
                top.forEach(hashMap -> {
                    Profile profile = asPojo(hashMap, Profile.class);
                    assert profile.getUser() != null;
                    moneyAmounts.put(profile.getUser(), profile.getMoney());
                });
                Map<User, Double> sortedAmounts = MapUtils.sortByValue(moneyAmounts);
                String topMoney = "Global Richest Users | Page " + page;
                EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
                builder.setAuthor(topMoney, getShard().url, guild.getIconUrl());
                StringBuilder description = new StringBuilder();
                final int[] current = { 0 };
                sortedAmounts.forEach((u, money) -> {
                    if (u != null) {
                        description.append("\n#" + ((page * 20) + current[0] + 1) + ": **" + u.getName() + "** " + RPGUtils.formatMoney(money));
                        current[0]++;
                    }
                });
                description.append("\n\nGet money by sending commands or asking questions on our support server ( https://ardentbot" + ".tk/guild )\n\nSee people's money by doing /money @User or see yours by just using /money");
                sendEmbed(builder.setDescription(description.toString()), channel, user);
            } catch (NumberFormatException e) {
                sendTranslatedMessage("You need to specify a valid page number!", channel, user);
            }
        }
    });
    subcommands.add(new Subcommand("See who has the most money in your server", "server", "server", "guild", "topguild") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (!generatedFirstTimeFor.contains(guild.getId())) {
                sendTranslatedMessage("Please wait a second, generating and caching your server's statistics", channel, user);
                generatedFirstTimeFor.add(guild.getId());
            }
            HashMap<User, Double> moneyAmounts = new HashMap<>();
            guild.getMembers().forEach(member -> {
                User u = member.getUser();
                moneyAmounts.put(u, Profile.get(u).getMoney());
            });
            Map<User, Double> sortedAmounts = MapUtils.sortByValue(moneyAmounts);
            EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
            builder.setAuthor("Richest Users | This Server", getShard().url, guild.getIconUrl());
            StringBuilder description = new StringBuilder();
            description.append("**Richest Users in this Server**");
            final int[] current = { 0 };
            sortedAmounts.forEach((u, money) -> {
                if (current[0] < 10) {
                    description.append("\n#" + (current[0] + 1) + ": **" + u.getName() + "** " + RPGUtils.formatMoney(money));
                    current[0]++;
                }
            });
            description.append("\n\nGet money by sending commands or asking questions on our support server ( https://ardentbot" + ".tk/guild )\n\nSee people's money by doing /money @User or see yours by just using /money");
            sendEmbed(builder.setDescription(description.toString()), channel, user);
        }
    });
}
Also used : MessageUtils(tk.ardentbot.utils.discord.MessageUtils) Ratelimitable(tk.ardentbot.core.executor.Ratelimitable) Subcommand(tk.ardentbot.core.executor.Subcommand) Database.r(tk.ardentbot.rethink.Database.r) HashMap(java.util.HashMap) Profile(tk.ardentbot.utils.rpg.profiles.Profile) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) ArrayList(java.util.ArrayList) Guild(net.dv8tion.jda.core.entities.Guild) List(java.util.List) Cursor(com.rethinkdb.net.Cursor) User(net.dv8tion.jda.core.entities.User) RPGUtils(tk.ardentbot.utils.rpg.RPGUtils) MapUtils(tk.ardentbot.utils.MapUtils) Database.connection(tk.ardentbot.rethink.Database.connection) Map(java.util.Map) User(net.dv8tion.jda.core.entities.User) Subcommand(tk.ardentbot.core.executor.Subcommand) Message(net.dv8tion.jda.core.entities.Message) HashMap(java.util.HashMap) Guild(net.dv8tion.jda.core.entities.Guild) Cursor(com.rethinkdb.net.Cursor) Profile(tk.ardentbot.utils.rpg.profiles.Profile) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with Subcommand

use of tk.ardentbot.core.executor.Subcommand in project Ardent by adamint.

the class Bet method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Bet a specified amount of money", "start", "start") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (args.length == 2) {
                sendTranslatedMessage("You need to specify a bet amount", channel, user);
                return;
            }
            try {
                double amountToBet = Double.parseDouble(args[2]);
                Profile profile = Profile.get(user);
                if (amountToBet <= 0 || profile.getMoney() < amountToBet) {
                    sendTranslatedMessage("Invalid amount - either it was more than what you have or less than $0", channel, user);
                    return;
                }
                sendEditedTranslation("Are you sure you want to bet **{0}**? Type **yes** if so, or **no** to cancel", user, channel, RPGUtils.formatMoney(amountToBet));
                interactiveOperation(channel, message, (returnedMessage) -> {
                    if (returnedMessage.getContent().equalsIgnoreCase("yes")) {
                        sendTranslatedMessage("Type 1 or 2 below - choose wisely!", channel, user);
                        interactiveOperation(channel, message, (numberInput) -> {
                            try {
                                int num = Integer.parseInt(numberInput.getContent());
                                if (num > 0 && num <= 2) {
                                    int generated = new SecureRandom().nextInt(2) + 1;
                                    if (num == generated || new Random().nextInt(20) == 5) {
                                        profile.addMoney(profile.afterCredit(amountToBet));
                                        sendTranslatedMessage("Congrats! You won " + RPGUtils.formatMoney(profile.afterCredit(amountToBet)), channel, user);
                                    } else {
                                        sendTranslatedMessage("Sorry, you lost " + RPGUtils.formatMoney(amountToBet) + " :frowning: " + "The correct answer " + "was " + generated, channel, user);
                                        profile.removeMoney(amountToBet);
                                    }
                                } else
                                    sendTranslatedMessage("You specified an invalid number", channel, user);
                            } catch (Exception ex) {
                                sendTranslatedMessage("You specified an invalid number", channel, user);
                            }
                        });
                    } else {
                        sendTranslatedMessage("Ok, cancelled your bet", channel, user);
                    }
                });
            } catch (NumberFormatException e) {
                sendTranslatedMessage("That's not a number!", channel, user);
            }
        }
    });
    subcommands.add(new Subcommand("Bet all your money", "all", "all") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            Profile profile = Profile.get(user);
            if (profile.getMoney() < 0) {
                sendTranslatedMessage("You can't bet if you're in debt!", channel, user);
                return;
            }
            sendEditedTranslation("Are you sure you want to bet all? Type **yes** if so, or **no** to cancel", user, channel, RPGUtils.formatMoney(profile.getMoney()));
            interactiveOperation(channel, message, (returnedMessage) -> {
                if (returnedMessage.getContent().equalsIgnoreCase("yes")) {
                    sendTranslatedMessage("Type 1 or 2 below - choose wisely!", channel, user);
                    interactiveOperation(channel, message, (numberInput) -> {
                        try {
                            int num = Integer.parseInt(numberInput.getContent());
                            if (num > 0 && num <= 2) {
                                int generated = new SecureRandom().nextInt(2) + 1;
                                if (num == generated || new Random().nextInt(20) == 5) {
                                    profile.addMoney(profile.afterCredit(profile.getMoney()));
                                    sendTranslatedMessage("Congrats! You won " + RPGUtils.formatMoney(profile.afterCredit(profile.getMoney())), channel, user);
                                } else {
                                    sendTranslatedMessage("Sorry, you lost " + RPGUtils.formatMoney(profile.getMoney()) + " :frowning: The correct answer " + "was " + generated, channel, user);
                                    profile.setZero();
                                }
                            } else
                                sendTranslatedMessage("You specified an invalid number", channel, user);
                        } catch (Exception ex) {
                            sendTranslatedMessage("You specified an invalid number", channel, user);
                        }
                    });
                } else {
                    sendTranslatedMessage("Ok, cancelled your bet", channel, user);
                }
            });
        }
    });
}
Also used : SecureRandom(java.security.SecureRandom) Guild(net.dv8tion.jda.core.entities.Guild) User(net.dv8tion.jda.core.entities.User) RPGUtils(tk.ardentbot.utils.rpg.RPGUtils) Command(tk.ardentbot.core.executor.Command) Subcommand(tk.ardentbot.core.executor.Subcommand) Random(java.util.Random) Profile(tk.ardentbot.utils.rpg.profiles.Profile) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Message(net.dv8tion.jda.core.entities.Message) User(net.dv8tion.jda.core.entities.User) Subcommand(tk.ardentbot.core.executor.Subcommand) Message(net.dv8tion.jda.core.entities.Message) SecureRandom(java.security.SecureRandom) Guild(net.dv8tion.jda.core.entities.Guild) Profile(tk.ardentbot.utils.rpg.profiles.Profile) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) SecureRandom(java.security.SecureRandom) Random(java.util.Random)

Example 13 with Subcommand

use of tk.ardentbot.core.executor.Subcommand in project Ardent by adamint.

the class Stats method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("See what commands are being used", "commands", "commands") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            StringBuilder commandBars = new StringBuilder();
            Map<String, Long> commandsUsed = getCommandData(ShardManager.getShards());
            final int[] counter = { 0 };
            final int[] totalCommandsReceived = { 0 };
            commandsUsed.forEach((key, value) -> {
                if (counter[0] < 7)
                    totalCommandsReceived[0] += value;
                counter[0]++;
            });
            counter[0] = 0;
            commandsUsed.forEach((key, value) -> {
                if (counter[0] < 7) {
                    int percent = (int) (value * 100 / totalCommandsReceived[0]);
                    String bar = bar(percent);
                    if (bar != null) {
                        commandBars.append(bar + " " + percent + "% **" + key + "**\n");
                    }
                }
                counter[0]++;
            });
            EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
            builder.setAuthor("Command Statistics", getShard().url, getShard().bot.getAvatarUrl());
            builder.setColor(Color.GREEN);
            builder.setDescription("Command Usage\n" + commandBars.toString());
            sendEmbed(builder, channel, user);
        }
    });
    subcommands.add(new Subcommand("See how many server's I'm joining and leaving", "guilds") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
            builder.setAuthor("Guild Statistics", getShard().url, getShard().bot.getAvatarUrl());
            builder.setColor(Color.GREEN);
            int lPH = 0;
            int lPD = 0;
            int lPS = 0;
            long epochSecond = Instant.now().getEpochSecond();
            long hBar = epochSecond - (60 * 60);
            long dBar = epochSecond - (60 * 60 * 24);
            long sBar = 0;
            for (Instant i : Leave.botLeaveEvents) {
                if (i.getEpochSecond() >= hBar)
                    lPH++;
                if (i.getEpochSecond() >= dBar)
                    lPD++;
                // Always true
                if (i.getEpochSecond() >= sBar)
                    lPS++;
            }
            int jPH = 0;
            int jPD = 0;
            int jPS = 0;
            for (Instant i : Join.botJoinEvents) {
                if (i.getEpochSecond() >= hBar)
                    jPH++;
                if (i.getEpochSecond() >= dBar)
                    jPD++;
                // Always true
                if (i.getEpochSecond() >= sBar)
                    jPS++;
            }
            builder.addField("Hourly", generateGuild(jPH, lPH), false);
            builder.addField("Daily", generateGuild(jPD, lPD), false);
            builder.addField("This Session", generateGuild(jPS, lPS), false);
            builder.addField("Guilds", String.valueOf(InternalStats.collect().getGuilds()), false);
            sendEmbed(builder, channel, user);
        }
    });
}
Also used : ShardManager(tk.ardentbot.main.ShardManager) MessageUtils(tk.ardentbot.utils.discord.MessageUtils) Join(tk.ardentbot.core.events.Join) Command(tk.ardentbot.core.executor.Command) Leave(tk.ardentbot.core.events.Leave) Subcommand(tk.ardentbot.core.executor.Subcommand) HashMap(java.util.HashMap) Instant(java.time.Instant) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Shard(tk.ardentbot.main.Shard) Message(net.dv8tion.jda.core.entities.Message) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) java.awt(java.awt) Guild(net.dv8tion.jda.core.entities.Guild) User(net.dv8tion.jda.core.entities.User) MapUtils(tk.ardentbot.utils.MapUtils) Map(java.util.Map) InternalStats(tk.ardentbot.utils.discord.InternalStats) User(net.dv8tion.jda.core.entities.User) Subcommand(tk.ardentbot.core.executor.Subcommand) Message(net.dv8tion.jda.core.entities.Message) Instant(java.time.Instant) Guild(net.dv8tion.jda.core.entities.Guild) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with Subcommand

use of tk.ardentbot.core.executor.Subcommand in project Ardent by adamint.

the class Music method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Play a song by its name or url", "play") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /play <name/url> or /fancyplay <name/url> instead or type /help for to see our music " + "category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Ardent will add recommended tracks based on currently playing songs", "recommend [amt of songs]", "recommend") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /recommend <number> instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("View set music settings for the current guild", "config") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /mconfig instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("View the currently queued songs", "queue") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /queue instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Skip the currently playing song (must have queued it or have permissions)", "skip") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /skip instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Remove a song from the queue by its number", "remove [number in queue]", "remove") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /remove <queue number> instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Makes me leave the voice channel I'm currently in", "leave") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /leave instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Resumes music playback if it has been paused", "resume") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /resume instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Stops playback and clears the queue", "stop") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /stop instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Vote to skip the current song", "votetoskip") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /votetoskip instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Clear all songs from the queue", "clear") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /clear instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("View information about the currently playing song", "playing") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /playing instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Shuffle the songs in the queue", "shuffle") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /shuffle instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Pause music playback", "pause") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /pause instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Set a channel to send all music output to", "setoutput #mentionchannel", "setoutput") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /musicoutput instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("This will loop the given amount of songs, starting at the first song in the queue, for the amount" + " of times given", "loop [amount of songs starting at first in the queue] [amount of times to loop]", "loop") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /loop <songs in queue> <amount of times> instead or type /help for to see our music " + "category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Removes all the tracks of the mentioned user from the queue", "removeallfrom @User", "removeallfrom") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /removefrom @User instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Restarts the currently playing song", "restart") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /restart instead or type /help for to see our music category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("Want to know the link of that cool song you're playing now? This subcommand will display it!", "geturl [number in queue (optional)]", "geturl") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /geturl <number in queue [OPTIONAL]> instead or type /help for to see our music " + "category commands!", channel, user);
        }
    });
    subcommands.add(new Subcommand("volume [number between 1 and 100]", "Sets the volume of the music player to the specified value -" + " Staff and Patrons only!", "volume") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("Please use /volume <number [optional]> instead or type /help for to see our music category " + "commands!", channel, user);
        }
    });
}
Also used : Subcommand(tk.ardentbot.core.executor.Subcommand) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) SQLException(java.sql.SQLException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 15 with Subcommand

use of tk.ardentbot.core.executor.Subcommand in project Ardent by adamint.

the class Iam method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("See a list of setup Iams", "view", "view") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            Shard shard = GuildUtils.getShard(guild);
            String title = "Auto Roles | /iam";
            String givesYouRole = "will give you the role";
            EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
            builder.setAuthor(title, shard.url, shard.bot.getAvatarUrl());
            StringBuilder msg = new StringBuilder();
            msg.append("**" + title + "**");
            for (Pair<String, Role> autorole : getAutoRoles(guild)) {
                String roleName;
                Role role = autorole.getV();
                if (role == null)
                    roleName = "deleted-role";
                else
                    roleName = role.getName();
                msg.append("\n**" + autorole.getK() + "** " + givesYouRole + " **" + roleName + "**");
            }
            msg.append("\n\nAdd an autorole by doing /iam role and going through the setup wizard");
            builder.setDescription(msg.toString());
            sendEmbed(builder, channel, user);
        }
    });
    subcommands.add(new Subcommand("Give yourself an autorole", "role", "role") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (args.length > 2) {
                String query = message.getRawContent().replace(GuildUtils.getPrefix(guild) + args[0] + " " + args[1] + " ", "");
                boolean found = false;
                ArrayList<Pair<String, Role>> autoroles = getAutoRoles(guild);
                for (Pair<String, Role> rolePair : autoroles) {
                    if (rolePair.getK().equalsIgnoreCase(query)) {
                        found = true;
                        Role role = rolePair.getV();
                        if (role == null)
                            sendTranslatedMessage("Hmm.. there's no role for this", channel, user);
                        else {
                            boolean failure = false;
                            try {
                                guild.getController().addRolesToMember(guild.getMember(user), role).queue();
                            } catch (Exception ex) {
                                failure = true;
                                sendTranslatedMessage("Please make sure I can add roles", channel, user);
                            }
                            if (!failure)
                                sendTranslatedMessage("Successfully gave you the role {0}".replace("{0}", role.getName()), channel, user);
                        }
                    }
                }
                if (!found)
                    sendTranslatedMessage("An autorole with that name wasn't found", channel, user);
            } else
                sendTranslatedMessage("You need to include an autorole name", channel, user);
        }
    });
    subcommands.add(new Subcommand("Remove autoroles", "remove", "remove") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (UserUtils.hasManageServerOrStaff(guild.getMember(user))) {
                String query = message.getRawContent().replace(GuildUtils.getPrefix(guild) + args[0] + " " + args[1] + " ", "");
                boolean found = false;
                ArrayList<Pair<String, Role>> autoroles = getAutoRoles(guild);
                for (Pair<String, Role> rolePair : autoroles) {
                    if (rolePair.getK().equalsIgnoreCase(query)) {
                        found = true;
                        r.db("data").table("autoroles").filter(row -> row.g("name").eq(query).and(row.g("guild_id").eq(guild.getId()))).delete().run(connection);
                        sendTranslatedMessage("Deleted the autorole **{0}**".replace("{0}", rolePair.getK()), channel, user);
                    }
                }
                if (!found)
                    sendTranslatedMessage("An autorole with that name wasn't found", channel, user);
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
    subcommands.add(new Subcommand("Add an autorole", "add", "add") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            if (UserUtils.hasManageServerOrStaff(guild.getMember(user))) {
                sendTranslatedMessage("Type the name of the autorole you want to add", channel, user);
                interactiveOperation(channel, message, nameMessage -> {
                    String name = nameMessage.getContent();
                    sendTranslatedMessage("Type the name of the **discord role** you want this set to", channel, user);
                    interactiveOperation(channel, message, roleMessage -> {
                        try {
                            String role = roleMessage.getRawContent();
                            boolean found = false;
                            ArrayList<Pair<String, Role>> autoroles = getAutoRoles(guild);
                            for (Pair<String, Role> rolePair : autoroles) {
                                if (rolePair.getK().equalsIgnoreCase(name)) {
                                    found = true;
                                }
                            }
                            if (found)
                                sendTranslatedMessage("An autorole with that name is already setup", channel, user);
                            else {
                                List<Role> roleList = guild.getRolesByName(role, true);
                                if (roleList.size() > 0) {
                                    Role toAdd = roleList.get(0);
                                    r.table("autoroles").insert(r.json(gson.toJson(new AutoroleModel(guild.getId(), name, toAdd.getId())))).run(connection);
                                    sendTranslatedMessage("Successfully added autorole **{0}** which gives the role **{1}**".replace("{0}", name).replace("{1}", role), channel, user);
                                } else
                                    sendTranslatedMessage("An role with that name wasn't found", channel, user);
                            }
                        } catch (Exception e) {
                            new BotException(e);
                        }
                    });
                });
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
}
Also used : MessageUtils(tk.ardentbot.utils.discord.MessageUtils) Command(tk.ardentbot.core.executor.Command) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) UserUtils(tk.ardentbot.utils.discord.UserUtils) Subcommand(tk.ardentbot.core.executor.Subcommand) Database.r(tk.ardentbot.rethink.Database.r) HashMap(java.util.HashMap) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) ArrayList(java.util.ArrayList) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) List(java.util.List) Cursor(com.rethinkdb.net.Cursor) Database.connection(tk.ardentbot.rethink.Database.connection) AutoroleModel(tk.ardentbot.rethink.models.AutoroleModel) Pair(tk.ardentbot.utils.javaAdditions.Pair) Subcommand(tk.ardentbot.core.executor.Subcommand) ArrayList(java.util.ArrayList) BotException(tk.ardentbot.core.misc.logging.BotException) AutoroleModel(tk.ardentbot.rethink.models.AutoroleModel) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) BotException(tk.ardentbot.core.misc.logging.BotException) ArrayList(java.util.ArrayList) List(java.util.List) Shard(tk.ardentbot.main.Shard) Pair(tk.ardentbot.utils.javaAdditions.Pair)

Aggregations

Subcommand (tk.ardentbot.core.executor.Subcommand)16 Command (tk.ardentbot.core.executor.Command)9 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)8 Guild (net.dv8tion.jda.core.entities.Guild)8 Message (net.dv8tion.jda.core.entities.Message)8 MessageChannel (net.dv8tion.jda.core.entities.MessageChannel)8 User (net.dv8tion.jda.core.entities.User)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Database.connection (tk.ardentbot.rethink.Database.connection)6 Database.r (tk.ardentbot.rethink.Database.r)6 Permission (net.dv8tion.jda.core.Permission)5 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)5 BotException (tk.ardentbot.core.misc.logging.BotException)5 MessageUtils (tk.ardentbot.utils.discord.MessageUtils)5 HashMap (java.util.HashMap)4 Cursor (com.rethinkdb.net.Cursor)3 SecureRandom (java.security.SecureRandom)3 Shard (tk.ardentbot.main.Shard)3 GuildUtils (tk.ardentbot.utils.discord.GuildUtils)3