package main import ( "sync" "github.com/bwmarrin/discordgo" ) var prestigeCache = make(map[string]int) var prestigeMutex = sync.Mutex{} func setPrestige(dg *discordgo.Session, discordID string, newMultiplier int) { prestigeMutex.Lock() defer prestigeMutex.Unlock() tx, err := db.Begin() if err != nil { logError(dg, "setPrestige", "begin", discordID, newMultiplier, err) return } _, err = tx.Exec("REPLACE INTO prestige(id, multiplier) VALUES(?,?)", discordID, newMultiplier) if err != nil { logError(dg, "setPrestige", "insertMultiplier", discordID, newMultiplier, err) tx.Rollback() return } _, err = tx.Exec("REPLACE INTO xp(id, xp) VALUES(?,0)", discordID) err = tx.Commit() if err != nil { logError(dg, "setPrestige", "insertXP", discordID, newMultiplier, err) tx.Rollback() return } if err != nil { logError(dg, "setPrestige", "commit", discordID, newMultiplier, err) return } prestigeCache[discordID] = newMultiplier } func getPrestige(dg *discordgo.Session, discordID string) int { prestigeMutex.Lock() defer prestigeMutex.Unlock() if prestigeCache[discordID] > 0 { return prestigeCache[discordID] } rows, err := db.Query("SELECT multiplier FROM prestige WHERE id=?", discordID) if err != nil { logError(dg, "getPrestige", "select", discordID, err) return 0 } defer rows.Close() if rows.Next() { var multiplier int rows.Scan(&multiplier) prestigeCache[discordID] = multiplier return multiplier } prestigeCache[discordID] = 1 return 1 } func cleanUpRole(s *discordgo.Session, discordID string) { member, err := s.GuildMember(guildID, discordID) if err != nil { logError(s, "cleanUpRole", "guildMember", discordID) return } for _, v := range member.Roles { for _, w := range rolesFromLevel { if v == w { s.GuildMemberRoleRemove(guildID, discordID, v) break } } } }