44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package commands
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"PermissionGacha/modules/config"
|
|
"PermissionGacha/modules/level"
|
|
)
|
|
|
|
func handleLevelCommand(s *discordgo.Session, args []string, m *discordgo.MessageCreate) error {
|
|
levels, rawXP, err := level.GetLevelAndXP(m.Author.ID)
|
|
if err != nil {
|
|
return fmt.Errorf("commands: handleLevelCommand: error while getting level and XP: %w", err)
|
|
}
|
|
randomSource := make([]byte, 1)
|
|
rand.Read(randomSource)
|
|
xp := rawXP - (rawXP % 100) + ((int(randomSource[0]) + 128) % 100)
|
|
levelUpRequirement := level.GetLevelUpRequirement(levels)
|
|
if xp > levelUpRequirement {
|
|
xp -= 100
|
|
}
|
|
if levels < len(config.LevelRoles) {
|
|
s.ChannelMessageSend(
|
|
m.ChannelID,
|
|
fmt.Sprintf(
|
|
"<@%s> is now level %d. (%d XP/%d XP) [%.1f%%]",
|
|
m.Author.ID, levels, xp, levelUpRequirement, float64(xp)/float64(levelUpRequirement)*100,
|
|
),
|
|
)
|
|
} else {
|
|
s.ChannelMessageSend(
|
|
m.ChannelID,
|
|
fmt.Sprintf(
|
|
"<@%s> is now level %d. (%d XP) [MAX LEVEL]",
|
|
m.Author.ID, levels, xp,
|
|
),
|
|
)
|
|
}
|
|
return nil
|
|
}
|