aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <anhgelus.morhtuuzh@proton.me>2024-04-16 15:24:25 +0200
committerAnhgelus Morhtuuzh <anhgelus.morhtuuzh@proton.me>2024-04-16 15:24:25 +0200
commit7fe68106bec7133981ac91026156977e0c7b4ca1 (patch)
tree3037ee9508fb52ca96137bfac0679a138d03f37f
parentef39c156f94b854796bb1c2adddfd40a5acaeca8 (diff)
feat(reducer): periodic reducer updating xp
-rw-r--r--main.go18
-rw-r--r--xp/level.go44
2 files changed, 33 insertions, 29 deletions
diff --git a/main.go b/main.go
index 1a9693a..d8cdbc2 100644
--- a/main.go
+++ b/main.go
@@ -3,10 +3,12 @@ package main
import (
"flag"
"github.com/anhgelus/gokord"
+ "github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/commands"
"github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/xp"
"github.com/bwmarrin/discordgo"
+ "time"
)
var token string
@@ -126,12 +128,12 @@ func afterInit(dg *discordgo.Session) {
dg.AddHandler(xp.OnLeave)
// setup timer for periodic reducer
- //d := 24 * time.Hour
- //if gokord.Debug {
- // // reduce for debug
- // d = time.Minute
- //}
- //utils.NewTimer(d, func(stop chan struct{}) {
- // xp.PeriodicReducer(dg)
- //})
+ d := 24 * time.Hour
+ if gokord.Debug {
+ // reduce time for debug
+ d = time.Minute
+ }
+ utils.NewTimer(d, func(stop chan struct{}) {
+ xp.PeriodicReducer(dg)
+ })
}
diff --git a/xp/level.go b/xp/level.go
index 12e5a28..bbe11a4 100644
--- a/xp/level.go
+++ b/xp/level.go
@@ -5,6 +5,8 @@ import (
"github.com/anhgelus/les-copaings-bot/config"
"github.com/bwmarrin/discordgo"
"slices"
+ "sync"
+ "time"
)
func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) {
@@ -116,24 +118,24 @@ func XPUpdate(s *discordgo.Session, c *Copaing) {
}
}
-//func PeriodicReducer(s *discordgo.Session) {
-// var wg sync.WaitGroup
-// for _, g := range s.State.Guilds {
-// for _, m := range utils.FetchGuildUser(s, g.ID) {
-// if m.User.Bot {
-// continue
-// }
-// wg.Add(1)
-// go func() {
-// utils.SendDebug("Async reducer", "user", m.DisplayName(), "guild", g.Name)
-// c := GetCopaing(m.User.ID, g.ID)
-// XPUpdate(s, c)
-// wg.Done()
-// }()
-// }
-// wg.Wait() // finish the entire guild before starting another
-// utils.SendDebug("Guild finished", "guild", g.Name)
-// time.Sleep(10 * time.Second) // sleep prevents from spamming the Discord API
-// }
-// utils.SendDebug("Periodic reduce finished", "len(guilds)", len(s.State.Guilds))
-//}
+func PeriodicReducer(s *discordgo.Session) {
+ var wg sync.WaitGroup
+ for _, g := range s.State.Guilds {
+ for _, m := range utils.FetchGuildUser(s, g.ID) {
+ if m.User.Bot {
+ continue
+ }
+ wg.Add(1)
+ go func() {
+ utils.SendDebug("Async reducer", "user", m.DisplayName(), "guild", g.Name)
+ c := GetCopaing(m.User.ID, g.ID)
+ XPUpdate(s, c)
+ wg.Done()
+ }()
+ }
+ wg.Wait() // finish the entire guild before starting another
+ utils.SendDebug("Guild finished", "guild", g.Name)
+ time.Sleep(10 * time.Second) // sleep prevents from spamming the Discord API
+ }
+ utils.SendDebug("Periodic reduce finished", "len(guilds)", len(s.State.Guilds))
+}