From ba9b761b6dd4d848f868554f56e55639d298230e Mon Sep 17 00:00:00 2001 From: Joe Shimell <49585339+JoeShimell@users.noreply.github.com> Date: Tue, 2 Jun 2020 18:29:57 +0100 Subject: [PATCH] Support for using ids for Discord roles instead of the role name Helps when trying to add Role with emojis and special characters --- .../uk/co/angrybee/joe/AuthorPermissions.java | 69 +++++++++++++++---- .../uk/co/angrybee/joe/DiscordClient.java | 21 ++++-- .../co/angrybee/joe/DiscordWhitelister.java | 14 ++++ 3 files changed, 83 insertions(+), 21 deletions(-) diff --git a/src/main/java/uk/co/angrybee/joe/AuthorPermissions.java b/src/main/java/uk/co/angrybee/joe/AuthorPermissions.java index 0379f17..8d4ea94 100644 --- a/src/main/java/uk/co/angrybee/joe/AuthorPermissions.java +++ b/src/main/java/uk/co/angrybee/joe/AuthorPermissions.java @@ -22,29 +22,70 @@ public class AuthorPermissions { return userHasLimitedAdd; } - public boolean isUserCanUseCommand() { + public boolean isUserCanUseCommand() + { return userCanAdd || userCanAddRemove || userHasLimitedAdd; } - public AuthorPermissions(MessageReceivedEvent event) { - for (Role role : event.getGuild().getMember(event.getAuthor()).getRoles()) { - if (Arrays.stream(DiscordClient.allowedToAddRemoveRoles).parallel().anyMatch(role.getName()::equalsIgnoreCase)) { - userCanAddRemove = true; - break; + public AuthorPermissions(MessageReceivedEvent event) + { + for (Role role : event.getGuild().getMember(event.getAuthor()).getRoles()) + { + if(!DiscordWhitelister.useIdForRoles) + { + if (Arrays.stream(DiscordClient.allowedToAddRemoveRoles).parallel().anyMatch(role.getName()::equalsIgnoreCase)) + { + userCanAddRemove = true; + break; + } + } + else + { + if (Arrays.stream(DiscordClient.allowedToAddRemoveRoles).parallel().anyMatch(role.getId()::equalsIgnoreCase)) + { + userCanAddRemove = true; + break; + } } } - for (Role role : event.getGuild().getMember(event.getAuthor()).getRoles()) { - if (Arrays.stream(DiscordClient.allowedToAddRoles).parallel().anyMatch(role.getName()::equalsIgnoreCase)) { - userCanAdd = true; - break; + for (Role role : event.getGuild().getMember(event.getAuthor()).getRoles()) + { + if (!DiscordWhitelister.useIdForRoles) + { + if (Arrays.stream(DiscordClient.allowedToAddRoles).parallel().anyMatch(role.getName()::equalsIgnoreCase)) + { + userCanAdd = true; + break; + } + } + else + { + if (Arrays.stream(DiscordClient.allowedToAddRoles).parallel().anyMatch(role.getId()::equalsIgnoreCase)) + { + userCanAdd = true; + break; + } } } - for (Role role : event.getGuild().getMember(event.getAuthor()).getRoles()) { - if (Arrays.stream(DiscordClient.allowedToAddLimitedRoles).parallel().anyMatch(role.getName()::equalsIgnoreCase)) { - userHasLimitedAdd = true; - break; + for (Role role : event.getGuild().getMember(event.getAuthor()).getRoles()) + { + if(!DiscordWhitelister.useIdForRoles) + { + if (Arrays.stream(DiscordClient.allowedToAddLimitedRoles).parallel().anyMatch(role.getName()::equalsIgnoreCase)) + { + userHasLimitedAdd = true; + break; + } + } + else + { + if (Arrays.stream(DiscordClient.allowedToAddLimitedRoles).parallel().anyMatch(role.getId()::equalsIgnoreCase)) + { + userHasLimitedAdd = true; + break; + } } } } diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index f3135fd..0094b78 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -69,7 +69,8 @@ public class DiscordClient extends ListenerAdapter { } } - private static void AssignVars() { + private static void AssignVars() + { // assign vars here instead of every time a message is received, as they do not change targetTextChannels = new String[DiscordWhitelister.getWhitelisterBotConfig().getList("target-text-channels").size()]; for (int i = 0; i < targetTextChannels.length; ++i) { @@ -86,7 +87,8 @@ public class DiscordClient extends ListenerAdapter { whitelistedRoleName = DiscordWhitelister.getWhitelisterBotConfig().getString("whitelisted-role"); } - private static void BuildStrings() { + private static void BuildStrings() + { // build here instead of every time a message is received, as they do not change EmbedBuilder embedBuilderBotInfo = new EmbedBuilder(); embedBuilderBotInfo.setTitle("Discord Whitelister Bot for Spigot"); @@ -128,14 +130,18 @@ public class DiscordClient extends ListenerAdapter { } @Override - public void onMessageReceived(MessageReceivedEvent messageReceivedEvent) { - if (messageReceivedEvent.isFromType(ChannelType.TEXT)) { + public void onMessageReceived(MessageReceivedEvent messageReceivedEvent) + { + if (messageReceivedEvent.isFromType(ChannelType.TEXT)) + { // Check if message should be handled - if (!Arrays.asList(targetTextChannels).contains(messageReceivedEvent.getTextChannel().getId())) { + if (!Arrays.asList(targetTextChannels).contains(messageReceivedEvent.getTextChannel().getId())) + { return; } - if (messageReceivedEvent.getAuthor().isBot()) { + if (messageReceivedEvent.getAuthor().isBot()) + { return; } @@ -817,7 +823,8 @@ public class DiscordClient extends ListenerAdapter { } // if the name is not on the list - if (DiscordWhitelister.getRemovedList().get(finalNameToRemove) == null) { + if (DiscordWhitelister.getRemovedList().get(finalNameToRemove) == null) + { DiscordWhitelister.getRemovedList().set(finalNameToRemove, author.getId()); DiscordWhitelister.getRemovedList().save(DiscordWhitelister.getRemovedListFile().getPath()); } diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index 8dcba0a..a4ce3b1 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -41,6 +41,7 @@ public class DiscordWhitelister extends JavaPlugin public static boolean useEasyWhitelist = false; public static boolean useCustomMessages = false; + public static boolean useIdForRoles = false; private boolean botEnabled; @@ -78,6 +79,9 @@ public class DiscordWhitelister extends JavaPlugin { getLogger().info("Initializing Discord client..."); + if(getWhitelisterBotConfig().getBoolean("use-id-for-roles")) + useIdForRoles = true; + // set add & remove roles DiscordClient.allowedToAddRemoveRoles = new String[getWhitelisterBotConfig().getList("add-remove-roles").size()]; for(int roles = 0; roles < DiscordClient.allowedToAddRemoveRoles.length; ++roles) @@ -496,6 +500,16 @@ public class DiscordWhitelister extends JavaPlugin } } + if(getWhitelisterBotConfig().get("use-id-for-roles") == null) + { + getWhitelisterBotConfig().set("use-id-for-roles", false); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'use-id-for-roles' was not found, adding it to the config..."); + } + } + try { getWhitelisterBotConfig().save((whitelisterBotConfigFile.getPath()));