Skip to content

Commit

Permalink
Merge pull request #6 from alexisgeoffrey/readlock
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisgeoffrey authored Feb 25, 2022
2 parents bd1d485 + ae6be9c commit 02d8449
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions internal/discordapi/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ func (us users) updateAllEloRoles(s *discordgo.Session, guildId string) error {
if err != nil {
return fmt.Errorf("error retrieving guild %s: %w", guildId, err)
}
s.State.Lock()
defer s.State.Unlock()
s.State.RLock()
defer s.State.RUnlock()
rs := make([]*discordgo.Role, 0, len(g.Roles))

for _, role := range g.Roles {
if strings.Contains(role.Name, "Elo:") {
rs = append(rs, role)
rs = append(rs, &discordgo.Role{ID: role.ID})
}
}

sort.SliceStable(rs, func(i, j int) bool {
sort.Slice(rs, func(i, j int) bool {
matchSize := func(prefix string) aoe4api.TeamSize {
switch prefix {
case "1v1":
Expand All @@ -93,8 +93,8 @@ func (us users) updateAllEloRoles(s *discordgo.Session, guildId string) error {
return matchSize(iPrefix) > matchSize(jPrefix)
})

for i := range rs {
rs[i].Position = i
for i, role := range rs {
role.Position = i
}

if _, err := s.GuildRoleReorder(guildId, rs); err != nil {
Expand Down

0 comments on commit 02d8449

Please sign in to comment.