Search in sources :

Example 26 with IRole

use of sx.blah.discord.handle.obj.IRole in project DisCal-Discord-Bot by NovaFox161.

the class DashboardHandler method handleSettingsUpdate.

public static String handleSettingsUpdate(Request request, Response response) {
    try {
        if (request.queryParams().contains("bot-nick")) {
            // Update bot nickname...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setBotNick(request.queryParams("bot-nick"));
                IGuild guild = Main.client.getGuildByID(Long.valueOf(g.getId()));
                guild.setUserNickname(Main.client.getOurUser(), g.getBotNick());
            }
        } else if (request.queryParams().contains("prefix")) {
            // Update prefix...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                g.getSettings().setPrefix(request.queryParams("prefix"));
                DatabaseManager.getManager().updateSettings(g.getSettings());
            }
        } else if (request.queryParams().contains("lang")) {
            // Update lang...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                g.getSettings().setLang(request.queryParams("lang"));
                DatabaseManager.getManager().updateSettings(g.getSettings());
            }
        } else if (request.queryParams().contains("con-role")) {
            // Update control role...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                IGuild guild = Main.client.getGuildByID(Long.valueOf(g.getId()));
                IRole role = guild.getRoleByID(Long.valueOf(request.queryParams("con-role")));
                if (role.isEveryoneRole()) {
                    g.getSettings().setControlRole("everyone");
                } else {
                    g.getSettings().setControlRole(role.getStringID());
                }
                DatabaseManager.getManager().updateSettings(g.getSettings());
                // Update role list to display properly...
                g.getRoles().clear();
                for (IRole r : guild.getRoles()) {
                    g.getRoles().add(new WebRole().fromRole(r, g.getSettings()));
                }
            }
        } else if (request.queryParams().contains("discal-channel")) {
            // Update control role...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isDiscalRole()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                IGuild guild = Main.client.getGuildByID(Long.valueOf(g.getId()));
                if (request.queryParams("discal-channel").equalsIgnoreCase("0")) {
                    // All channels
                    g.getSettings().setDiscalChannel("all");
                } else {
                    g.getSettings().setDiscalChannel(request.queryParams("discal-channel"));
                }
                DatabaseManager.getManager().updateSettings(g.getSettings());
                // Update channel list to display properly...
                g.getChannels().clear();
                WebChannel all = new WebChannel();
                all.setId(0);
                all.setName("All Channels");
                all.setDiscalChannel(g.getSettings().getDiscalChannel().equalsIgnoreCase("all"));
                g.getChannels().add(all);
                for (IChannel c : guild.getChannels()) {
                    g.getChannels().add(new WebChannel().fromChannel(c, g.getSettings()));
                }
            }
        } else if (request.queryParams().contains("branding")) {
            // Update bot nickname...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            if (g.isManageServer()) {
                g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
                if (g.getSettings().isPatronGuild()) {
                    if (request.queryParams().contains("value")) {
                        g.getSettings().setBranded(true);
                    } else {
                        g.getSettings().setBranded(false);
                    }
                    DatabaseManager.getManager().updateSettings(g.getSettings());
                }
            }
        } else if (request.queryParams().contains("simple-ann")) {
            // Update simple announcements...
            Map m = DiscordAccountHandler.getHandler().getAccount(request.session().id());
            WebGuild g = (WebGuild) m.get("selected");
            // Guess this one never checked for perms so...
            g.setSettings(DatabaseManager.getManager().getSettings(Long.valueOf(g.getId())));
            if (request.queryParams().contains("value")) {
                g.getSettings().setSimpleAnnouncements(true);
            } else {
                g.getSettings().setSimpleAnnouncements(false);
            }
            DatabaseManager.getManager().updateSettings(g.getSettings());
            response.redirect("/dashboard/guild/announcements", 301);
            return response.body();
        }
        // Finally redirect back to the dashboard
        response.redirect("/dashboard/guild", 301);
    } catch (Exception e) {
        Logger.getLogger().exception(null, "[WEB] Settings update failed!", e, DashboardHandler.class, true);
        halt(500, "Internal Server Exception");
    }
    return response.body();
}
Also used : WebChannel(com.cloudcraftgaming.discal.api.object.web.WebChannel) IChannel(sx.blah.discord.handle.obj.IChannel) IRole(sx.blah.discord.handle.obj.IRole) WebRole(com.cloudcraftgaming.discal.api.object.web.WebRole) WebGuild(com.cloudcraftgaming.discal.api.object.web.WebGuild) IGuild(sx.blah.discord.handle.obj.IGuild) HashMap(java.util.HashMap) Map(java.util.Map) JSONException(org.json.JSONException)

Example 27 with IRole

use of sx.blah.discord.handle.obj.IRole in project DiscordSailv2 by Vaerys-Dawn.

the class TopUserForRole method execute.

@Override
public String execute(String args, CommandObject command) {
    // init index value
    int index = 1;
    // try to get role.
    IRole role = GuildHandler.getRoleFromName(args, command.guild.get());
    if (role == null) {
        try {
            // if role get fails, try again, but this time assume the first "word" is the rank the user wants to get.
            index = Integer.parseInt(new SplitFirstObject(args).getFirstWord());
        } catch (NumberFormatException e) {
            // not a valid number, can't find role, bork.
            return "> Invalid Role";
        }
        // remove index from string, try to get role again.
        args = new SplitFirstObject(args).getRest();
        role = GuildHandler.getRoleFromName(args, command.guild.get());
        if (role == null)
            return "> Invalid Role.";
    }
    IMessage working = RequestHandler.sendMessage("`Working...`", command.channel.get()).get();
    // populate list with users with role defined.
    List<Long> userIDs = command.guild.get().getUsersByRole(role).stream().map(IUser::getLongID).collect(Collectors.toList());
    if (userIDs.isEmpty()) {
        RequestHandler.deleteMessage(working);
        return "> Could not find any users with that role!";
    }
    userIDs.removeIf(f -> PixelHandler.rank(command.guild.users, command.guild.get(), f) == -1);
    userIDs.sort((o1, o2) -> {
        long rank1 = PixelHandler.rank(command.guild.users, command.guild.get(), o1);
        long rank2 = PixelHandler.rank(command.guild.users, command.guild.get(), o2);
        return Long.compare(rank1, rank2);
    });
    if (userIDs.size() == 0)
        return "> Could not find any ranked users with that role.";
    if (index > userIDs.size()) {
        RequestHandler.deleteMessage(working);
        return "> There's only " + userIDs.size() + (userIDs.size() == 1 ? " user" : " users") + " with that role.";
    }
    if (index == 1) {
        // show an embed with the top (at most) 5 users instead.
        RequestHandler.deleteMessage(working);
        getEmbed(command, role, userIDs);
        return null;
    }
    ProfileObject topUserProfile = command.guild.users.getUserByID(userIDs.get(index - 1));
    UserObject topUser = topUserProfile.getUser(command.guild);
    NumberFormat nf = NumberFormat.getInstance();
    RequestHandler.deleteMessage(working);
    return "> @" + topUser.username + ", **Pixels:** " + nf.format(topUserProfile.getXP()) + ", **Level:** " + topUserProfile.getCurrentLevel() + ", **User ID:** " + topUser.longID;
}
Also used : IRole(sx.blah.discord.handle.obj.IRole) IMessage(sx.blah.discord.handle.obj.IMessage) UserObject(com.github.vaerys.masterobjects.UserObject) SplitFirstObject(com.github.vaerys.objects.SplitFirstObject) ProfileObject(com.github.vaerys.objects.ProfileObject) NumberFormat(java.text.NumberFormat)

Example 28 with IRole

use of sx.blah.discord.handle.obj.IRole in project DiscordSailv2 by Vaerys-Dawn.

the class CosmeticRoles method execute.

@Override
public String execute(String args, CommandObject command) {
    if (args == null || args.isEmpty()) {
        return get(ListRoles.class).execute(args, command);
    }
    if (EDIT_ROLES.isSubCommand(command)) {
        boolean isAdding = args.split(" ")[0].equals("+");
        // test the permissions of the user to make sure they can modify the role list.
        IRole role = null;
        String subArgs = EDIT_ROLES.getArgs(command);
        try {
            role = command.guild.getRoleByID(Utility.stringLong(subArgs));
        } catch (NumberFormatException e) {
        // move on.
        }
        if (role == null)
            role = GuildHandler.getRoleFromName(subArgs, command.guild.get());
        if (role == null)
            return "> **" + subArgs + "** is not a valid Role Name.";
        // tests to see if the bot is allowed to mess with a role.
        if (!Utility.testUserHierarchy(command.client.bot.get(), role, command.guild.get())) {
            return "> I do not have permission to modify the **" + role.getName() + "** role.";
        }
        // test the user's hierarchy to make sure that the are allowed to mess with that role.
        if (Utility.testUserHierarchy(command.user.get(), role, command.guild.get())) {
            // do if modifier is true
            if (isAdding) {
                // check for the role and add if its not a cosmetic role.
                if (command.guild.config.isRoleCosmetic(role.getLongID())) {
                    return "> The **" + role.getName() + "** role is already listed as a cosmetic role.";
                } else {
                    command.guild.config.getCosmeticRoleIDs().add(role.getLongID());
                    return "> The **" + role.getName() + "** role was added to the cosmetic role list.";
                }
            // do if modifier is false
            } else {
                // check for the role and remove if it is a cosmetic role.
                if (command.guild.config.isRoleCosmetic(role.getLongID())) {
                    Iterator iterator = command.guild.config.getCosmeticRoleIDs().listIterator();
                    while (iterator.hasNext()) {
                        long id = (long) iterator.next();
                        if (role.getLongID() == id) {
                            iterator.remove();
                        }
                    }
                    return "> The **" + role.getName() + "** role was removed from the cosmetic role list.";
                } else {
                    return "> The **" + role.getName() + "** role is not listed as a cosmetic role.";
                }
            }
        } else {
            return "> You do not have permission to modify the **" + role.getName() + "** role.";
        }
    // do user role modification
    } else {
        // check to make sure that the user isn't including the args brackets or the /remove at the end;
        if (command.guild.config.getCosmeticRoleIDs().size() == 0)
            return "> No Cosmetic roles are set up right now. Come back later.";
        if (args.matches("[(|\\[].*[)|\\]]")) {
            return Constants.ERROR_BRACKETS + "\n" + Utility.getCommandInfo(this, command);
        }
        if (args.matches(".*/remove")) {
            return "> Did you mean `" + command.guild.config.getPrefixCommand() + names()[0] + " " + args.replaceAll("(?i)/remove", "") + "`?";
        }
        List<IRole> userRoles = command.user.roles;
        String response;
        // check if role is valid
        IRole role;
        role = GuildHandler.getRoleFromName(args, command.guild.get());
        if (role == null && args.length() > 3) {
            role = GuildHandler.getRoleFromName(args, command.guild.get(), true);
        }
        if (role == null && !args.equalsIgnoreCase("remove")) {
            RequestHandler.sendEmbedMessage("> **" + args + "** is not a valid Role Name.", ListRoles.getList(command), command.channel.get());
            return null;
        // if args = remove. remove the user's cosmetic role
        } else if (args.equalsIgnoreCase("remove")) {
            userRoles = userRoles.stream().filter(r -> !command.guild.config.isRoleCosmetic(r.getLongID())).collect(Collectors.toList());
            if (command.user.getCosmeticRoles(command).size() == 0)
                return "> You don't have a role to remove...";
            else
                response = "> You have had your cosmetic role removed.";
        } else {
            // check if role is cosmetic
            if (command.guild.config.isRoleCosmetic(role.getLongID())) {
                // check to see if roles are toggles
                if (command.guild.config.roleIsToggle) {
                    // if user has role, remove it.
                    if (userRoles.contains(role)) {
                        userRoles.remove(role);
                        response = "> You have had the **" + role.getName() + "** role removed.";
                    // else add that role.
                    } else {
                        userRoles.add(role);
                        response = "> You have been granted the **" + role.getName() + "** role.";
                    }
                // if roles arent toggles run this.
                } else {
                    // if they already have that role
                    if (userRoles.contains(role)) {
                        return "> You already have the **" + role.getName() + "** role.";
                    } else {
                        // remove all cosmetic role and add the new one
                        userRoles = userRoles.stream().filter(r -> !command.guild.config.isRoleCosmetic(r.getLongID())).collect(Collectors.toList());
                        userRoles.add(role);
                        response = "> You have selected the cosmetic role: **" + role.getName() + "**.";
                    }
                }
            } else {
                RequestHandler.sendEmbedMessage("> **" + args + "** is not a valid cosmetic role.", ListRoles.getList(command), command.channel.get());
                return null;
            }
        }
        // push the changes to the user's roles.
        if (RequestHandler.roleManagement(command.user.get(), command.guild.get(), userRoles).get()) {
            return response;
        } else {
            return Constants.ERROR_UPDATING_ROLE;
        }
    }
}
Also used : CommandObject(com.github.vaerys.commands.CommandObject) RequestHandler(com.github.vaerys.handlers.RequestHandler) Iterator(java.util.Iterator) IRole(sx.blah.discord.handle.obj.IRole) ChannelSetting(com.github.vaerys.enums.ChannelSetting) SAILType(com.github.vaerys.enums.SAILType) GuildHandler(com.github.vaerys.handlers.GuildHandler) Constants(com.github.vaerys.main.Constants) Collectors(java.util.stream.Collectors) SubCommandObject(com.github.vaerys.objects.SubCommandObject) List(java.util.List) Permissions(sx.blah.discord.handle.obj.Permissions) Command(com.github.vaerys.templates.Command) Utility(com.github.vaerys.main.Utility) IRole(sx.blah.discord.handle.obj.IRole) Iterator(java.util.Iterator)

Example 29 with IRole

use of sx.blah.discord.handle.obj.IRole in project DiscordSailv2 by Vaerys-Dawn.

the class ModifierRoles method execute.

@Override
public String execute(String args, CommandObject command) {
    if (args == null || args.isEmpty()) {
        return get(ListModifs.class).execute(args, command);
    }
    if (EDIT_ROLES.isSubCommand(command)) {
        boolean isAdding = args.split(" ")[0].equals("+");
        // test the permissions of the user to make sure they can modify the role list.
        IRole role = null;
        String subArgs = EDIT_ROLES.getArgs(command);
        try {
            role = command.guild.getRoleByID(Utility.stringLong(subArgs));
        } catch (NumberFormatException e) {
        // move on.
        }
        if (role == null)
            role = GuildHandler.getRoleFromName(subArgs, command.guild.get());
        if (role == null)
            return "> **" + subArgs + "** is not a valid Role Name.";
        // tests to see if the bot is allowed to mess with a role.
        if (!Utility.testUserHierarchy(command.client.bot.get(), role, command.guild.get())) {
            return "> I do not have permission to modify the **" + role.getName() + "** role.";
        }
        // test the user's hierarchy to make sure that the are allowed to mess with that role.
        if (Utility.testUserHierarchy(command.user.get(), role, command.guild.get())) {
            // do if modifier is true
            if (isAdding) {
                // check for the role and add if its not a Modifier role.
                if (command.guild.config.isRoleModifier(role.getLongID())) {
                    return "> The **" + role.getName() + "** role is already listed as a modifier role.";
                } else {
                    command.guild.config.getModifierRoleIDs().add(role.getLongID());
                    return "> The **" + role.getName() + "** role was added to the modifier role list.";
                }
            // do if modifier is false
            } else {
                // check for the role and remove if it is a Modifier role.
                if (command.guild.config.isRoleModifier(role.getLongID())) {
                    Iterator iterator = command.guild.config.getModifierRoleIDs().listIterator();
                    while (iterator.hasNext()) {
                        long id = (long) iterator.next();
                        if (role.getLongID() == id) {
                            iterator.remove();
                        }
                    }
                    return "> The **" + role.getName() + "** role was removed from the modifier role list.";
                } else {
                    return "> The **" + role.getName() + "** role is not listed as a modifier role.";
                }
            }
        } else {
            return "> You do not have permission to modify the **" + role.getName() + "** role.";
        }
    // do user role modification
    } else {
        if (command.guild.config.getModifierRoleIDs().size() == 0)
            return "> No Modifier roles are set up right now. Come back later.";
        IRole role;
        role = GuildHandler.getRoleFromName(args, command.guild.get());
        if (role == null && args.length() > 3) {
            role = GuildHandler.getRoleFromName(args, command.guild.get(), true);
        }
        List<IRole> userRoles = command.user.roles;
        String response;
        if (role == null) {
            RequestHandler.sendEmbedMessage("> **" + args + "** is not a valid Role Name.", ListModifs.getList(command), command.channel.get());
            return null;
        } else {
            if (command.guild.config.isRoleModifier(role.getLongID())) {
                // if user has role remove it
                if (userRoles.contains(role)) {
                    userRoles.remove(role);
                    response = "> You have had the **" + role.getName() + "** role removed.";
                // else add it
                } else {
                    userRoles.add(role);
                    response = "> You have been granted the **" + role.getName() + "** role.";
                }
                // push changes
                if (RequestHandler.roleManagement(command.user.get(), command.guild.get(), userRoles).get()) {
                    return response;
                } else {
                    return Constants.ERROR_UPDATING_ROLE;
                }
            } else {
                RequestHandler.sendEmbedMessage("> The **" + role.getName() + "** role is not listed as a modifier role.", ListModifs.getList(command), command.channel.get());
                return null;
            }
        }
    }
}
Also used : IRole(sx.blah.discord.handle.obj.IRole) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator)

Example 30 with IRole

use of sx.blah.discord.handle.obj.IRole in project DiscordSailv2 by Vaerys-Dawn.

the class ModulePixels method stats.

@Override
public String stats(CommandObject command) {
    boolean hasManageServer = GuildHandler.testForPerms(command, Permissions.MANAGE_SERVER);
    StringBuilder builder = new StringBuilder();
    builder.append("**Pixels Per Message: ** " + command.guild.config.xpRate);
    builder.append("\n**Pixel Modifier:** " + command.guild.config.xpModifier);
    if (hasManageServer) {
        IRole topTen = command.guild.getRoleByID(command.guild.config.topTenRoleID);
        IRole xpDenied = command.guild.getRoleByID(command.guild.config.xpDeniedRoleID);
        if (topTen != null)
            builder.append("\n**Top Ten Role:** " + topTen.getName());
        if (xpDenied != null)
            builder.append("\n**Xp Denied Role:** " + xpDenied.getName());
    }
    if (command.guild.config.getRewardRoles().size() != 0) {
        builder.append("\n\n**[REWARD ROLES]**");
        for (RewardRoleObject r : command.guild.config.getRewardRoles()) {
            IRole role = command.guild.getRoleByID(r.getRoleID());
            if (role != null) {
                builder.append("\n**" + role.getName() + "** - Lvl " + r.getLevel());
            }
        }
    }
    return builder.toString();
}
Also used : RewardRoleObject(com.github.vaerys.objects.RewardRoleObject) IRole(sx.blah.discord.handle.obj.IRole)

Aggregations

IRole (sx.blah.discord.handle.obj.IRole)39 IUser (sx.blah.discord.handle.obj.IUser)8 IChannel (sx.blah.discord.handle.obj.IChannel)7 ArrayList (java.util.ArrayList)6 UserObject (com.github.vaerys.masterobjects.UserObject)5 SplitFirstObject (com.github.vaerys.objects.SplitFirstObject)5 XEmbedBuilder (com.github.vaerys.objects.XEmbedBuilder)5 IGuild (sx.blah.discord.handle.obj.IGuild)5 CommandObject (com.github.vaerys.commands.CommandObject)4 ChannelSetting (com.github.vaerys.enums.ChannelSetting)4 SAILType (com.github.vaerys.enums.SAILType)4 RewardRoleObject (com.github.vaerys.objects.RewardRoleObject)4 CharacterObject (com.github.vaerys.objects.CharacterObject)3 ProfileObject (com.github.vaerys.objects.ProfileObject)3 Command (com.github.vaerys.templates.Command)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 DBGuild (me.shadorc.shadbot.data.db.DBGuild)3 Permissions (sx.blah.discord.handle.obj.Permissions)3 GuildHandler (com.github.vaerys.handlers.GuildHandler)2