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("levels.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 { levelMutex.Lock() for k, v := range levelQueue { if !v { continue } levelQueue[k] = false level, _ := getLevelAndXP(dg, k) if assignedLevels[k] != level { assignedLevels[k] = level err := dg.GuildMemberRoleAdd(guildID, k, rolesFromLevel[level-1]) if err != nil { logError(dg, "assignlevel", err) } } } levelMutex.Unlock() time.Sleep(5 * time.Second) } }