From 2c6dcc08cd04135e075bd98e7c1ca9357bfd8683 Mon Sep 17 00:00:00 2001 From: Liam Date: Thu, 27 Jun 2024 19:48:15 +0100 Subject: [PATCH] cleanup missing permission message and check if the user has permission to give the role when adding a new auto role --- .../features/autorole/command/AddSubCommand.java | 14 +++++++++++++- .../command/userfeed/UserFeedCommand.java | 2 +- .../cc/fascinated/bat/service/CommandService.java | 10 ++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/cc/fascinated/bat/features/autorole/command/AddSubCommand.java b/src/main/java/cc/fascinated/bat/features/autorole/command/AddSubCommand.java index ee1e6c8..c7f4d90 100644 --- a/src/main/java/cc/fascinated/bat/features/autorole/command/AddSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/autorole/command/AddSubCommand.java @@ -35,6 +35,7 @@ public class AddSubCommand extends BatSubCommand { @Override public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class); + // Check if the guild has reached the maximum auto roles count if (profile.getRoleSlotsInUse() >= profile.getMaxRoles()) { interaction.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("You can only have a maximum of %d roles set for the auto role feature" @@ -50,8 +51,9 @@ public class AddSubCommand extends BatSubCommand { .build()).queue(); return; } - Role role = option.getAsRole(); + + // Check if the role is already in the auto roles list if (profile.hasRole(role.getId())) { interaction.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("The role %s is already in the auto roles list".formatted(role.getAsMention())) @@ -59,6 +61,7 @@ public class AddSubCommand extends BatSubCommand { return; } + // Check if the bot has permission to give the role if (!RoleUtils.hasPermissionToGiveRole(guild, guild.getDiscordGuild().getSelfMember(), role)) { interaction.replyEmbeds(EmbedUtils.errorEmbed() .setDescription("I do not have permission to give the role %s".formatted(role.getAsMention())) @@ -66,6 +69,15 @@ public class AddSubCommand extends BatSubCommand { return; } + // Check if the role is higher than the user adding the role + if (!RoleUtils.hasPermissionToGiveRole(guild, member, role)) { + interaction.replyEmbeds(EmbedUtils.errorEmbed() + .setDescription("You cannot add a role that is higher than you") + .build()).queue(); + return; + } + + // Add the role to the auto roles list profile.addRole(role.getId()); guildService.saveGuild(guild); interaction.replyEmbeds(EmbedUtils.successEmbed() diff --git a/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserFeedCommand.java b/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserFeedCommand.java index 125b0fd..9b281ed 100644 --- a/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserFeedCommand.java +++ b/src/main/java/cc/fascinated/bat/features/scoresaber/command/userfeed/UserFeedCommand.java @@ -11,7 +11,7 @@ import org.springframework.stereotype.Component; * @author Fascinated (fascinated7) */ @Component("scoresaber-user-feed.command") -@CommandInfo(name = "scoresaber-user-feed", description = "Modifies the settings for the feed.", requiredPermissions = Permission.MANAGE_SERVER) +@CommandInfo(name = "scoresaber-user-feed", description = "Modifies the settings for the feed.", requiredPermissions = Permission.MANAGE_CHANNEL) public class UserFeedCommand extends BatCommand { public UserFeedCommand(@NonNull ApplicationContext context) { super.addSubCommand(context.getBean(UserSubCommand.class)); diff --git a/src/main/java/cc/fascinated/bat/service/CommandService.java b/src/main/java/cc/fascinated/bat/service/CommandService.java index 9e01513..72a7b2e0 100644 --- a/src/main/java/cc/fascinated/bat/service/CommandService.java +++ b/src/main/java/cc/fascinated/bat/service/CommandService.java @@ -27,7 +27,8 @@ import java.util.*; * @author Fascinated (fascinated7) */ @Service -@Log4j2 @Getter +@Log4j2 +@Getter @DependsOn("discordService") public class CommandService extends ListenerAdapter { /** @@ -159,8 +160,13 @@ public class CommandService extends ListenerAdapter { } } if (!missingPermissions.isEmpty()) { + StringBuilder missing = new StringBuilder(); + for (Permission permission : missingPermissions) { + missing.append("`").append(permission.getName()).append("`").append(", "); + } event.replyEmbeds(EmbedUtils.errorEmbed() - .setDescription("You are missing the following permissions to execute this command: " + missingPermissions) + .setDescription("You are missing the following permissions to execute this command:\n" + + missing.substring(0, missing.length() - 2)) .build()) .setEphemeral(true) .queue();