added perm check
This commit is contained in:
@@ -258,8 +258,59 @@ func HandleReactionRoleModalSubmit(s *discordgo.Session, i *discordgo.Interactio
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// botHasManageRoles checks if the bot has the Manage Roles permission in the guild.
|
||||||
|
func botHasManageRoles(s *discordgo.Session, guildID string) (bool, error) {
|
||||||
|
botMember, err := s.GuildMember(guildID, s.State.User.ID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
roles, err := s.GuildRoles(guildID)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Combine permissions from all roles the bot has.
|
||||||
|
var perms int64 = 0
|
||||||
|
for _, botRoleID := range botMember.Roles {
|
||||||
|
for _, role := range roles {
|
||||||
|
if role.ID == botRoleID {
|
||||||
|
perms |= int64(role.Permissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// discordgo.PermissionManageRoles is 0x10000000 (268435456)
|
||||||
|
if perms&discordgo.PermissionManageRoles != 0 {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
// HandleReactionRoleButton processes button clicks for reaction roles.
|
// HandleReactionRoleButton processes button clicks for reaction roles.
|
||||||
func HandleReactionRoleButton(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
func HandleReactionRoleButton(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
// Check if the bot has Manage Roles permission.
|
||||||
|
if i.GuildID == "" {
|
||||||
|
helpers.HandleError(s, i, fmt.Errorf("cannot assign roles in DMs"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hasPerm, err := botHasManageRoles(s, i.GuildID)
|
||||||
|
if err != nil {
|
||||||
|
helpers.HandleError(s, i, fmt.Errorf("failed to check bot permissions: %w", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hasPerm {
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "*Bot does not have the Manage Roles permission!*",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
customID := i.MessageComponentData().CustomID
|
customID := i.MessageComponentData().CustomID
|
||||||
// Expect customID in the format "rr:<roleID>"
|
// Expect customID in the format "rr:<roleID>"
|
||||||
roleID := strings.TrimPrefix(customID, "rr:")
|
roleID := strings.TrimPrefix(customID, "rr:")
|
||||||
@@ -268,10 +319,6 @@ func HandleReactionRoleButton(s *discordgo.Session, i *discordgo.InteractionCrea
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if i.GuildID == "" {
|
|
||||||
helpers.HandleError(s, i, fmt.Errorf("cannot assign roles in DMs"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
member := i.Member
|
member := i.Member
|
||||||
if member == nil {
|
if member == nil {
|
||||||
helpers.HandleError(s, i, fmt.Errorf("member not found"))
|
helpers.HandleError(s, i, fmt.Errorf("member not found"))
|
||||||
@@ -285,7 +332,7 @@ func HandleReactionRoleButton(s *discordgo.Session, i *discordgo.InteractionCrea
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Respond to acknowledge the action.
|
// Respond to acknowledge the action.
|
||||||
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: "Role successfully assigned!",
|
Content: "Role successfully assigned!",
|
||||||
|
|||||||
Reference in New Issue
Block a user