From b8090d9a528adbe9477f3881613e7c85f26aed04 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sat, 31 Jul 2021 01:25:32 +0200 Subject: [PATCH] Add user protection to cleanchannel module Signed-off-by: Knut Ahlers --- mod_clearChannel.go | 36 +++++++++++++++++++++++++++++++++++- wiki/Home.md | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mod_clearChannel.go b/mod_clearChannel.go index 19ca4a6..fb12086 100644 --- a/mod_clearChannel.go +++ b/mod_clearChannel.go @@ -5,6 +5,7 @@ import ( "strconv" "time" + "github.com/Luzifer/go_helpers/v2/str" "github.com/bwmarrin/discordgo" "github.com/pkg/errors" "github.com/robfig/cron/v3" @@ -46,7 +47,10 @@ func (m *modClearChannel) Initialize(crontab *cron.Cron, discord *discordgo.Sess func (m modClearChannel) cronClearChannel() { var ( - after = "0" + after = "0" + err error + onlyUsers []string + protectUsers []string // @attr discord_channel_id required string "" ID of the Discord channel to clean up channelID = m.attrs.MustString("discord_channel_id", nil) @@ -54,6 +58,26 @@ func (m modClearChannel) cronClearChannel() { retention = m.attrs.MustDuration("retention", nil) ) + // @attr only_users optional []string "[]" When this list contains user IDs, only posts authored by those IDs will be deleted + onlyUsers, err = m.attrs.StringSlice("only_users") + switch err { + case nil, errValueNotSet: + // This is fine + default: + log.WithError(err).Error("Unable to load value for only_users") + return + } + + // @attr protect_users optional []string "[]" When this list contains user IDs, posts authored by those IDs will not be deleted + protectUsers, err = m.attrs.StringSlice("protect_users") + switch err { + case nil, errValueNotSet: + // This is fine + default: + log.WithError(err).Error("Unable to load value for protect_users") + return + } + for { msgs, err := m.discord.ChannelMessages(channelID, 100, "", after, "") if err != nil { @@ -83,6 +107,16 @@ func (m modClearChannel) cronClearChannel() { break } + if len(onlyUsers) > 0 && !str.StringInSlice(msg.Author.ID, onlyUsers) { + // Is not written by one of the users we may purge + continue + } + + if len(protectUsers) > 0 && str.StringInSlice(msg.Author.ID, protectUsers) { + // Is written by protected user, we may not purge + continue + } + if err = m.discord.ChannelMessageDelete(channelID, msg.ID); err != nil { log.WithError(err).Error("Unable to delete messages") return diff --git a/wiki/Home.md b/wiki/Home.md index da911a7..f400996 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -25,6 +25,8 @@ Cleans up old messages from a channel (for example announcement channel) which a | `discord_channel_id` | ✅ | string | | ID of the Discord channel to clean up | | `retention` | ✅ | duration | | How long to keep messages in this channel | | `cron` | | string | `0 * * * *` | When to execute the cleaner | +| `only_users` | | []string | `[]` | When this list contains user IDs, only posts authored by those IDs will be deleted | +| `protect_users` | | []string | `[]` | When this list contains user IDs, posts authored by those IDs will not be deleted | ## Type: `liveposting`