Add user protection to cleanchannel module

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-07-31 01:25:32 +02:00
parent 8dc637e41e
commit b8090d9a52
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
2 changed files with 37 additions and 1 deletions

View File

@ -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"
@ -47,6 +48,9 @@ func (m *modClearChannel) Initialize(crontab *cron.Cron, discord *discordgo.Sess
func (m modClearChannel) cronClearChannel() {
var (
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

View File

@ -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`