Skip to content

Commit

Permalink
feat: Add log entry for member kicks.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Jul 4, 2024
1 parent 6d3d557 commit 52dcbeb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
2 changes: 2 additions & 0 deletions assets/localisations/events/eng-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"events.messageUpdate.description": "{user} has updated their message in {channel}.",
"events.messageUpdate.fields.before": "Before",
"events.messageUpdate.fields.after": "After",
"events.memberKick.title": "User kicked",
"events.memberKick.description": "{user} has been kicked by {moderator}.",
"events.entryRequestAccept.title": "Entry request accepted",
"events.entryRequestAccept.description": "{user}'s entry request has been accepted by {moderator}.",
"events.entryRequestReject.title": "Entry request rejected",
Expand Down
4 changes: 4 additions & 0 deletions source/constants/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,10 @@ export default Object.freeze({
after: localise("events.messageUpdate.fields.after", locale)(),
},
}),
memberKick: ({ localise, locale }) => ({
title: localise("events.memberKick.title", locale)(),
description: localise("events.memberKick.description", locale),
}),
entryRequestAccept: ({ localise, locale }) => ({
title: localise("events.entryRequestAccept.title", locale)(),
description: localise("events.entryRequestAccept.description", locale),
Expand Down
1 change: 1 addition & 0 deletions source/constants/emojis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default Object.freeze({
unbanned: "😇",
joined: "😁",
left: "😔",
kicked: "🚪",
},
message: {
updated: "⬆️",
Expand Down
38 changes: 30 additions & 8 deletions source/library/stores/journalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,22 @@ class JournallingStore {
}

async #guildMemberRemove(user: Discord.User, guildId: bigint): Promise<void> {
const wasKicked = await this.#wasKicked({ user, guildId });
if (wasKicked) {
await this.tryLog("guildMemberKick", { guildId, args: [user, guildId] });
} else {
await this.tryLog("guildMemberRemove", { guildId, args: [user, guildId] });
const kickInformation = await this.#getKickInformation({ user, guildId });
if (kickInformation !== undefined) {
if (kickInformation.userId === null) {
return;
}

const authorMember = this.#client.entities.members.get(guildId)?.get(BigInt(kickInformation.userId));
if (authorMember === undefined) {
return;
}

await this.tryLog("guildMemberKick", { guildId, args: [user, authorMember] });
return;
}

await this.tryLog("guildMemberRemove", { guildId, args: [user, guildId] });
}

async #messageDelete(
Expand All @@ -161,13 +171,25 @@ class JournallingStore {
await this.tryLog("messageUpdate", { guildId, args: [message, oldMessage] });
}

async #wasKicked({ user, guildId }: { user: Logos.User; guildId: bigint }): Promise<boolean> {
async #getKickInformation({
user,
guildId,
}: { user: Logos.User; guildId: bigint }): Promise<Discord.CamelizedDiscordAuditLogEntry | undefined> {
const now = Date.now();

const auditLog = await this.#client.bot.helpers.getAuditLog(guildId, { actionType: AuditLogEvents.MemberKick });
const auditLog = await this.#client.bot.helpers
.getAuditLog(guildId, { actionType: AuditLogEvents.MemberKick })
.catch((reason) => {
this.log.warn(`Could not get audit log for ${this.#client.diagnostics.guild(guildId)}:`, reason);
return undefined;
});
if (auditLog === undefined) {
return undefined;
}

return auditLog.auditLogEntries
.filter((entry) => snowflakeToTimestamp(BigInt(entry.id)) >= now - constants.time.second * 5)
.some((entry) => entry.targetId === user.id.toString());
.find((entry) => entry.targetId === user.id.toString());
}
}

Expand Down
22 changes: 22 additions & 0 deletions source/library/stores/journalling/logos/guild-member-kick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { EventLogger } from "logos/stores/journalling/loggers";

const logger: EventLogger<"guildMemberKick"> = (client, [user, author], { guildLocale }) => {
const strings = constants.contexts.memberKick({
localise: client.localise.bind(client),
locale: guildLocale,
});
return {
embeds: [
{
title: `${constants.emojis.events.user.kicked} ${strings.title}`,
color: constants.colours.warning,
description: strings.description({
user: client.diagnostics.user(user),
moderator: client.diagnostics.member(author),
}),
},
],
};
};

export default logger;
2 changes: 1 addition & 1 deletion source/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ declare global {
/** Type representing events that occur within a guild. */
type Events = {
/** Fill-in Discord event for a member having been kicked. */
guildMemberKick: [user: Logos.User, guildId: bigint];
guildMemberKick: [user: Logos.User, by: Logos.Member];
} & {
/** An entry request has been submitted. */
entryRequestSubmit: [user: Logos.User, entryRequest: EntryRequest];
Expand Down

0 comments on commit 52dcbeb

Please sign in to comment.