feat(xp): xp and level gain
This commit is contained in:
parent
1f68e69899
commit
1078cf3deb
7 changed files with 108 additions and 3 deletions
40
xp/events.go
Normal file
40
xp/events.go
Normal 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))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue