feat(xp): xp and level gain

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-14 17:11:31 +02:00
parent 1f68e69899
commit 1078cf3deb
No known key found for this signature in database
GPG key ID: CF4550297832A29F
7 changed files with 108 additions and 3 deletions

40
xp/events.go Normal file
View file

@ -0,0 +1,40 @@
package xp
import (
"github.com/anhgelus/gokord/utils"
"github.com/bwmarrin/discordgo"
)
func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
c := Copaing{DiscordID: m.Author.ID, GuildID: m.GuildID}
c.Load()
// add xp
pastLevel := Level(c.XP)
trimmed := utils.TrimMessage(m.Content)
c.XP += XPMessage(uint(len(trimmed)), calcDiversity(trimmed))
c.Save()
newLevel := Level(c.XP)
// handle new level
if pastLevel < newLevel {
if err := s.MessageReactionAdd(m.ChannelID, m.Message.ID, "⬆"); err != nil {
utils.SendAlert("xp/events.go - reaction add new level", "cannot add the reaction: "+err.Error())
}
onNewLevel(s, newLevel)
}
}
func calcDiversity(msg string) uint {
var chars []rune
for _, c := range []rune(msg) {
toAdd := true
for _, ch := range chars {
if ch == c {
toAdd = false
}
}
if toAdd {
chars = append(chars, c)
}
}
return uint(len(chars))
}