Milen/level/roles.go

52 lines
1.0 KiB
Go

2020-05-30 14:50:26 +07:00
package level
import (
"time"
"github.com/bwmarrin/discordgo"
"gitea.teamortix.com/chanbakjsd/Milen/db"
"gitea.teamortix.com/chanbakjsd/Milen/util"
)
var Roles []string
var ShouldCheck = make(map[string]map[string]bool)
func QueueRoles(dg *discordgo.Session) {
for {
time.Sleep(5 * time.Second)
for guildID, v := range ShouldCheck {
for userID, _ := range v {
rank, _ := GetLevelFromXP(db.GetXP(userID))
AssignRoles(dg, guildID, userID, rank)
delete(v, userID)
}
}
}
}
func AssignRoles(dg *discordgo.Session, guildID string, userID string, rank Rank) {
member, err := util.GetMember(dg, guildID, userID)
if err != nil {
util.ReportError(dg, err)
return
}
//FIXME: Hack. To prevent someone who isn't part of the server yet to be assigned a role.
if len(member.Roles) == 0 {
return
}
2020-05-30 14:50:26 +07:00
OuterLoop:
for i := 0; i <= int(rank); i++ {
if i >= len(Roles) {
return
}
for _, v := range member.Roles {
if v == Roles[i] {
continue OuterLoop
}
}
dg.GuildMemberRoleAdd(guildID, userID, Roles[i])
}
}