add check to see if the bot can give the role when adding a new role to the auto role list
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 45s

This commit is contained in:
Lee 2024-06-25 16:45:59 +01:00
parent e0fca911d9
commit 055c8709f8
4 changed files with 33 additions and 7 deletions

@ -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());
}
}

@ -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
*/

@ -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();
}
}

@ -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();
}