2019-11-27 10:40:31 +07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
2019-12-13 10:52:06 +07:00
|
|
|
"PermissionGacha/modules/config"
|
2019-11-27 10:40:31 +07:00
|
|
|
"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)
|
2019-12-12 06:13:43 +07:00
|
|
|
if xp > levelUpRequirement {
|
|
|
|
xp -= 100
|
|
|
|
}
|
2019-12-13 10:52:06 +07:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2019-11-27 10:40:31 +07:00
|
|
|
return nil
|
|
|
|
}
|