Milen/level/roles.go

59 lines
1.1 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 {
2020-05-31 03:23:23 +07:00
for userID := range v {
2020-05-30 14:50:26 +07:00
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-31 04:10:45 +07:00
if member.User.Bot {
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
}
}
2020-05-31 03:23:23 +07:00
err := dg.GuildMemberRoleAdd(guildID, userID, Roles[i])
if err != nil {
util.ReportError(dg, err)
return
}
2020-05-30 14:50:26 +07:00
}
}