feat(reducer): periodic reducer updating xp

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-16 15:24:25 +02:00
parent ef39c156f9
commit 7fe68106be
No known key found for this signature in database
GPG key ID: CF4550297832A29F
2 changed files with 33 additions and 29 deletions

18
main.go
View file

@ -3,10 +3,12 @@ package main
import ( import (
"flag" "flag"
"github.com/anhgelus/gokord" "github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils"
"github.com/anhgelus/les-copaings-bot/commands" "github.com/anhgelus/les-copaings-bot/commands"
"github.com/anhgelus/les-copaings-bot/config" "github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/xp" "github.com/anhgelus/les-copaings-bot/xp"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"time"
) )
var token string var token string
@ -126,12 +128,12 @@ func afterInit(dg *discordgo.Session) {
dg.AddHandler(xp.OnLeave) dg.AddHandler(xp.OnLeave)
// setup timer for periodic reducer // setup timer for periodic reducer
//d := 24 * time.Hour d := 24 * time.Hour
//if gokord.Debug { if gokord.Debug {
// // reduce for debug // reduce time for debug
// d = time.Minute d = time.Minute
//} }
//utils.NewTimer(d, func(stop chan struct{}) { utils.NewTimer(d, func(stop chan struct{}) {
// xp.PeriodicReducer(dg) xp.PeriodicReducer(dg)
//}) })
} }

View file

@ -5,6 +5,8 @@ import (
"github.com/anhgelus/les-copaings-bot/config" "github.com/anhgelus/les-copaings-bot/config"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
"slices" "slices"
"sync"
"time"
) )
func onNewLevel(s *discordgo.Session, m *discordgo.Member, level uint) { 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) { func PeriodicReducer(s *discordgo.Session) {
// var wg sync.WaitGroup var wg sync.WaitGroup
// for _, g := range s.State.Guilds { for _, g := range s.State.Guilds {
// for _, m := range utils.FetchGuildUser(s, g.ID) { for _, m := range utils.FetchGuildUser(s, g.ID) {
// if m.User.Bot { if m.User.Bot {
// continue continue
// } }
// wg.Add(1) wg.Add(1)
// 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(s, c) XPUpdate(s, c)
// wg.Done() wg.Done()
// }() }()
// } }
// wg.Wait() // finish the entire guild before starting another wg.Wait() // finish the entire guild before starting another
// utils.SendDebug("Guild finished", "guild", g.Name) utils.SendDebug("Guild finished", "guild", g.Name)
// time.Sleep(10 * time.Second) // sleep prevents from spamming the Discord API time.Sleep(10 * time.Second) // sleep prevents from spamming the Discord API
// } }
// utils.SendDebug("Periodic reduce finished", "len(guilds)", len(s.State.Guilds)) utils.SendDebug("Periodic reduce finished", "len(guilds)", len(s.State.Guilds))
//} }