Merge pull request #7 from vacer25/feature/DiscordRoleManagement

Added option to add/remove a role to users after whitelisting.
This commit is contained in:
Joe Shimell 2020-04-24 23:39:57 +01:00 committed by GitHub
commit 7c153397e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 16 deletions

View File

@ -17,19 +17,29 @@ If you upgrade without knowing what you are doing. Registration will not work co
- **limited-add group**: allows the user to whitelist a limited amount of times (recommended for users, default amount is 3)
- limited-add group can be disabled in the config (enabled by default)
- removed list:
- this list removes the ability for limited-add users to add back users that have been removed by the add-remove group
- can be disabled in the config (enabled by default)
- Removed list:
- This list removes the ability for limited-add users to add back users that have been removed by the add-remove group
- Can be disabled in the config (enabled by default)
- use '!whitelist add "minecraftUsername"' in a valid channel to whitelist people on your minecraft server
- use '!whitelist remove "minecraftUsername"' in a valid channel to remove people from the whitelist on your minecraft server
- use '!whitelist' in a valid channel to get info about the bot and how to use it
- Discord commands:
- Use `!whitelist add "minecraftUsername"` in a valid channel to whitelist people on your minecraft server
- Use `!whitelist remove "minecraftUsername"` in a valid channel to remove people from the whitelist on your minecraft server
- Use `!whitelist` in a valid channel to get info about the bot and how to use it
- only select Discord roles can whitelist through the bot
- bot only listens for messages in select text channels
- logs whitelist attempts from valid roles in the console
- Automatically add/remove a role when adding/removing to/from the whitelist
- This feature is meant to be used when users can add themselves to the whitelist.
- 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 to that user when they successfully add (themselves) to the whitelist.
- If `whitelisted-role-auto-remove` is set to true (false by default), that role will be removed from that user when they successfully remove (themselves) from the whitelist.
- This requires:
- The bot to have the `Manage Roles` permission in Discord
- Setting up a Discord role with the same name (case sensitive) as the config
- The bot's role must be higher than the whitelist role
- Only select Discord roles can whitelist through the bot
- Bot only listens for messages in select text channels
- Logs whitelist attempts from valid roles in the console
### Set Up:
Config file is located at: (server-root)/plugins/DiscordWhitelister - this needs a valid bot token and valid channel ids to work.
Config file is located at: (server-root)/plugins/DiscordWhitelister - this needs a valid bot token and valid channel id(s) to work.
To create a Discord application and/or find your discord bot token, follow this link: https://discordapp.com/developers/applications/

View File

@ -1,5 +1,5 @@
name: DiscordWhitelister
version: 1.2.0
version: 1.3.0
author: Joe Shimell
main: uk.co.angrybee.joe.DiscordWhitelister
description: Discord whitelister bot.

View File

@ -9,8 +9,7 @@
<version>1.0-SNAPSHOT</version>
<name>discord-whitelister</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<url>https://github.com/JoeShimell/DiscordWhitelisterSpigot</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -11,7 +11,7 @@ import uk.co.angrybee.joe.VersionInfo;
public class CommandAbout implements CommandExecutor {
// /dw
// version command
// about command
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
sender.sendMessage("[DW] DiscordWhiteLister by JoeShimell\nhttps://github.com/JoeShimell/DiscordWhitelisterSpigot");

View File

@ -11,7 +11,7 @@ import uk.co.angrybee.joe.VersionInfo;
public class CommandStatus implements CommandExecutor {
// /dw
// version command
// version & status command
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
String discordOnlineStatus = DiscordClient.getOnlineStatus();

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