Skip to content

Commit

Permalink
moderation: fix unban modlog entry creation (#1841)
Browse files Browse the repository at this point in the history
* moderation: fix unban modlog entry creation

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

* moderation: remove old logic

Signed-off-by: Galen CC <galen8183@gmail.com>

* moderation: check discord API errors in scheduled unban handler

Signed-off-by: Galen CC <galen8183@gmail.com>

---------

Signed-off-by: Galen CC <galen8183@gmail.com>
  • Loading branch information
galen8183 authored Feb 13, 2025
1 parent e3c6408 commit b90711b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 14 additions & 6 deletions moderation/plugin_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 5 additions & 1 deletion moderation/punishments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit b90711b

Please sign in to comment.