From b90711be20d488d1125cd62b2463af4756057be9 Mon Sep 17 00:00:00 2001 From: Galen CC <95080649+galen8183@users.noreply.github.com> Date: Wed, 12 Feb 2025 23:10:01 -0500 Subject: [PATCH] moderation: fix unban modlog entry creation (#1841) * moderation: fix unban modlog entry creation Fix e3c64084 incorrectly reporting ban authors in the modlog. Modlog creation for unbans was moved to the audit log entry event handler, erroneously reporting the bot user as the author of any unban, including manual unbans with the bot's moderation command. Instead, UnbanUser now handles modlog entry creation, and handleScheduledUnban calls the same UnbanUser function rather than directly unbanning the user. Signed-off-by: Galen CC * moderation: remove old logic Signed-off-by: Galen CC * moderation: check discord API errors in scheduled unban handler Signed-off-by: Galen CC --------- Signed-off-by: Galen CC --- moderation/plugin_bot.go | 20 ++++++++++++++------ moderation/punishments.go | 6 +++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/moderation/plugin_bot.go b/moderation/plugin_bot.go index 64ea3f66b..a203df033 100644 --- a/moderation/plugin_bot.go +++ b/moderation/plugin_bot.go @@ -313,10 +313,8 @@ func HandleGuildAuditLogEntryCreate(evt *eventsystem.EventData) (retry bool, err } if data.UserID == common.BotUser.ID { - if *data.ActionType != discordgo.AuditLogActionMemberBanRemove { - // we performed the action, do not duplicate - return false, nil - } + // we performed the action, do not duplicate + return false, nil } author, err := bot.GetMember(data.GuildID, data.UserID) @@ -573,9 +571,19 @@ func handleScheduledUnban(evt *seventsmodels.ScheduledEvent, data interface{}) ( return false, nil } - err = common.BotSession.GuildBanDeleteWithReason(guildID, userID, scheduledUnbanReason) + config, err := FetchConfig(guildID) + if err != nil { + return false, err + } + + user := &discordgo.User{ + Username: "unknown", + Discriminator: "????", + ID: userID, + } + + _, err = UnbanUser(config, guildID, common.BotUser, scheduledUnbanReason, user) if err != nil { - logger.WithField("guild", guildID).WithError(err).Error("failed unbanning user") return scheduledevents2.CheckDiscordErrRetry(err), err } diff --git a/moderation/punishments.go b/moderation/punishments.go index 85e8922dc..970473b8f 100644 --- a/moderation/punishments.go +++ b/moderation/punishments.go @@ -152,7 +152,7 @@ func punish(config *Config, p Punishment, guildID int64, channel *dstate.Channel return err } - logger.WithField("guild_id", guildID).Infof("MODERATION: %s %s %s cause %q", author.Username, action.Prefix, user.Username, reason) + logger.WithField("guild_id", guildID).Infof("MODERATION: %s %s %s with reason %q", author.Username, action.Prefix, user.Username, reason) if memberNotFound { // Wait a tiny bit to make sure the audit log is updated time.Sleep(time.Second * 3) @@ -385,6 +385,10 @@ func UnbanUser(config *Config, guildID int64, author *discordgo.User, reason str return notbanned, err } + if config.LogUnbans { + err = CreateModlogEmbed(config, author, action, user, reason, "") + } + logger.Infof("MODERATION: %s %s %s with reason %q", author.Username, action.Prefix, user.Username, reason) return false, err }