diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index 67a3561..85e3860 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -5,7 +5,6 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.*; -import net.dv8tion.jda.api.events.DisconnectEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberLeaveEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; @@ -44,6 +43,10 @@ public class DiscordClient extends ListenerAdapter { private static boolean limitedWhitelistEnabled; private static boolean usernameValidation; + private static boolean whitelistedRoleAutoAdd; + private static boolean whitelistedRoleAutoRemove; + private static String whitelistedRoleName; + private final char[] validCharacters = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_'}; @@ -76,6 +79,11 @@ public class DiscordClient extends ListenerAdapter { maxWhitelistAmount = DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount"); limitedWhitelistEnabled = DiscordWhitelister.getWhitelisterBotConfig().getBoolean("limited-whitelist-enabled"); usernameValidation = DiscordWhitelister.getWhitelisterBotConfig().getBoolean("username-validation"); + + // Set the name of the role to add/remove to/from the user after they have been added/removed to/from the whitelist and if this feature is enabled + whitelistedRoleAutoAdd = DiscordWhitelister.getWhitelisterBotConfig().getBoolean("whitelisted-role-auto-add"); + whitelistedRoleAutoRemove = DiscordWhitelister.getWhitelisterBotConfig().getBoolean("whitelisted-role-auto-remove"); + whitelistedRoleName = DiscordWhitelister.getWhitelisterBotConfig().getString("whitelisted-role"); } private static void BuildStrings() { @@ -371,6 +379,18 @@ public class DiscordClient extends ListenerAdapter { if (!invalidMinecraftName && tempFileConfiguration.getStringList("whitelisted").contains(finalNameToAdd)) { channel.sendMessage(embedBuilderWhitelistSuccess.build()).queue(); + // Add role to user when they have been added to the whitelist if need be + if(whitelistedRoleAutoAdd) { + Role whitelistRole = null; + try { + whitelistRole = javaDiscordAPI.getRolesByName(whitelistedRoleName, false).get(0); + Member member = messageReceivedEvent.getMember(); + messageReceivedEvent.getGuild().addRoleToMember(member, whitelistRole).queue(); + } catch (Exception e) { + DiscordWhitelister.getPlugin().getLogger().severe("Could not add role with name " + whitelistedRoleName + " to " + author.getName() + ", check that it has the correct name in the config"); + } + } + if (onlyHasLimitedAdd) { DiscordWhitelister.addRegisteredUser(author.getId(), finalNameToAdd); @@ -388,6 +408,18 @@ public class DiscordClient extends ListenerAdapter { if (checkWhitelistJSON(whitelistJSON, finalNameToAdd)) { channel.sendMessage(embedBuilderWhitelistSuccess.build()).queue(); + // Add role to user when they have been added to the whitelist if need be + if(whitelistedRoleAutoAdd) { + Role whitelistRole = null; + try { + whitelistRole = javaDiscordAPI.getRolesByName(whitelistedRoleName, false).get(0); + Member member = messageReceivedEvent.getMember(); + messageReceivedEvent.getGuild().addRoleToMember(member, whitelistRole).queue(); + } catch (Exception e) { + DiscordWhitelister.getPlugin().getLogger().severe("Could not add role with name " + whitelistedRoleName + " to " + author.getName() + ", check that it has the correct name in the config"); + } + } + if (onlyHasLimitedAdd) { DiscordWhitelister.addRegisteredUser(author.getId(), finalNameToAdd); @@ -490,6 +522,18 @@ public class DiscordClient extends ListenerAdapter { if (!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove)) { channel.sendMessage(embedBuilderSuccess.build()).queue(); + // Remove role from user when they have been removed from the whitelist if need be + if(whitelistedRoleAutoRemove) { + Role whitelistRole = null; + try { + whitelistRole = javaDiscordAPI.getRolesByName(whitelistedRoleName, false).get(0); + Member member = messageReceivedEvent.getMember(); + messageReceivedEvent.getGuild().removeRoleFromMember(member, whitelistRole).queue(); + } catch (Exception e) { + DiscordWhitelister.getPlugin().getLogger().severe("Could not remove role with name " + whitelistedRoleName + " from " + author.getName() + ", check that it has the correct name in the config"); + } + } + // if the name is not on the list if (DiscordWhitelister.getRemovedList().get(finalNameToRemove) == null) { DiscordWhitelister.getRemovedList().set(finalNameToRemove, author.getId()); @@ -506,6 +550,18 @@ public class DiscordClient extends ListenerAdapter { if (!checkWhitelistJSON(whitelistJSON, finalNameToRemove)) { channel.sendMessage(embedBuilderSuccess.build()).queue(); + // Remove role from user when they have been removed from the whitelist if need be + if(whitelistedRoleAutoRemove) { + Role whitelistRole = null; + try { + whitelistRole = javaDiscordAPI.getRolesByName(whitelistedRoleName, false).get(0); + Member member = messageReceivedEvent.getMember(); + messageReceivedEvent.getGuild().removeRoleFromMember(member, whitelistRole).queue(); + } catch (Exception e) { + DiscordWhitelister.getPlugin().getLogger().severe("Could not remove role with name " + whitelistedRoleName + " from " + author.getName() + ", check that it has the correct name in the config"); + } + } + // if the name is not on the list if (DiscordWhitelister.getRemovedList().get(finalNameToRemove) == null) { DiscordWhitelister.getRemovedList().set(finalNameToRemove, author.getId()); diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index 9633f5a..98e51f4 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -376,6 +376,39 @@ public class DiscordWhitelister extends JavaPlugin } } + // If adding the whitelisted role to the discord user is enabled + if(getWhitelisterBotConfig().get("whitelisted-role-auto-add") == null) + { + getWhitelisterBotConfig().set("whitelisted-role-auto-add", false); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'whitelisted-role-auto-add' was not found, adding it to the config..."); + } + } + + // If removing the whitelisted role from the discord user is enabled + if(getWhitelisterBotConfig().get("whitelisted-role-auto-remove") == null) + { + getWhitelisterBotConfig().set("whitelisted-role-auto-remove", false); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'whitelisted-role-auto-remove' was not found, adding it to the config..."); + } + } + + // The name of the role to add/remove to the user + if(getWhitelisterBotConfig().get("whitelisted-role") == null) + { + getWhitelisterBotConfig().set("whitelisted-role", "Whitelisted"); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'whitelisted-role' was not found, adding it to the config..."); + } + } + // easy whitelist support if(getWhitelisterBotConfig().get("use-easy-whitelist") == null) {