package main import ( "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() { 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 { 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 go checkForGachaEligibility(dg, k, rolesFromLevel[level-1]) } } levelMutex.Unlock() } }