[FEAT] Auto whitelisting based on account linking and roles

This commit is contained in:
2023-08-05 18:53:41 +02:00
parent b901609957
commit 0ffbc0f48e
3 changed files with 31 additions and 0 deletions

View File

@@ -299,6 +299,14 @@ public class MinecraftAccount {
if (account == null) if (account == null)
return false; return false;
User discordUser = getDiscordUser();
if (discordUser != null) {
Member m = BotController.INSTANCE.getJDA().getGuilds().get(0).getMemberById(discordUser.getId());
if (m != null)
return m.getRoles().stream().anyMatch(r -> RoleManager.getAutoWhitelistRoles().contains(r));
}
return !SDLinkPlatform.minecraftHelper.isPlayerWhitelisted(MinecraftAccount.standard(account.getUsername())).isError() && account.isWhitelisted(); return !SDLinkPlatform.minecraftHelper.isPlayerWhitelisted(MinecraftAccount.standard(account.getUsername())).isError() && account.isWhitelisted();
} }

View File

@@ -7,6 +7,9 @@ package com.hypherionmc.sdlink.core.config.impl;
import me.hypherionmc.moonconfig.core.conversion.Path; import me.hypherionmc.moonconfig.core.conversion.Path;
import me.hypherionmc.moonconfig.core.conversion.SpecComment; import me.hypherionmc.moonconfig.core.conversion.SpecComment;
import java.util.ArrayList;
import java.util.List;
/** /**
* @author HypherionSA * @author HypherionSA
* Config Structure to control Whitelisting and Account Linking * Config Structure to control Whitelisting and Account Linking
@@ -52,6 +55,10 @@ public class LinkAndWhitelistConfigSettings {
@SpecComment("Automatically link Minecraft and Discord Accounts when a user is whitelisted") @SpecComment("Automatically link Minecraft and Discord Accounts when a user is whitelisted")
public boolean linkedWhitelist = false; public boolean linkedWhitelist = false;
@Path("autoWhitelistRoles")
@SpecComment("Users with linked discord accounts, that contain any of these roles will be automatically whitelisted")
public List<String> autoWhitelistRoles = new ArrayList<>();
@Path("autoWhitelistRole") @Path("autoWhitelistRole")
@SpecComment("If a role ID (or name) is defined here, it will be assigned to players when they are whitelisted") @SpecComment("If a role ID (or name) is defined here, it will be assigned to players when they are whitelisted")
public String autoWhitelistRole = ""; public String autoWhitelistRole = "";

View File

@@ -10,7 +10,9 @@ import com.hypherionmc.sdlink.core.util.SystemUtils;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@@ -22,6 +24,7 @@ public class RoleManager {
private static Role whitelistedRole; private static Role whitelistedRole;
private static Role linkedRole; private static Role linkedRole;
private static final HashMap<String, Role> commandRoles = new HashMap<>(); private static final HashMap<String, Role> commandRoles = new HashMap<>();
private static final Set<Role> autoWhitelistRoles = new HashSet<>();
/** /**
* Check and load the roles required by the bot * Check and load the roles required by the bot
@@ -37,6 +40,15 @@ public class RoleManager {
linkedRole = getRole(errCount, builder, "Linked Account", SDLinkConfig.INSTANCE.whitelistingAndLinking.accountLinking.linkedRole); linkedRole = getRole(errCount, builder, "Linked Account", SDLinkConfig.INSTANCE.whitelistingAndLinking.accountLinking.linkedRole);
} }
if (SDLinkConfig.INSTANCE.whitelistingAndLinking.whitelisting.whitelisting) {
SDLinkConfig.INSTANCE.whitelistingAndLinking.whitelisting.autoWhitelistRoles.forEach(r -> {
Role role = getRole(errCount, builder, "Auto Whitelist Role", r);
if (role != null)
autoWhitelistRoles.add(role);
});
}
if (SDLinkConfig.INSTANCE.linkedCommands.enabled) { if (SDLinkConfig.INSTANCE.linkedCommands.enabled) {
commandRoles.clear(); commandRoles.clear();
SDLinkConfig.INSTANCE.linkedCommands.commands.forEach(cmd -> { SDLinkConfig.INSTANCE.linkedCommands.commands.forEach(cmd -> {
@@ -97,4 +109,8 @@ public class RoleManager {
public static HashMap<String, Role> getCommandRoles() { public static HashMap<String, Role> getCommandRoles() {
return commandRoles; return commandRoles;
} }
public static Set<Role> getAutoWhitelistRoles() {
return autoWhitelistRoles;
}
} }