From 055c8709f898c4872251d1d9de9f6a9151dd5398 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 25 Jun 2024 16:45:59 +0100 Subject: [PATCH] add check to see if the bot can give the role when adding a new role to the auto role list --- .../cc/fascinated/bat/common/RoleUtils.java | 22 +++++++++++++++++++ .../cc/fascinated/bat/features/Feature.java | 5 ----- .../autorole/command/AddSubCommand.java | 11 +++++++++- .../autorole/command/RemoveSubCommand.java | 2 +- 4 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 src/main/java/cc/fascinated/bat/common/RoleUtils.java diff --git a/src/main/java/cc/fascinated/bat/common/RoleUtils.java b/src/main/java/cc/fascinated/bat/common/RoleUtils.java new file mode 100644 index 0000000..fbe0626 --- /dev/null +++ b/src/main/java/cc/fascinated/bat/common/RoleUtils.java @@ -0,0 +1,22 @@ +package cc.fascinated.bat.common; + +import cc.fascinated.bat.model.BatGuild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; + +/** + * @author Fascinated (fascinated7) + */ +public class RoleUtils { + /** + * Checks if a member has permission to give the role to another member + * + * @param guild the guild to check + * @param member the member to check + * @param role the role to check + * @return if the member has permission to give the role + */ + public static boolean hasPermissionToGiveRole(BatGuild guild, Member member, Role role) { + return member.getRoles().stream().anyMatch(r -> r.getPosition() > role.getPosition()); + } +} diff --git a/src/main/java/cc/fascinated/bat/features/Feature.java b/src/main/java/cc/fascinated/bat/features/Feature.java index 8d34f24..a8815b2 100644 --- a/src/main/java/cc/fascinated/bat/features/Feature.java +++ b/src/main/java/cc/fascinated/bat/features/Feature.java @@ -1,9 +1,7 @@ package cc.fascinated.bat.features; -import cc.fascinated.bat.service.CommandService; import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -13,9 +11,6 @@ import org.springframework.stereotype.Component; @Getter @Component public abstract class Feature { - @Autowired - private CommandService commandService; - /** * The name of the feature */ 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 5953e6c..a013ed0 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 @@ -2,9 +2,11 @@ package cc.fascinated.bat.features.autorole.command; import cc.fascinated.bat.command.BatSubCommand; import cc.fascinated.bat.common.EmbedUtils; +import cc.fascinated.bat.common.RoleUtils; import cc.fascinated.bat.features.autorole.profile.AutoRoleProfile; import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatUser; +import cc.fascinated.bat.service.DiscordService; import cc.fascinated.bat.service.GuildService; import lombok.NonNull; import net.dv8tion.jda.api.entities.Member; @@ -54,10 +56,17 @@ public class AddSubCommand extends BatSubCommand { return; } + 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())) + .build()).queue(); + return; + } + profile.addRole(role.getId()); guildService.saveGuild(guild); interaction.replyEmbeds(EmbedUtils.successEmbed() - .setDescription("Successfully added the role %s to the auto roles list".formatted(role.getAsMention())) + .setDescription("Successfully added the %s role to the auto roles list".formatted(role.getAsMention())) .build()).queue(); } } diff --git a/src/main/java/cc/fascinated/bat/features/autorole/command/RemoveSubCommand.java b/src/main/java/cc/fascinated/bat/features/autorole/command/RemoveSubCommand.java index d05f59a..4b37e4b 100644 --- a/src/main/java/cc/fascinated/bat/features/autorole/command/RemoveSubCommand.java +++ b/src/main/java/cc/fascinated/bat/features/autorole/command/RemoveSubCommand.java @@ -48,7 +48,7 @@ public class RemoveSubCommand extends BatSubCommand { profile.removeRole(role.getId()); guildService.saveGuild(guild); - interaction.replyEmbeds(EmbedUtils.errorEmbed() + interaction.replyEmbeds(EmbedUtils.successEmbed() .setDescription("Successfully removed the role %s from the auto roles list".formatted(role.getAsMention())) .build()).queue(); }