From 2c797e487e87d00817a89087b7ab3e4cf5c22569 Mon Sep 17 00:00:00 2001 From: Galen CC <95080649+galen8183@users.noreply.github.com> Date: Thu, 20 Feb 2025 21:13:13 -0500 Subject: [PATCH] moderation: ignore non-moderation audit log events (#1843) * moderation: ignore non-timeout audit log member update events Explicitly check for AuditLogChangeKeyCommunicationDisabledUntil to differentiate between audit log member update events that change a member's timeout and others AuditLogActionMemberUpdate fires for any member update event, including things like role and nickname changes Signed-off-by: Galen CC * moderation: slices.ContainsFunc over manual iteration for timeout check Prefer slices.ContainsFunc over manual iteration to check audit log member update event changes for new timeouts Fix non-timeout changes immediately returning regardless of other changes possibly being a new timeout Signed-off-by: Galen CC * moderation: ignore audit log events without a target user All moderation related audit log events will have a target user, make sure we ignore any events without a TargetID or a non-user target. Signed-off-by: Galen CC --------- Signed-off-by: Galen CC --- moderation/plugin_bot.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/moderation/plugin_bot.go b/moderation/plugin_bot.go index 7aae56a98..273cb3b51 100644 --- a/moderation/plugin_bot.go +++ b/moderation/plugin_bot.go @@ -3,6 +3,7 @@ package moderation import ( "database/sql" "math/rand" + "slices" "strconv" "strings" "time" @@ -303,6 +304,10 @@ func RefreshMuteOverrideForChannel(config *Config, channel dstate.ChannelState) func HandleGuildAuditLogEntryCreate(evt *eventsystem.EventData) (retry bool, err error) { data := evt.GuildAuditLogEntryCreate() + if data.UserID == 0 || data.TargetID == 0 { + return false, nil + } + config, err := BotCachedGetConfig(data.GuildID) if err != nil { return true, errors.WithStackIf(err) @@ -324,21 +329,24 @@ func HandleGuildAuditLogEntryCreate(evt *eventsystem.EventData) (retry bool, err target, err := common.BotSession.User(data.TargetID) if err != nil { - return true, errors.WithStackIf(err) + // TargetID may not be a user ID, 404s are expected + if bot.CheckDiscordErrRetry(err) { + return true, errors.WithStackIf(err) + } + return false, nil } // setup done, now we get to the actions. switch { case config.LogTimeouts && *data.ActionType == discordgo.AuditLogActionMemberUpdate: - for _, c := range data.Changes { - if *c.Key == discordgo.AuditLogChangeKeyCommunicationDisabledUntil { - if c.NewValue == nil { - return false, nil - } else { - break - } - } + isTimeout := slices.ContainsFunc(data.Changes, func(c *discordgo.AuditLogChange) bool { + return *c.Key == discordgo.AuditLogChangeKeyCommunicationDisabledUntil && c.NewValue != nil + }) + + if !isTimeout { + return false, nil } + err = CreateModlogEmbed(config, &author.User, MATimeoutAdded, target, data.Reason, "") case config.LogKicks && *data.ActionType == discordgo.AuditLogActionMemberKick: err = CreateModlogEmbed(config, &author.User, MAKick, target, data.Reason, "")