feat(reducer): role update on new level

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-16 15:23:15 +02:00
parent c3dc81bd29
commit ef39c156f9
No known key found for this signature in database
GPG key ID: CF4550297832A29F
4 changed files with 32 additions and 8 deletions

View file

@ -10,7 +10,7 @@ import (
func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) { func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
optMap := utils.GenerateOptionMap(i) optMap := utils.GenerateOptionMap(i)
c := xp.GetCopaing(i.Member.User.ID, i.GuildID) // current copaing = member who used /rank c := xp.GetCopaing(i.Member.User.ID, i.GuildID) // current copaing = member who used /rank
xp.LastEventUpdate(c) // update xp and reset last event xp.LastEventUpdate(s, c) // update xp and reset last event
msg := "Votre niveau" msg := "Votre niveau"
m := i.Member m := i.Member
var err error var err error
@ -41,7 +41,7 @@ func Rank(s *discordgo.Session, i *discordgo.InteractionCreate) {
} }
c.DiscordID = u.ID // current copaing = member targeted by member who wrote /rank c.DiscordID = u.ID // current copaing = member targeted by member who wrote /rank
c.Load() // reload copaing (change line before) c.Load() // reload copaing (change line before)
xp.XPUpdate(c) // update xp without resetting event xp.XPUpdate(s, c) // update xp without resetting event
msg = fmt.Sprintf("Le niveau de %s", m.DisplayName()) msg = fmt.Sprintf("Le niveau de %s", m.DisplayName())
} }
lvl := xp.Level(c.XP) lvl := xp.Level(c.XP)

View file

@ -9,7 +9,7 @@ import (
) )
func Top(s *discordgo.Session, i *discordgo.InteractionCreate) { func Top(s *discordgo.Session, i *discordgo.InteractionCreate) {
xp.LastEventUpdate(xp.GetCopaing(i.User.ID, i.GuildID)) xp.LastEventUpdate(s, xp.GetCopaing(i.User.ID, i.GuildID))
resp := utils.ResponseBuilder{C: s, I: i} resp := utils.ResponseBuilder{C: s, I: i}
err := resp.IsDeferred().Send() err := resp.IsDeferred().Send()
if err != nil { if err != nil {

View file

@ -30,7 +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)
LastEventUpdate(c) LastEventUpdate(s, 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
@ -63,7 +63,7 @@ func OnVoiceUpdate(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
if e.Member.User.Bot { if e.Member.User.Bot {
return return
} }
LastEventUpdate(GetCopaing(e.UserID, e.GuildID)) LastEventUpdate(s, 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

@ -43,7 +43,23 @@ func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) {
} }
} }
func LastEventUpdate(c *Copaing) { func (c *Copaing) OnNewLevel(s *discordgo.Session, level uint) {
m, err := s.GuildMember(c.GuildID, c.DiscordID)
if err != nil {
utils.SendAlert(
"xp/level.go - Getting member for new level",
err.Error(),
"discord_id",
c.DiscordID,
"guild_id",
c.GuildID,
)
return
}
onNewLevel(s, m, level)
}
func LastEventUpdate(s *discordgo.Session, c *Copaing) {
h := c.HourSinceLastEvent() h := c.HourSinceLastEvent()
l := Lose(h, c.XP) l := Lose(h, c.XP)
xp := c.XPAlreadyRemoved() xp := c.XPAlreadyRemoved()
@ -61,11 +77,15 @@ func LastEventUpdate(c *Copaing) {
} }
if oldXP != c.XP { if oldXP != c.XP {
c.Save() c.Save()
lvl := Level(c.XP)
if Level(oldXP) != Level(c.XP) {
c.OnNewLevel(s, lvl)
}
} }
c.SetLastEvent() c.SetLastEvent()
} }
func XPUpdate(c *Copaing) { func XPUpdate(s *discordgo.Session, c *Copaing) {
oldXP := c.XP oldXP := c.XP
if oldXP == 0 { if oldXP == 0 {
return return
@ -89,6 +109,10 @@ func XPUpdate(c *Copaing) {
if oldXP != c.XP { if oldXP != c.XP {
utils.SendDebug("Save XP", "old", oldXP, "new", c.XP, "user", c.DiscordID) utils.SendDebug("Save XP", "old", oldXP, "new", c.XP, "user", c.DiscordID)
c.Save() c.Save()
lvl := Level(c.XP)
if Level(oldXP) != Level(c.XP) {
c.OnNewLevel(s, lvl)
}
} }
} }
@ -103,7 +127,7 @@ func XPUpdate(c *Copaing) {
// go func() { // go func() {
// utils.SendDebug("Async reducer", "user", m.DisplayName(), "guild", g.Name) // utils.SendDebug("Async reducer", "user", m.DisplayName(), "guild", g.Name)
// c := GetCopaing(m.User.ID, g.ID) // c := GetCopaing(m.User.ID, g.ID)
// XPUpdate(c) // XPUpdate(s, c)
// wg.Done() // wg.Done()
// }() // }()
// } // }