package level import ( "sync" "time" "github.com/bwmarrin/discordgo" "PermissionGacha/modules/gacha" "PermissionGacha/modules/log" "PermissionGacha/modules/roles" ) var ( levelMutex sync.Mutex levelQueue = make(map[string]bool) lastKnownLevel = make(map[string]int) ) func QueueCheck(discordID string) { levelMutex.Lock() defer levelMutex.Unlock() levelQueue[discordID] = true } func ScheduleUpdates(s *discordgo.Session) { for { time.Sleep(5 * time.Second) levelMutex.Lock() for id, isQueued := range levelQueue { if !isQueued { continue } delete(levelQueue, id) level, _, err := GetLevelAndXP(id) if err != nil { log.Error(s, err) continue } if lastKnownLevel[id] != level { lastKnownLevel[id] = level go func() { oldLevel, err := roles.GetOldLevel(s, id) if err != nil { log.Error(s, err) return } if level > oldLevel { gacha.RollReward(s, id, level-oldLevel) roles.GiveLevelRoles(s, id, oldLevel, level) } }() } } levelMutex.Unlock() } }