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

33 lines
838 B
Go

package commands
import (
"crypto/rand"
"fmt"
"github.com/bwmarrin/discordgo"
"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
}
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,
),
)
return nil
}