Added option to add/remove a role to users after whitelisting.

This feature is meant to be used when users have the ability to whitelist themselves (limited-add-roles).
If whitelisted-role-auto-add is set to true (false by default), the Discord role with the name defined by whitelisted-role ("Whitelisted" by default) will be added when the user successfully adds to the whitelist.
If whitelisted-role-auto-remove is set to true (false by default), that role will be removed when the user successfully removes from the whitelist.
This commit is contained in:
vacer25 2020-04-24 17:40:36 -04:00
parent be4bc8fff5
commit 847273bf48
2 changed files with 90 additions and 1 deletions

View File

@ -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());

View File

@ -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)
{