diff --git a/discord.go b/discord.go index 2ae2b5b..53f43e1 100644 --- a/discord.go +++ b/discord.go @@ -9,7 +9,9 @@ func removeRole(discordClient *discordgo.Session, guildID string, userID string, if ! dryrun { err := discordClient.GuildMemberRoleRemove(guild, userID, roleID) 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 { err := discordClient.GuildMemberRoleAdd(guild, userID, roleID) if err != nil { - logrus.Error(err.Error()) + logrus.WithFields(logrus.Fields{ + "id": userID, + }).Error(err.Error()) } } } diff --git a/docker-compose.yaml b/docker-compose.yaml index d4dbab3..81d3823 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -5,4 +5,4 @@ services: build: . container_name: bot_discord env_file: ./env_file - entrypoint: bot --guild=943459216901955604 --top1=798638767359524875 --top10=801787467064672286 --top50=803729539145924649 + entrypoint: bot --guild=606162827274616845 --top1=798638767359524875 --top10=801787467064672286 --top50=803729539145924649 diff --git a/main.go b/main.go index 50c5861..665fe1a 100755 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ type UserRole struct { } // 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 rolesID map[string]string var Users map[string]*UserRole diff --git a/rank.go b/rank.go index dc8817a..a37cf5f 100644 --- a/rank.go +++ b/rank.go @@ -29,7 +29,10 @@ func populateUserRole(discordClient *discordgo.Session) error { Users[id] = &UserRole{Rank: rank, CurrentRole: ""} } } else { - logrus.Error("ID not found", id) + logrus.WithFields(logrus.Fields{ + "id": id, + "rank": rank, + }).Error(err.Error()) } } return nil @@ -53,27 +56,33 @@ func updateUserRole(discordClient *discordgo.Session, dryrun bool) error { Users[id] = &UserRole{Rank: rank, CurrentRole: ""} } } else { - logrus.Error("ID not found", id) + logrus.WithFields(logrus.Fields{ + "id": id, + "rank": rank, + }).Error(err.Error()) } - if Users[id].Rank != rank { - logrus.Info("New rank (", rank, ") for user ", id) - if Users[id].CurrentRole != "" { - logrus.Info("Remove role for ", id) - removeRole(discordClient, guild, id, Users[id].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"]} + 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"]} + } } } } @@ -101,18 +110,21 @@ func forceUpdate(discordClient *discordgo.Session, dryrun bool) (error) { } else { logrus.Error("ID not found", id) } - 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"]} + _, 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