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

52 lines
992 B
Go

package main
import (
2019-11-18 14:38:03 +07:00
"fmt"
"io/ioutil"
"strings"
"sync"
"time"
"github.com/bwmarrin/discordgo"
)
const guildID = "645550569573842945"
var levelMutex sync.Mutex
var levelQueue = make(map[string]bool)
var assignedLevels = make(map[string]int)
var rolesFromLevel []string
func init() {
2019-11-18 14:38:03 +07:00
levels, _ := ioutil.ReadFile("roles.txt")
rolesFromLevel = strings.Split(string(levels), "\n")
for k, v := range rolesFromLevel {
rolesFromLevel[k] = strings.TrimSpace(v)
}
}
func addToLevelQueue(discordID string) {
levelMutex.Lock()
defer levelMutex.Unlock()
levelQueue[discordID] = true
}
func listenToLevelUpdate(dg *discordgo.Session) {
for {
2019-11-18 14:38:03 +07:00
time.Sleep(5 * time.Second)
levelMutex.Lock()
for k, v := range levelQueue {
if !v {
continue
}
levelQueue[k] = false
level, _ := getLevelAndXP(dg, k)
if assignedLevels[k] != level {
assignedLevels[k] = level
2019-11-18 14:38:03 +07:00
go checkForGachaEligibility(dg, k, rolesFromLevel[level-1])
}
}
levelMutex.Unlock()
}
}