cleanup missing permission message and check if the user has permission to give the role when adding a new auto role

This commit is contained in:
Lee 2024-06-27 19:48:15 +01:00
parent f062fa21c3
commit 2c6dcc08cd
3 changed files with 22 additions and 4 deletions

@ -35,6 +35,7 @@ public class AddSubCommand extends BatSubCommand {
@Override @Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class); AutoRoleProfile profile = guild.getProfile(AutoRoleProfile.class);
// Check if the guild has reached the maximum auto roles count
if (profile.getRoleSlotsInUse() >= profile.getMaxRoles()) { if (profile.getRoleSlotsInUse() >= profile.getMaxRoles()) {
interaction.replyEmbeds(EmbedUtils.errorEmbed() interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("You can only have a maximum of %d roles set for the auto role feature" .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(); .build()).queue();
return; return;
} }
Role role = option.getAsRole(); Role role = option.getAsRole();
// Check if the role is already in the auto roles list
if (profile.hasRole(role.getId())) { if (profile.hasRole(role.getId())) {
interaction.replyEmbeds(EmbedUtils.errorEmbed() interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("The role %s is already in the auto roles list".formatted(role.getAsMention())) .setDescription("The role %s is already in the auto roles list".formatted(role.getAsMention()))
@ -59,6 +61,7 @@ public class AddSubCommand extends BatSubCommand {
return; return;
} }
// Check if the bot has permission to give the role
if (!RoleUtils.hasPermissionToGiveRole(guild, guild.getDiscordGuild().getSelfMember(), role)) { if (!RoleUtils.hasPermissionToGiveRole(guild, guild.getDiscordGuild().getSelfMember(), role)) {
interaction.replyEmbeds(EmbedUtils.errorEmbed() interaction.replyEmbeds(EmbedUtils.errorEmbed()
.setDescription("I do not have permission to give the role %s".formatted(role.getAsMention())) .setDescription("I do not have permission to give the role %s".formatted(role.getAsMention()))
@ -66,6 +69,15 @@ public class AddSubCommand extends BatSubCommand {
return; 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()); profile.addRole(role.getId());
guildService.saveGuild(guild); guildService.saveGuild(guild);
interaction.replyEmbeds(EmbedUtils.successEmbed() interaction.replyEmbeds(EmbedUtils.successEmbed()

@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Component("scoresaber-user-feed.command") @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 class UserFeedCommand extends BatCommand {
public UserFeedCommand(@NonNull ApplicationContext context) { public UserFeedCommand(@NonNull ApplicationContext context) {
super.addSubCommand(context.getBean(UserSubCommand.class)); super.addSubCommand(context.getBean(UserSubCommand.class));

@ -27,7 +27,8 @@ import java.util.*;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Service @Service
@Log4j2 @Getter @Log4j2
@Getter
@DependsOn("discordService") @DependsOn("discordService")
public class CommandService extends ListenerAdapter { public class CommandService extends ListenerAdapter {
/** /**
@ -159,8 +160,13 @@ public class CommandService extends ListenerAdapter {
} }
} }
if (!missingPermissions.isEmpty()) { if (!missingPermissions.isEmpty()) {
StringBuilder missing = new StringBuilder();
for (Permission permission : missingPermissions) {
missing.append("`").append(permission.getName()).append("`").append(", ");
}
event.replyEmbeds(EmbedUtils.errorEmbed() 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()) .build())
.setEphemeral(true) .setEphemeral(true)
.queue(); .queue();