This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
PermissionGacha/modules/commands/prestige.go

103 lines
3.1 KiB
Go

package commands
import (
"fmt"
"math"
"time"
"github.com/bwmarrin/discordgo"
"PermissionGacha/db"
"PermissionGacha/modules/config"
"PermissionGacha/modules/level"
"PermissionGacha/modules/log"
"PermissionGacha/modules/roles"
)
const emojiCheck = "✅"
var prestigeList = make(map[string]string)
func handlePrestigeCommand(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error {
//TODO: Factorize into backend parts
currentPrestigeLevel, err := db.GetPrestigeMultiplier(m.Author.ID)
if err != nil {
return fmt.Errorf("commands: handlePrestigeCommand: error while getting current prestige level: %w", err)
}
xp, err := db.GetXP(m.Author.ID)
if err != nil {
return fmt.Errorf("commands: handlePrestigeCommand: error while getting current XP: %w", err)
}
newPrestigeLevel := xp/config.PrestigeRequirement + 1
currentLevel, _ := level.ConvertXPIntoLevel(xp)
if currentLevel < 5 && currentPrestigeLevel == 1 {
s.ChannelMessageSend(m.ChannelID, "**PRESTIGE > **Prestige is only unlocked after level 5!")
return nil
}
var lowerLevelWarning string
if newPrestigeLevel <= currentPrestigeLevel {
lowerLevelWarning = "**WARNING:** *Your new prestige multiplier is __NOT__ higher than your current prestige multiplier.*"
}
nearestPowerOfTen := math.Pow10(int(math.Floor(math.Log10(float64(newPrestigeLevel)))))
nextNearestLogicalPrestige := int((math.Floor(float64(newPrestigeLevel)/nearestPowerOfTen) + 1) * nearestPowerOfTen)
msg, err := s.ChannelMessageSend(
m.ChannelID,
fmt.Sprintf(
"<@%s>, here's your prestige multiplier:\n**Now: **__%dx__\n**New: **__%dx__\n**Next: **__%dx__ [%.1f%%]\n%s\n"+
"React with :white_check_mark: to proceed with prestiging.",
m.Author.ID,
currentPrestigeLevel,
newPrestigeLevel,
nextNearestLogicalPrestige, float64(xp)/float64(config.PrestigeRequirement*(nextNearestLogicalPrestige-1))*100,
lowerLevelWarning,
),
)
if err != nil {
return fmt.Errorf("commands: handlePrestigeCommand: error while sending message: %w", err)
}
prestigeList[msg.ID] = m.Author.ID
err = s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
if err != nil {
return fmt.Errorf("commands: handlePrestigeCommand: error while adding reaction: %w", err)
}
time.Sleep(15 * time.Second)
delete(prestigeList, msg.ID)
err = s.MessageReactionsRemoveAll(msg.ChannelID, msg.ID)
if err != nil {
return fmt.Errorf("commands: handlePrestigeCommand: error while removing reaction: %w", err)
}
return nil
}
func ListenPrestigeReact(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
if prestigeList[m.MessageID] != m.UserID {
return
}
if m.Emoji.Name != emojiCheck {
return
}
xp, err := db.GetXP(m.UserID)
if err != nil {
log.Error(s, "checkPrestigeReact", "GetXP", err)
return
}
newPrestigeLevel := xp/config.PrestigeRequirement + 1
go roles.RemoveAllLevelRoles(s, m.UserID)
err = db.SetPrestigeMultiplier(m.UserID, newPrestigeLevel)
if err != nil {
log.Error(s, "checkPrestigeReact", "SetPrestigeMultiplier", err)
return
}
level.ClearCache(m.UserID)
s.ChannelMessageSend(m.ChannelID,
fmt.Sprintf(
"**PRESTIGE > ** <@%s>, you have prestiged!", m.UserID,
),
)
}