Skip to content

Commit

Permalink
[FEAT] Add support for Role ID's along with Role Name
Browse files Browse the repository at this point in the history
  • Loading branch information
hypherionmc committed Feb 25, 2023
1 parent abb403d commit 87cf431
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
73 changes: 53 additions & 20 deletions src/main/java/me/hypherionmc/sdlinklib/discord/BotController.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@
import okhttp3.OkHttpClient;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import static me.hypherionmc.sdlinklib.config.ConfigController.modConfig;

Expand All @@ -69,7 +72,7 @@ public final class BotController {
// Common Variables
private CommandClient commandClient;
private WebhookClient chatWebhookClient, eventWebhookClient, consoleWebhookClient;
private String adminRole = "";
private Role adminRole;

private Role whitelistedRole;

Expand All @@ -91,6 +94,8 @@ public final class BotController {
// Invite URL for bot shown in server logs
private final String DISCORD_INVITE = "https://discord.com/api/oauth2/authorize?client_id={bot_id}&permissions=3154463760&scope=bot%20applications.commands";

private final Pattern ID_MATCHER = Pattern.compile("[0-9]+", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);

public BotController(IMinecraftHelper minecraftHelper, Logger logger) {
LOGGER = logger;

Expand Down Expand Up @@ -214,38 +219,66 @@ private void checkBotSetup() {
} else {
Guild guild = _jda.getGuilds().get(0);

if (!modConfig.generalConfig.autoWhitelistRole.isEmpty()) {
List<Role> roles = guild.getRolesByName(modConfig.generalConfig.autoWhitelistRole, true);

if (!roles.isEmpty()) {
whitelistedRole = roles.get(0);
}
}

if (!modConfig.generalConfig.linkedRole.isEmpty()) {
List<Role> roles = guild.getRolesByName(modConfig.generalConfig.linkedRole, true);

if (!roles.isEmpty()) {
linkedRole = roles.get(0);
}
}

if (guild != null) {
Member bot = guild.getMemberById(_jda.getSelfUser().getIdLong());
EnumSet<Permission> botPerms = bot.getPermissionsExplicit();

// Find staff roles, and add them to list
if (!modConfig.botConfig.staffRole.isEmpty()) {
List<Role> roles = guild.getRolesByName(modConfig.botConfig.staffRole, true);
List<Role> roles = new ArrayList<>();

if (ID_MATCHER.matcher(modConfig.botConfig.staffRole).matches()) {
Role staffRole = guild.getRoleById(modConfig.botConfig.staffRole);
if (staffRole != null) {
roles = Collections.singletonList(staffRole);
}
} else {
roles = guild.getRolesByName(modConfig.botConfig.staffRole, true);
}

if (!roles.isEmpty()) {
adminRole = modConfig.botConfig.staffRole;
adminRole = roles.get(0);
} else {
errCount.incrementAndGet();
builder.append(errCount.get()).append(") ").append("Missing Staff Role. Role :").append(modConfig.botConfig.staffRole).append(" cannot be found in the server").append("\r\n");
}
}

// Find additional roles
if (!modConfig.generalConfig.autoWhitelistRole.isEmpty()) {
List<Role> roles = new ArrayList<>();

if (ID_MATCHER.matcher(modConfig.generalConfig.autoWhitelistRole).matches()) {
Role whitelistRole = guild.getRoleById(modConfig.generalConfig.autoWhitelistRole);
if (whitelistRole != null) {
roles = Collections.singletonList(whitelistRole);
}
} else {
roles = guild.getRolesByName(modConfig.generalConfig.autoWhitelistRole, true);
}

if (!roles.isEmpty()) {
whitelistedRole = roles.get(0);
}
}

if (!modConfig.generalConfig.linkedRole.isEmpty()) {
List<Role> roles = new ArrayList<>();

if (ID_MATCHER.matcher(modConfig.generalConfig.linkedRole).matches()) {
Role linkedRole = guild.getRoleById(modConfig.generalConfig.linkedRole);
if (linkedRole != null) {
roles = Collections.singletonList(linkedRole);
}
} else {
roles = guild.getRolesByName(modConfig.generalConfig.linkedRole, true);
}

if (!roles.isEmpty()) {
linkedRole = roles.get(0);
}
}

if (!botPerms.contains(Permission.ADMINISTRATOR)) {
if (!botPerms.contains(Permission.NICKNAME_CHANGE)) {
errCount.incrementAndGet();
Expand Down Expand Up @@ -497,7 +530,7 @@ public Role getLinkedRole() {
return linkedRole;
}

public String getAdminRole() {
public Role getAdminRole() {
return adminRole;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class BaseCommand extends Command {

public BaseCommand(BotController controller, boolean requiresPerms) {
if (requiresPerms) {
if (!controller.getAdminRole().isEmpty()) {
this.requiredRole = controller.getAdminRole();
if (controller.getAdminRole() != null) {
this.requiredRole = controller.getAdminRole().getName();
} else {
this.userPermissions = new Permission[] { Permission.ADMINISTRATOR, Permission.KICK_MEMBERS };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected void execute(CommandEvent event) {
linkedCommand.ifPresent(command -> {
if (!command.discordRole.isEmpty()) {
Optional<Role> role = event.getMember().getRoles()
.stream().filter(r -> r.getName().equalsIgnoreCase(command.discordRole))
.stream().filter(r -> r.getName().equalsIgnoreCase(command.discordRole) || r.getId().equals(command.discordRole))
.findFirst();

if (role.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class BaseSlashCommand extends SlashCommand {

public BaseSlashCommand(BotController controller, boolean requiresPerms) {
if (requiresPerms) {
if (!controller.getAdminRole().isEmpty()) {
this.requiredRole = controller.getAdminRole();
if (controller.getAdminRole() != null) {
this.requiredRole = controller.getAdminRole().getName();
} else {
this.userPermissions = new Permission[] { Permission.ADMINISTRATOR, Permission.KICK_MEMBERS };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void execute(SlashCommandEvent event) {
linkedCommand.ifPresent(command -> {
if (!command.discordRole.isEmpty()) {
Optional<Role> role = event.getMember().getRoles()
.stream().filter(r -> r.getName().equalsIgnoreCase(command.discordRole))
.stream().filter(r -> r.getName().equalsIgnoreCase(command.discordRole) || r.getId().equals(command.discordRole))
.findFirst();

if (role.isPresent()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/hypherionmc/sdlinklib/utils/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public static String secondsToTimestamp(long sec) {
}

public static boolean hasPermission(BotController controller, Member member) {
if (!controller.getAdminRole().isEmpty()) {
return member.getRoles().stream().anyMatch(r -> r.getName().equalsIgnoreCase(controller.getAdminRole()));
if (controller.getAdminRole() != null) {
return member.getRoles().stream().anyMatch(r -> r.getIdLong() == controller.getAdminRole().getIdLong());
}
return member.hasPermission(Permission.ADMINISTRATOR) || member.hasPermission(Permission.KICK_MEMBERS);
}
Expand Down

0 comments on commit 87cf431

Please sign in to comment.