-
Notifications
You must be signed in to change notification settings - Fork 6
Track improvements: formatting, kicks/bans, stats #246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
70f7153
Move reaction list to quoted message instead of report message
vcarl b7150ad
Include reaction/embed stats in tiny preview
vcarl e93f553
Move report attribution to its own line
vcarl 1dab999
Add kick/ban event logging to user mod threads
vcarl 92769a0
Apply suggestions from code review
vcarl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| import { | ||
| AuditLogEvent, | ||
| Events, | ||
| type Client, | ||
| type Guild, | ||
| type GuildBan, | ||
| type GuildMember, | ||
| type PartialGuildMember, | ||
| type PartialUser, | ||
| type User, | ||
| } from "discord.js"; | ||
|
|
||
| import { reportModAction, type ModActionReport } from "#~/helpers/modLog"; | ||
| import { log } from "#~/helpers/observability"; | ||
|
|
||
| // Time window to check audit log for matching entries (5 seconds) | ||
| const AUDIT_LOG_WINDOW_MS = 5000; | ||
|
|
||
| async function handleBanAdd(ban: GuildBan) { | ||
| const { guild, user } = ban; | ||
| let { reason } = ban; | ||
| let executor: User | PartialUser | null = null; | ||
|
|
||
| log("info", "ModActionLogger", "Ban detected", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| reason, | ||
| }); | ||
|
|
||
| try { | ||
| // Check audit log for who performed the ban | ||
| const auditLogs = await guild.fetchAuditLogs({ | ||
| type: AuditLogEvent.MemberBanAdd, | ||
| limit: 5, | ||
| }); | ||
|
|
||
| const banEntry = auditLogs.entries.find( | ||
| (entry) => | ||
| entry.target?.id === user.id && | ||
| Date.now() - entry.createdTimestamp < AUDIT_LOG_WINDOW_MS, | ||
| ); | ||
|
|
||
| executor = banEntry?.executor ?? null; | ||
| reason = banEntry?.reason ?? reason; | ||
|
|
||
| // Skip if the bot performed this action (it's already logged elsewhere) | ||
| if (executor?.id === guild.client.user?.id) { | ||
| log("debug", "ModActionLogger", "Skipping self-ban", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| }); | ||
| return; | ||
| } | ||
| } catch (error) { | ||
| // If we can't access audit log, still log the ban but without executor info | ||
| if ( | ||
| error instanceof Error && | ||
| error.message.includes("Missing Permissions") | ||
| ) { | ||
| log( | ||
| "warn", | ||
| "ModActionLogger", | ||
| "Cannot access audit log for ban details", | ||
| { userId: user.id, guildId: guild.id }, | ||
| ); | ||
| } else { | ||
| log("error", "ModActionLogger", "Failed to fetch audit log for ban", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| error, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| await reportModAction({ | ||
| guild, | ||
| user, | ||
| actionType: "ban", | ||
| executor, | ||
| reason: reason ?? "", | ||
| }); | ||
| } catch (error) { | ||
| log("error", "ModActionLogger", "Failed to report ban", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| error, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| async function fetchAuditLogs( | ||
| guild: Guild, | ||
| user: User, | ||
| ): Promise<ModActionReport | undefined> { | ||
| // Check audit log to distinguish kick from voluntary leave | ||
| const auditLogs = await guild.fetchAuditLogs({ | ||
| type: AuditLogEvent.MemberKick, | ||
| limit: 5, | ||
| }); | ||
|
|
||
| const kickEntry = auditLogs.entries.find( | ||
| (entry) => | ||
| entry.target?.id === user.id && | ||
| Date.now() - entry.createdTimestamp < AUDIT_LOG_WINDOW_MS, | ||
| ); | ||
|
|
||
| // If no kick entry found, user left voluntarily | ||
| if (!kickEntry) { | ||
| log( | ||
| "debug", | ||
| "ModActionLogger", | ||
| "No kick entry found, user left voluntarily", | ||
| { userId: user.id, guildId: guild.id }, | ||
| ); | ||
| return { | ||
| actionType: "left", | ||
| user, | ||
| guild, | ||
| executor: undefined, | ||
| reason: undefined, | ||
| }; | ||
| } | ||
| const { executor, reason } = kickEntry; | ||
|
|
||
| if (!executor) { | ||
| log( | ||
| "warn", | ||
| "ModActionLogger", | ||
| `No executor found for audit log entry ${kickEntry.id}`, | ||
| ); | ||
| } | ||
|
|
||
| // Skip if the bot performed this action | ||
| // TODO: maybe best to invert — remove manual kick logs in favor of this | ||
| if (kickEntry.executor?.id === guild.client.user?.id) { | ||
| log("debug", "ModActionLogger", "Skipping self-kick", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| return { actionType: "kick", user, guild, executor, reason: reason ?? "" }; | ||
| } | ||
|
|
||
| async function handleMemberRemove(member: GuildMember | PartialGuildMember) { | ||
| const { guild, user } = member; | ||
|
|
||
| log("info", "ModActionLogger", "Member removal detected", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| }); | ||
|
|
||
| try { | ||
| const auditLogs = await fetchAuditLogs(guild, user); | ||
|
|
||
| if (auditLogs) { | ||
| const { executor = null, reason = "" } = auditLogs; | ||
| await reportModAction({ | ||
| guild, | ||
| user, | ||
| actionType: "kick", | ||
| executor, | ||
| reason, | ||
| }); | ||
| return; | ||
| } | ||
| await reportModAction({ | ||
| guild, | ||
| user, | ||
| actionType: "left", | ||
| executor: undefined, | ||
| reason: undefined, | ||
| }); | ||
| } catch (error) { | ||
| log("error", "ModActionLogger", "Failed to handle member removal", { | ||
| userId: user.id, | ||
| guildId: guild.id, | ||
| error, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export default async (bot: Client) => { | ||
| bot.on(Events.GuildBanAdd, async (ban) => { | ||
| try { | ||
| await handleBanAdd(ban); | ||
| } catch (error) { | ||
| log("error", "ModActionLogger", "Unhandled error in ban handler", { | ||
| userId: ban.user.id, | ||
| guildId: ban.guild.id, | ||
| error, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| bot.on(Events.GuildMemberRemove, async (member) => { | ||
| try { | ||
| await handleMemberRemove(member); | ||
| } catch (error) { | ||
| log( | ||
| "error", | ||
| "ModActionLogger", | ||
| "Unhandled error in member remove handler", | ||
| { | ||
| userId: member.user?.id, | ||
| guildId: member.guild.id, | ||
| error, | ||
| }, | ||
| ); | ||
| } | ||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.