Search in sources :

Example 1 with RestrictedUserModel

use of tk.ardentbot.rethink.models.RestrictedUserModel in project Ardent by adamint.

the class Restrict method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Prevent a user from using Ardent commands", "block @User", "block") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            Member member = guild.getMember(user);
            if (member.hasPermission(Permission.MANAGE_SERVER)) {
                List<User> mentionedUsers = message.getMentionedUsers();
                if (mentionedUsers.size() > 0) {
                    for (User mentioned : mentionedUsers) {
                        Member mentionedMember = guild.getMember(mentioned);
                        if (mentionedMember.hasPermission(member.getPermissions()) || mentionedMember.hasPermission(Permission.MANAGE_SERVER) || mentionedMember.getUser().getId().equalsIgnoreCase(guild.getSelfMember().getUser().getId())) {
                            sendTranslatedMessage("You cannot modify this user because they have the same or higher permissions than " + "you.", channel, user);
                            return;
                        }
                        EntityGuild entityGuild = EntityGuild.get(guild);
                        boolean isRestricted = entityGuild.isRestricted(mentioned);
                        if (isRestricted) {
                            sendTranslatedMessage("This user has already been blocked from using commands!", channel, user);
                        } else {
                            RestrictedUser restrictedUser = new RestrictedUser(mentioned.getId(), user.getId(), guild);
                            entityGuild.addRestricted(restrictedUser);
                            r.db("data").table("restricted").insert(r.json(gson.toJson(new RestrictedUserModel(guild.getId(), mentioned.getId(), user.getId())))).run(connection);
                            sendEditedTranslation("{1} has blocked {0} from sending commands", user, channel, mentioned.getName(), user.getName());
                        }
                    }
                } else
                    sendTranslatedMessage("You need to mention one or more users", channel, user);
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
    subcommands.add(new Subcommand("Allow a blocked user to run Ardent commands again", "unblock @User", "unblock") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                List<User> mentionedUsers = message.getMentionedUsers();
                if (mentionedUsers.size() > 0) {
                    for (User mentioned : mentionedUsers) {
                        EntityGuild entityGuild = EntityGuild.get(guild);
                        boolean isRestricted = entityGuild.isRestricted(mentioned);
                        if (!isRestricted) {
                            sendEditedTranslation("{0} isn't restricted from sending commands!", user, channel, mentioned.getName());
                        } else {
                            entityGuild.removeRestricted(mentioned.getId());
                            r.db("data").table("restricted").filter(row -> row.g("user_id").eq(mentioned.getId()).and(row.g("guild_id").eq(guild.getId()))).delete().run(connection);
                            sendEditedTranslation("{0} unrestricted {1}, allowing them to use commands again", user, channel, mentioned.getName(), user.getName());
                        }
                    }
                } else
                    sendTranslatedMessage("You need to mention one or more users", channel, user);
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
    subcommands.add(new Subcommand("View all the users who have been blocked in your server", "view") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            EntityGuild entityGuild = EntityGuild.get(guild);
            ArrayList<RestrictedUser> restrictedUsers = entityGuild.getRestrictedUsers();
            String title = "Restricted users";
            String restrictedBy = "restricted by";
            StringBuilder response = new StringBuilder();
            response.append("**" + title + "**");
            if (restrictedUsers.size() == 0) {
                response.append("\nNo one");
            } else {
                restrictedUsers.forEach(restrictedUser -> response.append("\n - " + guild.getMemberById(restrictedUser.getUserId()).getUser().getName() + ", " + restrictedBy + " " + guild.getMemberById(restrictedUser.getRestrictedById()).getUser().getName()));
            }
            EmbedBuilder builder = MessageUtils.getDefaultEmbed(user);
            builder.setAuthor(title, getShard().url, guild.getSelfMember().getUser().getAvatarUrl());
            builder.setDescription(response.toString());
            sendEmbed(builder, 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) Subcommand(tk.ardentbot.core.executor.Subcommand) RestrictedUserModel(tk.ardentbot.rethink.models.RestrictedUserModel) RestrictedUser(tk.ardentbot.utils.models.RestrictedUser) Database.r(tk.ardentbot.rethink.Database.r) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) ArrayList(java.util.ArrayList) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) List(java.util.List) Permission(net.dv8tion.jda.core.Permission) Database.connection(tk.ardentbot.rethink.Database.connection) RestrictedUser(tk.ardentbot.utils.models.RestrictedUser) Subcommand(tk.ardentbot.core.executor.Subcommand) ArrayList(java.util.ArrayList) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) EntityGuild(tk.ardentbot.utils.rpg.EntityGuild) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) ArrayList(java.util.ArrayList) List(java.util.List) RestrictedUserModel(tk.ardentbot.rethink.models.RestrictedUserModel) RestrictedUser(tk.ardentbot.utils.models.RestrictedUser)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 Permission (net.dv8tion.jda.core.Permission)1 net.dv8tion.jda.core.entities (net.dv8tion.jda.core.entities)1 Command (tk.ardentbot.core.executor.Command)1 Subcommand (tk.ardentbot.core.executor.Subcommand)1 Database.connection (tk.ardentbot.rethink.Database.connection)1 Database.r (tk.ardentbot.rethink.Database.r)1 RestrictedUserModel (tk.ardentbot.rethink.models.RestrictedUserModel)1 MessageUtils (tk.ardentbot.utils.discord.MessageUtils)1 RestrictedUser (tk.ardentbot.utils.models.RestrictedUser)1 EntityGuild (tk.ardentbot.utils.rpg.EntityGuild)1