fix(bot): Check if user exist before perform action

This commit is contained in:
Starthur 2023-02-07 09:22:32 +01:00
parent 6363dd6857
commit d62b7beaf0
Signed by: Starthur
GPG Key ID: 68AD34A2CF658E32
4 changed files with 53 additions and 37 deletions

View File

@ -9,7 +9,9 @@ func removeRole(discordClient *discordgo.Session, guildID string, userID string,
if ! dryrun { if ! dryrun {
err := discordClient.GuildMemberRoleRemove(guild, userID, roleID) err := discordClient.GuildMemberRoleRemove(guild, userID, roleID)
if err != nil { if err != nil {
logrus.Error(err.Error()) logrus.WithFields(logrus.Fields{
"id": userID,
}).Error(err.Error())
} }
} }
} }
@ -18,7 +20,9 @@ func addRole(discordClient *discordgo.Session, guildID string, userID string, ro
if ! dryrun { if ! dryrun {
err := discordClient.GuildMemberRoleAdd(guild, userID, roleID) err := discordClient.GuildMemberRoleAdd(guild, userID, roleID)
if err != nil { if err != nil {
logrus.Error(err.Error()) logrus.WithFields(logrus.Fields{
"id": userID,
}).Error(err.Error())
} }
} }
} }

View File

@ -5,4 +5,4 @@ services:
build: . build: .
container_name: bot_discord container_name: bot_discord
env_file: ./env_file env_file: ./env_file
entrypoint: bot --guild=943459216901955604 --top1=798638767359524875 --top10=801787467064672286 --top50=803729539145924649 entrypoint: bot --guild=606162827274616845 --top1=798638767359524875 --top10=801787467064672286 --top50=803729539145924649

View File

@ -16,7 +16,7 @@ type UserRole struct {
} }
// Global var (I know, and i don't care) // Global var (I know, and i don't care)
var api string = "https://preprod.42ctf.org/api/bot/discord?token=" + os.Getenv("AUTH_TOKEN") var api string = os.Getenv("API_URL")
var guild string var guild string
var rolesID map[string]string var rolesID map[string]string
var Users map[string]*UserRole var Users map[string]*UserRole

22
rank.go
View File

@ -29,7 +29,10 @@ func populateUserRole(discordClient *discordgo.Session) error {
Users[id] = &UserRole{Rank: rank, CurrentRole: ""} Users[id] = &UserRole{Rank: rank, CurrentRole: ""}
} }
} else { } else {
logrus.Error("ID not found", id) logrus.WithFields(logrus.Fields{
"id": id,
"rank": rank,
}).Error(err.Error())
} }
} }
return nil return nil
@ -53,13 +56,18 @@ func updateUserRole(discordClient *discordgo.Session, dryrun bool) error {
Users[id] = &UserRole{Rank: rank, CurrentRole: ""} Users[id] = &UserRole{Rank: rank, CurrentRole: ""}
} }
} else { } else {
logrus.Error("ID not found", id) logrus.WithFields(logrus.Fields{
"id": id,
"rank": rank,
}).Error(err.Error())
} }
if Users[id].Rank != rank { user, exist := Users[id]
if exist {
if user.Rank != rank {
logrus.Info("New rank (", rank, ") for user ", id) logrus.Info("New rank (", rank, ") for user ", id)
if Users[id].CurrentRole != "" { if user.CurrentRole != "" {
logrus.Info("Remove role for ", id) logrus.Info("Remove role for ", id)
removeRole(discordClient, guild, id, Users[id].CurrentRole, dryrun) removeRole(discordClient, guild, id, user.CurrentRole, dryrun)
Users[id] = &UserRole{Rank: rank, CurrentRole: ""} Users[id] = &UserRole{Rank: rank, CurrentRole: ""}
} }
if rank == 1 { if rank == 1 {
@ -77,6 +85,7 @@ func updateUserRole(discordClient *discordgo.Session, dryrun bool) error {
} }
} }
} }
}
return nil return nil
} }
@ -101,6 +110,8 @@ func forceUpdate(discordClient *discordgo.Session, dryrun bool) (error) {
} else { } else {
logrus.Error("ID not found", id) logrus.Error("ID not found", id)
} }
_, exist := Users[id]
if exist {
if rank == 1 { if rank == 1 {
logrus.Info("Add role `top1` for ", id) logrus.Info("Add role `top1` for ", id)
addRole(discordClient, guild, id, rolesID["top1"], dryrun) addRole(discordClient, guild, id, rolesID["top1"], dryrun)
@ -115,5 +126,6 @@ func forceUpdate(discordClient *discordgo.Session, dryrun bool) (error) {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]}
} }
} }
}
return nil return nil
} }