Skip to content

Commit

Permalink
Fix bug with not reassigning personal color role to user if they don'…
Browse files Browse the repository at this point in the history
…t have it
  • Loading branch information
chilipizdrick committed May 7, 2024
1 parent 2e80437 commit d4a2531
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion commands/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ var ColorCommand = utl.SlashCommand{
return
}

s.GuildMemberRoleAdd(i.GuildID, userID, personalRole.ID)
err = s.GuildMemberRoleAdd(i.GuildID, userID, personalRole.ID)
if err != nil {
utl.RespondToInteractionCreateWithString(s, i, "Could not give newly created personal role to user.")
log.Printf("[ERROR] Could not give newly created personal role to user. %v", err)
return
}
}

// If role has not been created (already existed) then fetch it
Expand All @@ -66,6 +71,23 @@ var ColorCommand = utl.SlashCommand{
}
}

// Check if role exists, but user does not have it
// (may happen when user leaves and rejoins the server)
member, err := s.State.Member(i.GuildID, userID)
if err != nil {
utl.RespondToInteractionCreateWithString(s, i, "Could not fetch user by ID.")
log.Printf("[ERROR] Error fetching member by ID. %v", err)
return
}
if !funk.Contains(member.Roles, personalRole.ID) {
err = s.GuildMemberRoleAdd(i.GuildID, userID, personalRole.ID)
if err != nil {
utl.RespondToInteractionCreateWithString(s, i, "Could not give personal role to user.")
log.Printf("[ERROR] Could not give personal role to user. %v", err)
return
}
}

optionMap := utl.GetOptionMap(i)
colorField := optionMap["color"].StringValue()
cleanedHexColor := strings.Replace(colorField, "#", "", -1)
Expand Down

0 comments on commit d4a2531

Please sign in to comment.