From de464080689788cf062da9e077b7bf4751e1602d Mon Sep 17 00:00:00 2001 From: Jose Rojo <6jarv91@gmail.com> Date: Fri, 12 Apr 2024 16:52:18 +0200 Subject: [PATCH] Message cleaning --- cmd/jarvbot/commands.go | 2 +- cmd/jarvbot/config.go | 4 +++- cmd/jarvbot/main.go | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/cmd/jarvbot/commands.go b/cmd/jarvbot/commands.go index 0484c80..4e53a54 100644 --- a/cmd/jarvbot/commands.go +++ b/cmd/jarvbot/commands.go @@ -46,7 +46,7 @@ func onMessageCreated(ctx context.Context) func(ds *discordgo.Session, mc *disco // the command key must be lowercased var commands = map[string]command{ // public - "!version": simpleTextResponse("v3.5.0"), + "!version": simpleTextResponse("v3.5.1"), "!source": simpleTextResponse("Source code: https://github.com/j4rv/discord-bot"), "!genshindailycheckin": answerGenshinDailyCheckIn, "!genshindailycheckinstop": answerGenshinDailyCheckInStop, diff --git a/cmd/jarvbot/config.go b/cmd/jarvbot/config.go index 41c7bf0..8c62564 100644 --- a/cmd/jarvbot/config.go +++ b/cmd/jarvbot/config.go @@ -13,7 +13,7 @@ var warnMessageMinLength = 1 var warnMessageMaxLength = 320 const avatarTargetSize = "1024" -const maxMessageCount = 25 +const maxMessageCount = 40 // https://discord.com/branding const colorYellow = 0xFEE75C @@ -31,6 +31,8 @@ const timeoutDurationWhenMisfire = 8 * time.Minute const timeoutDurationWhenNuclearCatastrophe = 2 * time.Minute const backupCRON = "0 0 * * 1" +const cleanStateMessagesCRON = "*/10 * * * *" +const stateMessageMaxLifetime = 1 * time.Hour const dailyCheckInReminderCRON = "CRON_TZ=Asia/Shanghai 0 0 * * *" const dailyCheckInReminderMessage = `Remember to do the Daily Check-In! - Genshin: https://webstatic-sea.mihoyo.com/ys/event/signin-sea/index.html?act_id=e202102251931481 diff --git a/cmd/jarvbot/main.go b/cmd/jarvbot/main.go index a752b75..1781118 100644 --- a/cmd/jarvbot/main.go +++ b/cmd/jarvbot/main.go @@ -8,6 +8,7 @@ import ( "os/signal" "strings" "syscall" + "time" "github.com/bwmarrin/discordgo" "github.com/jmoiron/sqlx" @@ -112,6 +113,7 @@ func initCRONs(ds *discordgo.Session) { initCron("backupCRON", backupCRON, backupCRONFunc(ds)) initCron("dailyCheckInCRON", dailyCheckInReminderCRON, dailyCheckInCRONFunc(ds)) + initCron("cleanStateMessagesCRON", cleanStateMessagesCRON, cleanStateMessagesCRONFunc(ds)) initCron("parametricCRON", parametricReminderCRON, parametricCRONFunc(ds)) initCron("playStoreCRON", playStoreReminderCRON, playStoreCRONFunc(ds)) initCron("react4RolesCRON", react4RolesCRON, react4RolesCRONFunc(ds)) @@ -150,6 +152,28 @@ func initSlashCommands(ds *discordgo.Session) func() { } } +func cleanStateMessagesCRONFunc(ds *discordgo.Session) func() { + return func() { + log.Println("Doing state message cleanup") + for _, pc := range ds.State.PrivateChannels { + cleanStateMessagesInChannel(ds, pc) + } + for _, gc := range ds.State.Guilds { + for _, gc := range gc.Channels { + cleanStateMessagesInChannel(ds, gc) + } + } + } +} + +func cleanStateMessagesInChannel(ds *discordgo.Session, channel *discordgo.Channel) { + for _, msg := range channel.Messages { + if msg.Timestamp.Before(time.Now().Add(-stateMessageMaxLifetime)) { + ds.State.MessageRemove(msg) + } + } +} + func diff(body, prefix string) string { lines := strings.Split(body, "\n") var formattedBody string