package main import ( "github.com/bwmarrin/discordgo" "github.com/sirupsen/logrus" "golang.org/x/exp/slices" ) // Populate Users map for the first launch // This variable will be keep as sort of cache for all // operation func populateUserRole(discordClient *discordgo.Session) error { Users = make(map[string]*UserRole) APIResponse, err := getUsers42CTF() if err != nil { return err } for id, rank := range APIResponse { tmp, err := discordClient.GuildMember(guild, id) if err == nil { if slices.Contains(tmp.Roles, rolesID["top1"]) { Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]} } else if slices.Contains(tmp.Roles, rolesID["top10"]) { Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]} } else if slices.Contains(tmp.Roles, rolesID["top50"]) { Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} } else { Users[id] = &UserRole{Rank: rank, CurrentRole: ""} } } else { logrus.WithFields(logrus.Fields{ "id": id, "rank": rank, }).Error(err.Error()) } } return nil } func updateUserRole(discordClient *discordgo.Session, dryrun bool) error { APIResponse, err := getUsers42CTF() if err != nil { return err } for id, rank := range APIResponse { tmp, err := discordClient.GuildMember(guild, id) if err == nil { if slices.Contains(tmp.Roles, rolesID["top1"]) { Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]} } else if slices.Contains(tmp.Roles, rolesID["top10"]) { Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]} } else if slices.Contains(tmp.Roles, rolesID["top50"]) { Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} } else { Users[id] = &UserRole{Rank: rank, CurrentRole: ""} } } else { logrus.WithFields(logrus.Fields{ "id": id, "rank": rank, }).Error(err.Error()) } user, exist := Users[id] if exist { if user.Rank != rank { logrus.Info("New rank (", rank, ") for user ", id) if user.CurrentRole != "" { logrus.Info("Remove role for ", id) removeRole(discordClient, guild, id, user.CurrentRole, dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: ""} } if rank == 1 { logrus.Info("Add role `top1` for ", id) addRole(discordClient, guild, id, rolesID["top1"], dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]} } else if rank <= 10 { logrus.Info("Add role `top10` for ", id) addRole(discordClient, guild, id, rolesID["top10"], dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]} } else if rank <= 50 { logrus.Info("Add role `top50` for ", id) addRole(discordClient, guild, id, rolesID["top50"], dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} } } } } return nil } func forceUpdate(discordClient *discordgo.Session, dryrun bool) (error) { APIResponse, err := getUsers42CTF() if err != nil { return err } for id, rank := range APIResponse { tmp, err := discordClient.GuildMember(guild, id) if err == nil { if slices.Contains(tmp.Roles, rolesID["top1"]) { removeRole(discordClient, guild, id, rolesID["top1"], dryrun) } if slices.Contains(tmp.Roles, rolesID["top10"]) { removeRole(discordClient, guild, id, rolesID["top10"], dryrun) } if slices.Contains(tmp.Roles, rolesID["top50"]) { removeRole(discordClient, guild, id, rolesID["top50"], dryrun) } Users[id] = &UserRole{Rank: rank, CurrentRole: ""} } else { logrus.Error("ID not found", id) } _, exist := Users[id] if exist { if rank == 1 { logrus.Info("Add role `top1` for ", id) addRole(discordClient, guild, id, rolesID["top1"], dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]} } else if rank <= 10 { logrus.Info("Add role `top10` for ", id) addRole(discordClient, guild, id, rolesID["top10"], dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]} } else if rank <= 50 { logrus.Info("Add role `top50` for ", id) addRole(discordClient, guild, id, rolesID["top50"], dryrun) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} } } } return nil }