2019-11-27 10:40:31 +07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
|
|
|
"PermissionGacha/db"
|
2019-11-27 12:06:42 +07:00
|
|
|
"PermissionGacha/modules/config"
|
2019-11-27 10:40:31 +07:00
|
|
|
"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)
|
|
|
|
}
|
2019-11-27 12:06:42 +07:00
|
|
|
newPrestigeLevel := xp/config.PrestigeRequirement + 1
|
2019-11-27 10:40:31 +07:00
|
|
|
|
|
|
|
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,
|
2019-11-27 12:06:42 +07:00
|
|
|
nextNearestLogicalPrestige, float64(xp)/float64(config.PrestigeRequirement*(nextNearestLogicalPrestige-1))*100,
|
2019-11-27 10:40:31 +07:00
|
|
|
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
|
|
|
|
}
|
2019-11-27 12:06:42 +07:00
|
|
|
newPrestigeLevel := xp/config.PrestigeRequirement + 1
|
2019-11-27 10:40:31 +07:00
|
|
|
go roles.RemoveAllLevelRoles(s, m.UserID)
|
|
|
|
err = db.SetPrestigeMultiplier(m.UserID, newPrestigeLevel)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(s, "checkPrestigeReact", "SetPrestigeMultiplier", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.ChannelMessageSend(m.ChannelID,
|
|
|
|
fmt.Sprintf(
|
|
|
|
"**PRESTIGE > ** <@%s>, you have prestiged!", m.UserID,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|