aboutsummaryrefslogtreecommitdiff
path: root/xp
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <anhgelus.morhtuuzh@proton.me>2024-04-16 15:23:15 +0200
committerAnhgelus Morhtuuzh <anhgelus.morhtuuzh@proton.me>2024-04-16 15:23:15 +0200
commitef39c156f94b854796bb1c2adddfd40a5acaeca8 (patch)
treeac24922c2b02f4d17c66bfaabc16aee1f13fd7c9 /xp
parentc3dc81bd29318a4916bf8fc383a0d1a5ca90fa64 (diff)
feat(reducer): role update on new level
Diffstat (limited to 'xp')
-rw-r--r--xp/events.go4
-rw-r--r--xp/level.go30
2 files changed, 29 insertions, 5 deletions
diff --git a/xp/events.go b/xp/events.go
index 655e9fd..ebb7f40 100644
--- a/xp/events.go
+++ b/xp/events.go
@@ -30,7 +30,7 @@ func OnMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}
c := GetCopaing(m.Author.ID, m.GuildID)
- LastEventUpdate(c)
+ LastEventUpdate(s, c)
// add xp
trimmed := utils.TrimMessage(strings.ToLower(m.Content))
m.Member.User = m.Author
@@ -63,7 +63,7 @@ func OnVoiceUpdate(s *discordgo.Session, e *discordgo.VoiceStateUpdate) {
if e.Member.User.Bot {
return
}
- LastEventUpdate(GetCopaing(e.UserID, e.GuildID))
+ LastEventUpdate(s, GetCopaing(e.UserID, e.GuildID))
cfg := config.GetGuildConfig(e.GuildID)
if cfg.IsDisabled(e.ChannelID) {
return
diff --git a/xp/level.go b/xp/level.go
index bbcc71f..12e5a28 100644
--- a/xp/level.go
+++ b/xp/level.go
@@ -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()
l := Lose(h, c.XP)
xp := c.XPAlreadyRemoved()
@@ -61,11 +77,15 @@ func LastEventUpdate(c *Copaing) {
}
if oldXP != c.XP {
c.Save()
+ lvl := Level(c.XP)
+ if Level(oldXP) != Level(c.XP) {
+ c.OnNewLevel(s, lvl)
+ }
}
c.SetLastEvent()
}
-func XPUpdate(c *Copaing) {
+func XPUpdate(s *discordgo.Session, c *Copaing) {
oldXP := c.XP
if oldXP == 0 {
return
@@ -89,6 +109,10 @@ func XPUpdate(c *Copaing) {
if oldXP != c.XP {
utils.SendDebug("Save XP", "old", oldXP, "new", c.XP, "user", c.DiscordID)
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() {
// utils.SendDebug("Async reducer", "user", m.DisplayName(), "guild", g.Name)
// c := GetCopaing(m.User.ID, g.ID)
-// XPUpdate(c)
+// XPUpdate(s, c)
// wg.Done()
// }()
// }