Compare commits

..

2 commits

Author SHA1 Message Date
bbfaaaf04b
Merge branch 'main' into refactor/config-command 2025-08-21 13:26:17 +02:00
026abcc07a
feat(xp): increase precision of timestamp
seems like to fix periodic reducer not working
2025-08-18 13:56:32 +02:00
5 changed files with 30 additions and 40 deletions

1
.gitignore vendored
View file

@ -26,3 +26,4 @@ go.work
tmp
config/**.toml
data
docker-compose.yml

View file

@ -1,22 +0,0 @@
services:
bot:
build: .
restart: always
env_file:
- .env
volumes:
- ./config:/app/config
depends_on:
- postgres
postgres:
image: postgres:alpine
env_file:
- .env
volumes:
- ./data:/var/lib/postgresql/data
adminer:
image: docker.io/adminer
ports:
- "8080:8080"
depends_on:
- postgres

View file

@ -2,12 +2,13 @@ package exp
import (
"fmt"
"github.com/anhgelus/gokord"
"math"
"regexp"
"slices"
"strings"
"time"
"github.com/anhgelus/gokord"
)
func MessageXP(length uint, diversity uint) uint {
@ -50,14 +51,14 @@ func LevelXP(level uint) uint {
// TimeStampNDaysBefore returns the timestamp (year-month-day) n days before today
func TimeStampNDaysBefore(n uint) string {
var y, d int
var m time.Month
var unix time.Time
if gokord.Debug {
y, m, d = time.Unix(time.Now().Unix()-int64(24*60*60), 0).Date() // reduce time for debug
unix = time.Unix(time.Now().Unix()-int64(n), 0) // reduce time for debug
} else {
y, m, d = time.Unix(time.Now().Unix()-int64(n*24*60*60), 0).Date()
unix = time.Unix(time.Now().Unix()-int64(n*24*60*60), 0)
}
return fmt.Sprintf("%d-%d-%d", y, m, d)
unix = unix.UTC()
return fmt.Sprintf("%d-%d-%d %d:%d:%d UTC", unix.Year(), unix.Month(), unix.Day(), unix.Hour(), unix.Minute(), unix.Second())
}
func TrimMessage(s string) string {

17
main.go
View file

@ -4,16 +4,17 @@ import (
_ "embed"
"errors"
"flag"
"os"
"time"
"github.com/anhgelus/gokord"
cmd "github.com/anhgelus/gokord/cmd"
"github.com/anhgelus/gokord/cmd"
"github.com/anhgelus/gokord/logger"
"github.com/anhgelus/les-copaings-bot/commands"
"github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/user"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"os"
"time"
)
var (
@ -117,7 +118,15 @@ func main() {
creditsCmd,
},
AfterInit: func(dg *discordgo.Session) {
stopPeriodicReducer = gokord.NewTimer(24*time.Hour, func(stop chan<- interface{}) {
d := 24 * time.Hour
if gokord.Debug {
d = 24 * time.Second
}
user.PeriodicReducer(dg)
stopPeriodicReducer = gokord.NewTimer(d, func(stop chan<- interface{}) {
logger.Debug("Periodic reducer")
user.PeriodicReducer(dg)
})
},

View file

@ -1,14 +1,15 @@
package user
import (
"slices"
"sync"
"time"
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/logger"
"github.com/anhgelus/les-copaings-bot/config"
"github.com/anhgelus/les-copaings-bot/exp"
"github.com/bwmarrin/discordgo"
"slices"
"sync"
"time"
)
func onNewLevel(dg *discordgo.Session, m *discordgo.Member, level uint) {
@ -86,14 +87,14 @@ func PeriodicReducer(dg *discordgo.Session) {
go func() {
defer wg.Done()
cfg := config.GetGuildConfig(g.ID)
err := gokord.DB.
res := gokord.DB.
Model(&CopaingXP{}).
Where("guild_id = ? and created_at < ?", g.ID, exp.TimeStampNDaysBefore(cfg.DaysXPRemains)).
Delete(&CopaingXP{}).
Error
if err != nil {
logger.Alert("user/level.go - Removing old XP", err.Error(), "guild_id", g.ID)
Delete(&CopaingXP{})
if res.Error != nil {
logger.Alert("user/level.go - Removing old XP", res.Error.Error(), "guild_id", g.ID)
}
logger.Debug("Guild cleaned", "guild", g.Name, "rows affected", res.RowsAffected)
}()
}
wg.Wait()