feat(xp): basic reducer

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-15 18:47:52 +02:00
parent c1e4653a5c
commit 2aaeb2115b
No known key found for this signature in database
GPG key ID: CF4550297832A29F
3 changed files with 13 additions and 1 deletions

View file

@ -30,6 +30,7 @@ func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
return return
} }
c := GetCopaing(m.Author.ID, m.GuildID) c := GetCopaing(m.Author.ID, m.GuildID)
onLastEventUpdate(c)
// add xp // add xp
trimmed := utils.TrimMessage(strings.ToLower(m.Content)) trimmed := utils.TrimMessage(strings.ToLower(m.Content))
m.Member.User = m.Author m.Member.User = m.Author
@ -62,6 +63,7 @@ func OnVoiceUpdate(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
if e.Member.User.Bot { if e.Member.User.Bot {
return return
} }
onLastEventUpdate(GetCopaing(e.UserID, e.GuildID))
cfg := config.GetGuildConfig(e.GuildID) cfg := config.GetGuildConfig(e.GuildID)
if cfg.IsDisabled(e.ChannelID) { if cfg.IsDisabled(e.ChannelID) {
return return

View file

@ -42,3 +42,10 @@ func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) {
} }
} }
} }
func onLastEventUpdate(c *Copaing) {
h := c.HourSinceLastEvent()
c.XP -= Lose(h, c.XP)
c.Save()
c.SetLastEvent()
}

View file

@ -2,6 +2,7 @@ package xp
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"github.com/anhgelus/gokord" "github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils" "github.com/anhgelus/gokord/utils"
@ -77,7 +78,9 @@ func (c *Copaing) HourSinceLastEvent() uint {
} }
u := c.GetUserBase() u := c.GetUserBase()
res := client.Get(context.Background(), fmt.Sprintf("%s:%s", u.GenKey(), LastEvent)) res := client.Get(context.Background(), fmt.Sprintf("%s:%s", u.GenKey(), LastEvent))
if res.Err() != nil { if errors.Is(res.Err(), redis.Nil) {
return 0
} else if res.Err() != nil {
utils.SendAlert("xp/member.go - Getting last event", res.Err().Error(), "base_key", u.GenKey()) utils.SendAlert("xp/member.go - Getting last event", res.Err().Error(), "base_key", u.GenKey())
return 0 return 0
} }