Milen/level/roles.go

59 lines
1.1 KiB
Go

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
}
if member.User.Bot {
return
}
OuterLoop:
for i := 0; i <= int(rank); i++ {
if i >= len(Roles) {
return
}
for _, v := range member.Roles {
if v == Roles[i] {
continue OuterLoop
}
}
err := dg.GuildMemberRoleAdd(guildID, userID, Roles[i])
if err != nil {
util.ReportError(dg, err)
return
}
}
}