Skip to content

Commit

Permalink
moderation: ignore non-moderation audit log events (#1843)
Browse files Browse the repository at this point in the history
* 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 <galen8183@gmail.com>

* 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 <galen8183@gmail.com>

* 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 <galen8183@gmail.com>

---------

Signed-off-by: Galen CC <galen8183@gmail.com>
  • Loading branch information
galen8183 authored Feb 21, 2025
1 parent 876fda0 commit 2c797e4
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions moderation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package moderation
import (
"database/sql"
"math/rand"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)
Expand All @@ -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, "")
Expand Down

0 comments on commit 2c797e4

Please sign in to comment.