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(checkID string, newLevel int) { oldLevel, err := roles.GetOldLevel(s, checkID) if err != nil { log.Error(s, err) return } if newLevel > oldLevel { gacha.RollReward(s, checkID, newLevel-oldLevel) roles.GiveLevelRoles(s, checkID, oldLevel, newLevel) } }(id, level) } } levelMutex.Unlock() } }