Search in sources :

Example 21 with BotException

use of tk.ardentbot.core.misc.logging.BotException 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)

Example 22 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class Prune method noArgs.

@Override
public void noArgs(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
    if (args.length == 1) {
        sendTranslatedMessage("Prune\n" + "Prune users who have been inactive a certain amount of days\n" + "\n" + "Syntax: {0}prune [amount of days]\n" + "Example usage: {0}prune 4".replace("{0}", GuildUtils.getPrefix(guild) + args[0]), channel, user);
    } else {
        if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
            try {
                int day = Integer.parseInt(args[1]);
                if (day <= 0) {
                    sendTranslatedMessage("Your number has to be at least 1!", channel, user);
                    return;
                }
                guild.getController().prune(day).queue(integer -> {
                    try {
                        sendTranslatedMessage("Successfully pruned{0} users.".replace("{0}", String.valueOf(integer)), channel, user);
                    } catch (Exception e) {
                        new BotException(e);
                    }
                });
            } catch (NumberFormatException ex) {
                sendTranslatedMessage("You did not supply a whole number.", channel, user);
            } catch (PermissionException ex) {
                sendTranslatedMessage("I don't have permissions to kick users", channel, user);
            }
        } else
            sendTranslatedMessage("You don't have permission to kick users.", channel, user);
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) BotException(tk.ardentbot.core.misc.logging.BotException) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) BotException(tk.ardentbot.core.misc.logging.BotException)

Example 23 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class Automessage method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Prompt the automessage setup", "setup", "setup") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            sendTranslatedMessage("Type **join** to set this server's join message, **leave** to set this server's leave message, or " + "**channel** to set the channel I'll send join & leave messages to", channel, user);
            interactiveOperation(channel, message, selectType -> {
                String type;
                String content = selectType.getContent();
                if (content.equalsIgnoreCase("join")) {
                    type = "join";
                    sendTranslatedMessage("Type the message you want to send below", channel, user);
                } else if (content.equalsIgnoreCase("leave")) {
                    type = "leave";
                    sendTranslatedMessage("Type the message you want to send below", channel, user);
                } else if (content.equalsIgnoreCase("channel")) {
                    type = "channel";
                    sendTranslatedMessage("Mention a channel to send the automessages to", channel, user);
                } else {
                    sendTranslatedMessage("You specified an invalid category type! Please redo setup", channel, user);
                    return;
                }
                longInteractiveOperation(channel, message, 60, inputMessage -> {
                    try {
                        if (type.equals("channel")) {
                            List<TextChannel> mentionedChannels = inputMessage.getMentionedChannels();
                            if (mentionedChannels.size() > 0) {
                                TextChannel mentioned = mentionedChannels.get(0);
                                set(guild, mentioned.getId(), 0);
                                sendTranslatedMessage("Successfully set {0} as the automessage sending channel".replace("{0}", mentioned.getAsMention()), channel, user);
                            } else
                                sendTranslatedMessage("You needed to mention a channel!", channel, user);
                        } else {
                            String toPut = inputMessage.getRawContent();
                            if (type.equals("join")) {
                                set(guild, toPut, 1);
                                sendTranslatedMessage("Successfully set {0} as the join message".replace("{0}", toPut), channel, user);
                            } else {
                                set(guild, toPut, 2);
                                sendTranslatedMessage("Successfully set {0} as the leave message".replace("{0}", toPut), channel, user);
                            }
                            String textChannel = getMessagesAndChannel(guild).getA();
                            if (textChannel == null || guild.getTextChannelById(textChannel) == null) {
                                sendTranslatedMessage("Warning: you need a set a channel to send messages to, or else nothing " + "will happen", channel, user);
                            }
                        }
                    } catch (Exception e) {
                        new BotException(e);
                    }
                });
            });
        }
    });
    subcommands.add(new Subcommand("View special arguments you can add to your messages", "arguments") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            sendTranslatedMessage("The available arguments are listed here:\n" + "{user}: mentions the user involved\n" + "{servername}: replaced with the name of your server\n" + "{amtusers}: replaced with the amount of users in your server", channel, user);
        }
    });
    subcommands.add(new Subcommand("Remove a setting", "remove", "remove") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                if (args.length == 2) {
                    sendTranslatedMessage("Type **/automessage remove join** to remove this server's join message, **/automessage " + "remove leave** to remove this server's " + "leave message, or **/automessage remove channel** to remove the set channel", channel, user);
                } else {
                    String type = args[2];
                    Triplet<String, String, String> settings = getMessagesAndChannel(guild);
                    if (type.equalsIgnoreCase("channel")) {
                        if (settings.getA() == null) {
                            sendTranslatedMessage("There isn't a channel set!", channel, user);
                        } else {
                            remove(guild, 0);
                            sendTranslatedMessage("Successfully removed the automessage channel", channel, user);
                        }
                    } else if (type.equalsIgnoreCase("join")) {
                        if (settings.getB() == null) {
                            sendTranslatedMessage("There's no welcome message set!", channel, user);
                        } else {
                            remove(guild, 1);
                            sendTranslatedMessage("Successfully removed the welcome message", channel, user);
                        }
                    } else if (type.equalsIgnoreCase("leave")) {
                        if (settings.getC() == null) {
                            sendTranslatedMessage("There's no goodbye message set!", channel, user);
                        } else {
                            remove(guild, 2);
                            sendTranslatedMessage("The goodbye message has been successfully removed", channel, user);
                        }
                    } else
                        sendTranslatedMessage("Cancelling removal, you specified an invalid category", channel, user);
                }
            } else
                sendTranslatedMessage("You need the `Manage Server` permission to use this command", channel, user);
        }
    });
    subcommands.add(new Subcommand("View the current automessage settings", "view", "view", "settings") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            Triplet<String, String, String> messages = getMessagesAndChannel(guild);
            StringBuilder sb = new StringBuilder();
            sb.append("**Automessage Settings**\n=============\n");
            if (messages.getA() == null)
                sb.append("There isn't a set automessage channel" + "\n\n");
            else {
                TextChannel textChannel = guild.getTextChannelById(messages.getA());
                if (textChannel != null) {
                    sb.append("The set channel for messages is {0}".replace("{0}", textChannel.getName() + "\n\n"));
                } else
                    sb.append("There isn't a set automessage channel" + "\n\n");
            }
            if (messages.getB() != null)
                sb.append("The set join message is {0}".replace("{0}", messages.getB()) + "\n\n");
            else
                sb.append("There isn't a set join message" + "\n\n");
            if (messages.getC() != null)
                sb.append("The set join message is {0}".replace("{0}", messages.getC()) + "\n");
            else
                sb.append("There isn't a set goodbye message");
            sendTranslatedMessage(sb.toString(), channel, user);
        }
    });
}
Also used : AutomessageModel(tk.ardentbot.rethink.models.AutomessageModel) List(java.util.List) Cursor(com.rethinkdb.net.Cursor) Permission(net.dv8tion.jda.core.Permission) Command(tk.ardentbot.core.executor.Command) Database.connection(tk.ardentbot.rethink.Database.connection) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Subcommand(tk.ardentbot.core.executor.Subcommand) Database.r(tk.ardentbot.rethink.Database.r) HashMap(java.util.HashMap) Triplet(tk.ardentbot.utils.javaAdditions.Triplet) BotException(tk.ardentbot.core.misc.logging.BotException) Subcommand(tk.ardentbot.core.executor.Subcommand) Triplet(tk.ardentbot.utils.javaAdditions.Triplet) BotException(tk.ardentbot.core.misc.logging.BotException) BotException(tk.ardentbot.core.misc.logging.BotException) List(java.util.List)

Example 24 with BotException

use of tk.ardentbot.core.misc.logging.BotException in project Ardent by adamint.

the class MuteDaemon method run.

@Override
public void run() {
    for (Shard shard : ShardManager.getShards()) {
        HashMap<String, HashMap<String, Long>> mutes = shard.botMuteData.getMutes();
        for (String guildID : mutes.keySet()) {
            Guild guild = shard.jda.getGuildById(guildID);
            for (String userID : mutes.get(guildID).keySet()) {
                Member member = guild.getMember(shard.jda.getUserById(userID));
                if (!shard.botMuteData.isMuted(member) && shard.botMuteData.wasMute(member)) {
                    shard.botMuteData.unmute(member);
                    member.getUser().openPrivateChannel().queue(privateChannel -> {
                        try {
                            privateChannel.sendMessage("You can now speak in {0}".replace("{0}", guild.getName())).queue();
                        } catch (Exception e) {
                            new BotException(e);
                        }
                    });
                }
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) Guild(net.dv8tion.jda.core.entities.Guild) Member(net.dv8tion.jda.core.entities.Member) BotException(tk.ardentbot.core.misc.logging.BotException)

Aggregations

BotException (tk.ardentbot.core.misc.logging.BotException)24 Shard (tk.ardentbot.main.Shard)10 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)8 List (java.util.List)6 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Command (tk.ardentbot.core.executor.Command)5 Database.connection (tk.ardentbot.rethink.Database.connection)5 Database.r (tk.ardentbot.rethink.Database.r)5 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)4 Permission (net.dv8tion.jda.core.Permission)4 Subcommand (tk.ardentbot.core.executor.Subcommand)4 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)3 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)3 Guild (net.dv8tion.jda.core.entities.Guild)3 GuildUtils (tk.ardentbot.utils.discord.GuildUtils)3 UserUtils (tk.ardentbot.utils.discord.UserUtils)3 Cursor (com.rethinkdb.net.Cursor)2 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)2