Compare commits

...

3 Commits

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://www.42ctf.local/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

78
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,27 +56,33 @@ 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]
logrus.Info("New rank (", rank, ") for user ", id) if exist {
if Users[id].CurrentRole != "" { if user.Rank != rank {
logrus.Info("Remove role for ", id) logrus.Info("New rank (", rank, ") for user ", id)
removeRole(discordClient, guild, id, Users[id].CurrentRole, dryrun) if user.CurrentRole != "" {
Users[id] = &UserRole{Rank: rank, CurrentRole: ""} logrus.Info("Remove role for ", id)
} removeRole(discordClient, guild, id, user.CurrentRole, dryrun)
if rank == 1 { Users[id] = &UserRole{Rank: rank, CurrentRole: ""}
logrus.Info("Add role `top1` for ", id) }
addRole(discordClient, guild, id, rolesID["top1"], dryrun) if rank == 1 {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]} logrus.Info("Add role `top1` for ", id)
} else if rank <= 10 { addRole(discordClient, guild, id, rolesID["top1"], dryrun)
logrus.Info("Add role `top10` for ", id) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]}
addRole(discordClient, guild, id, rolesID["top10"], dryrun) } else if rank <= 10 {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]} logrus.Info("Add role `top10` for ", id)
} else if rank <= 50 { addRole(discordClient, guild, id, rolesID["top10"], dryrun)
logrus.Info("Add role `top50` for ", id) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]}
addRole(discordClient, guild, id, rolesID["top50"], dryrun) } else if rank <= 50 {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} 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 { } else {
logrus.Error("ID not found", id) logrus.Error("ID not found", id)
} }
if rank == 1 { _, exist := Users[id]
logrus.Info("Add role `top1` for ", id) if exist {
addRole(discordClient, guild, id, rolesID["top1"], dryrun) if rank == 1 {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]} logrus.Info("Add role `top1` for ", id)
} else if rank <= 10 { addRole(discordClient, guild, id, rolesID["top1"], dryrun)
logrus.Info("Add role `top10` for ", id) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top1"]}
addRole(discordClient, guild, id, rolesID["top10"], dryrun) } else if rank <= 10 {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]} logrus.Info("Add role `top10` for ", id)
} else if rank <= 50 { addRole(discordClient, guild, id, rolesID["top10"], dryrun)
logrus.Info("Add role `top50` for ", id) Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top10"]}
addRole(discordClient, guild, id, rolesID["top50"], dryrun) } else if rank <= 50 {
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]} logrus.Info("Add role `top50` for ", id)
addRole(discordClient, guild, id, rolesID["top50"], dryrun)
Users[id] = &UserRole{Rank: rank, CurrentRole: rolesID["top50"]}
}
} }
} }
return nil return nil