Search in sources :

Example 6 with Subcommand

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

the class Prefix method setupSubcommands.

@Override
public void setupSubcommands() {
    subcommands.add(new Subcommand("View the prefix of your server!", "view", "view") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            sendTranslatedMessage("The prefix for this server is **{0}**.".replace("{0}", GuildUtils.getPrefix(guild)), channel, user);
        }
    });
    subcommands.add(new Subcommand("Change the prefix of your server.", "change", "change") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (args.length > 2) {
                if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                    String newPrefix = message.getRawContent().replace(GuildUtils.getPrefix(guild) + args[0] + " " + "" + args[1] + " ", "").replace(" ", "");
                    if (newPrefix.length() == message.getRawContent().length()) {
                        newPrefix = message.getRawContent().replace("/" + args[0] + " " + args[1] + " ", "");
                    }
                    if (!newPrefix.contains(" ") && !newPrefix.contains("$")) {
                        GuildUtils.updatePrefix(newPrefix, guild);
                        r.db("data").table("guilds").filter(row -> row.g("guild_id").eq(guild.getId())).update(r.hashMap("prefix", newPrefix)).run(connection);
                        sendTranslatedMessage("Successfully updated the prefix,  {0}!".replace("{0}", newPrefix), channel, user);
                    } else
                        sendTranslatedMessage("Your supplied prefix contained invalid characters!", channel, user);
                } else {
                    sendTranslatedMessage("You need ```Manage Server``` permissions.", channel, user);
                }
            } else {
                sendTranslatedMessage("You must include a prefix as your third argument!", channel, user);
            }
        }
    });
}
Also used : Guild(net.dv8tion.jda.core.entities.Guild) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) Permission(net.dv8tion.jda.core.Permission) User(net.dv8tion.jda.core.entities.User) Command(tk.ardentbot.core.executor.Command) Database.connection(tk.ardentbot.rethink.Database.connection) Subcommand(tk.ardentbot.core.executor.Subcommand) Database.r(tk.ardentbot.rethink.Database.r) 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) MessageChannel(net.dv8tion.jda.core.entities.MessageChannel) Message(net.dv8tion.jda.core.entities.Message) Guild(net.dv8tion.jda.core.entities.Guild)

Example 7 with Subcommand

use of tk.ardentbot.core.executor.Subcommand 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)

Example 8 with Subcommand

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

the class DefaultRole method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("View the current default role in this server", "view", "view") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            Role role = getDefaultRole(guild);
            if (role == null) {
                sendTranslatedMessage("There's no default role set up in this server!", channel, user);
            } else {
                String reply = "The set default role is {0}".replace("{0}", role.getName());
                sendTranslatedMessage(reply, channel, user);
            }
        }
    });
    subcommands.add(new Subcommand("Remove the default role", "remove", "remove") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) {
            Role role = getDefaultRole(guild);
            if (role == null) {
                sendTranslatedMessage("There's no default role set up in this server!", channel, user);
            } else {
                if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                    removeDefaultRole(guild);
                    sendTranslatedMessage("Removed the set default role!", channel, user);
                } else
                    sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
            }
        }
    });
    subcommands.add(new Subcommand("Set the default role", "set", "set") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (GuildUtils.hasManageServerPermission(guild.getMember(user))) {
                String roleName = message.getRawContent().replace(GuildUtils.getPrefix(guild) + args[0] + " " + args[1] + " ", "");
                List<Role> roles = guild.getRolesByName(roleName, true);
                if (roles.size() == 0) {
                    sendTranslatedMessage("You need to type a role name!", channel, user);
                } else {
                    Role role = roles.get(0);
                    setDefaultRole(role, guild);
                    sendTranslatedMessage("Successfully set the default role as " + role.getName() + "!", channel, user);
                }
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
}
Also used : Subcommand(tk.ardentbot.core.executor.Subcommand) List(java.util.List)

Example 9 with Subcommand

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

the class NSFW method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Set whether people can send NSFW channels in all channels", "alloweverywhere [true/false]", "alloweverywhere") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                if (args.length == 3) {
                    try {
                        boolean allowEverywhere = Boolean.parseBoolean(args[2]);
                        getSettings(guild);
                        if (allowEverywhere)
                            sendTranslatedMessage("You can now use NSFW commands in all channels", channel, user);
                        else
                            sendTranslatedMessage("You can now use NSFW commands only in specified channels", channel, user);
                        r.table("nsfw_settings").get(guild.getId()).update(r.hashMap("global", allowEverywhere)).run(connection);
                    } catch (Exception e) {
                        sendTranslatedMessage("You need to specify true or false", channel, user);
                    }
                } else
                    sendTranslatedMessage("Invalid arguments ¯\\_(ツ)_/¯", channel, user);
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
    subcommands.add(new Subcommand("Set whether you need a role called NSFW to send NSFW commands", "neednsfwrole [true/false]", "neednsfwrole") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                if (args.length == 3) {
                    try {
                        boolean needs = Boolean.parseBoolean(args[2]);
                        getSettings(guild);
                        if (needs)
                            sendTranslatedMessage("You now need the NSFW role to send NSFW commands", channel, user);
                        else
                            sendTranslatedMessage("Anyone can now send NSFW commands", channel, user);
                        r.table("nsfw_settings").get(guild.getId()).update(r.hashMap("needNsfwRole", needs)).run(connection);
                    } catch (Exception e) {
                        sendTranslatedMessage("You need to specify true or false", channel, user);
                    }
                } else
                    sendTranslatedMessage("Invalid arguments ¯\\_(ツ)_/¯", channel, user);
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
    subcommands.add(new Subcommand("View the current NSFW settings for your server", "settings", "settings") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            String nsfwSettings = "NSFW Settings";
            NSFWSettings settings = getSettings(guild);
            EmbedBuilder embedBuilder = MessageUtils.getDefaultEmbed(user);
            embedBuilder.setAuthor(nsfwSettings, getShard().url, getShard().bot.getAvatarUrl());
            StringBuilder description = new StringBuilder();
            description.append("**" + nsfwSettings + "**");
            description.append("\n\nAble to use NSFW commands in all channels: " + settings.isGlobal());
            description.append("\n\nNeed the NSFW role: " + settings.isNeedNsfwRole());
            if (!settings.isGlobal()) {
                description.append("\n\nCan only use these command in discord NSFFW channels");
            }
            embedBuilder.setDescription(description.toString());
            sendEmbed(embedBuilder, channel, user);
        }
    });
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) NSFWSettings(tk.ardentbot.rethink.models.NSFWSettings) Subcommand(tk.ardentbot.core.executor.Subcommand)

Example 10 with Subcommand

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

the class Trivia method setupSubcommands.

@Override
public void setupSubcommands() throws Exception {
    subcommands.add(new Subcommand("Start a new trivia game", "start", "start") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            for (TriviaGame triviaGame : gamesInSession) {
                if (triviaGame.getGuildId().equalsIgnoreCase(guild.getId())) {
                    sendTranslatedMessage("There's already a game in session in this server!", channel, user);
                    return;
                }
            }
            if (gamesSettingUp.contains(guild.getId())) {
                sendTranslatedMessage("There's already a game in session in this server!", channel, user);
                return;
            }
            gamesSettingUp.add(guild.getId());
            sendTranslatedMessage("Do you want to play this solo? Type `yes` if so, or `no` if not", channel, user);
            interactiveOperation(channel, message, (soloMessage) -> {
                String content = soloMessage.getContent();
                boolean solo;
                solo = content.equalsIgnoreCase("yes");
                TriviaGame currentGame = new TriviaGame(user, solo, (TextChannel) channel, 15);
                gamesInSession.add(currentGame);
                sendTranslatedMessage("The game is starting! Type your answers in this channel. You have **15** seconds to answer " + "each question.", channel, user);
                commenceRounds(guild, (TextChannel) channel, user, currentGame);
                gamesSettingUp.remove(guild.getId());
            });
        }
    });
    subcommands.add(new Subcommand("Stop a currently active trivia game", "stop", "stop") {

        @Override
        public void onCall(Guild guild, MessageChannel channel, User user, Message message, String[] args) throws Exception {
            if (guild.getMember(user).hasPermission(Permission.MANAGE_SERVER)) {
                if (gamesInSession.stream().filter(game -> game.getGuildId().equals(guild.getId())).count() > 0 || gamesSettingUp.contains(guild.getId())) {
                    gamesSettingUp.remove(guild.getId());
                    gamesInSession.removeIf(g -> g.getGuildId().equals(guild.getId()));
                    sendTranslatedMessage("Stopped the trivia game in session.", channel, user);
                } else {
                    sendTranslatedMessage("There isn't a trivia game running!", channel, user);
                }
            } else
                sendTranslatedMessage("You need the Manage Server permission to use this command", channel, user);
        }
    });
}
Also used : Command(tk.ardentbot.core.executor.Command) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) Subcommand(tk.ardentbot.core.executor.Subcommand) BotException(tk.ardentbot.core.misc.logging.BotException) Shard(tk.ardentbot.main.Shard) TriviaQuestion(tk.ardentbot.utils.models.TriviaQuestion) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) TimeUnit(java.util.concurrent.TimeUnit) GuildUtils(tk.ardentbot.utils.discord.GuildUtils) Permission(net.dv8tion.jda.core.Permission) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TriviaGame(tk.ardentbot.utils.rpg.TriviaGame) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Subcommand(tk.ardentbot.core.executor.Subcommand) TriviaGame(tk.ardentbot.utils.rpg.TriviaGame) BotException(tk.ardentbot.core.misc.logging.BotException)

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