Skip to content

Commit

Permalink
refactored code to prevent ratelimits (#1844)
Browse files Browse the repository at this point in the history
* refactored code to prevent ratelimits

* fixed issue with spammy modlog

---------

Co-authored-by: Ashish <ashishjh-bst@users.noreply.github.com>
  • Loading branch information
ashishjh-bst and ashishjh-bst authored Feb 21, 2025
1 parent 2c797e4 commit d433b94
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions moderation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,47 +322,57 @@ func HandleGuildAuditLogEntryCreate(evt *eventsystem.EventData) (retry bool, err
return false, nil
}

author, err := bot.GetMember(data.GuildID, data.UserID)
if err != nil {
return true, errors.WithStackIf(err)
}

target, err := common.BotSession.User(data.TargetID)
if err != nil {
// 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:
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, "")
return doModlog(config, data, MATimeoutAdded)
case config.LogKicks && *data.ActionType == discordgo.AuditLogActionMemberKick:
err = CreateModlogEmbed(config, &author.User, MAKick, target, data.Reason, "")
return doModlog(config, data, MAKick)
case config.LogBans && *data.ActionType == discordgo.AuditLogActionMemberBanAdd:
err = CreateModlogEmbed(config, &author.User, MABanned, target, data.Reason, "")
return doModlog(config, data, MABanned)
case config.LogUnbans && *data.ActionType == discordgo.AuditLogActionMemberBanRemove:
err = CreateModlogEmbed(config, &author.User, MAUnbanned, target, data.Reason, "")
return doModlog(config, data, MAUnbanned)
}
return false, nil
}

func doModlog(config *Config, data *discordgo.GuildAuditLogEntryCreate, action ModlogAction) (retry bool, err error) {
author, target, retry, err := getMemberAndUser(data.GuildID, data.UserID, data.TargetID)
if err != nil {
logger.WithError(err).WithField("guild", data.GuildID).Error("Failed sending mod log entry.")
return retry, err
}
err = CreateModlogEmbed(config, &author.User, action, target, data.Reason, "")
if err != nil {
logger.WithError(err).WithField("guild", data.GuildID).Error("Failed sending mod log entry.")
return false, err
}

return false, nil
}

func getMemberAndUser(guildID, authorID, targetID int64) (author *dstate.MemberState, target *discordgo.User, retry bool, err error) {
author, err = bot.GetMember(guildID, authorID)
if err != nil {
return nil, nil, false, errors.WithStackIf(err)
}

target, err = common.BotSession.User(targetID)
if err != nil {
// TargetID may not be a user ID, 404s are expected
if bot.CheckDiscordErrRetry(err) {
return nil, nil, true, errors.WithStackIf(err)
}
return nil, nil, false, err
}
return author, target, false, nil
}

// Since updating mutes are now a complex operation with removing roles and whatnot,
// to avoid weird bugs from happening we lock it so it can only be updated one place per user
func LockMemberMuteMW(next eventsystem.HandlerFunc) eventsystem.HandlerFunc {
Expand Down

0 comments on commit d433b94

Please sign in to comment.