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))
}

9
xp/level.go Normal file
View file

@ -0,0 +1,9 @@
package xp
import (
"github.com/bwmarrin/discordgo"
)
func onNewLevel(s *discordgo.Session, level uint) {
// check roles
}

22
xp/member.go Normal file
View file

@ -0,0 +1,22 @@
package xp
import (
"github.com/anhgelus/gokord"
"gorm.io/gorm"
)
type Copaing struct {
gorm.Model
DiscordID string
XP uint
GuildID string
}
func (c *Copaing) Load() *Copaing {
gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c)
return c
}
func (c *Copaing) Save() {
gokord.DB.Save(c)
}