This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PermissionGacha/roles.go

54 lines
1.0 KiB
Go

package main
import (
"io/ioutil"
"strings"
"sync"
"time"
"github.com/bwmarrin/discordgo"
)
const guildID = "645550569573842945"
var roleMutex sync.Mutex
var roleQueue = make(map[string]bool)
var assignedRoles = make(map[string]int)
var roleFromLevel []string
func init() {
roles, _ := ioutil.ReadFile("roles.txt")
roleFromLevel = strings.Split(string(roles), "\n")
for k, v := range roleFromLevel {
roleFromLevel[k] = strings.TrimSpace(v)
}
}
func addToRoleQueue(discordID string) {
roleMutex.Lock()
defer roleMutex.Unlock()
roleQueue[discordID] = true
}
func assignRole(dg *discordgo.Session) {
for {
roleMutex.Lock()
for k, v := range roleQueue {
if !v {
continue
}
roleQueue[k] = false
level, _ := getLevelAndXP(dg, k)
if assignedRoles[k] != level {
assignedRoles[k] = level
err := dg.GuildMemberRoleAdd(guildID, k, roleFromLevel[level-1])
if err != nil {
logError(dg, "assignRole", err)
}
}
}
roleMutex.Unlock()
time.Sleep(5 * time.Second)
}
}