Skip to content

Commit

Permalink
feat: Distinguish between a user leaving and being kicked.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Jul 4, 2024
1 parent b89566d commit 6d3d557
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 16 additions & 1 deletion source/library/stores/journalling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Client } from "logos/client";
import { Collector } from "logos/collectors";
import { Logger } from "logos/logger";
import loggers from "logos/stores/journalling/loggers";
import { AuditLogEvents, snowflakeToTimestamp } from "@discordeno/bot";

type Events = Logos.Events & Discord.Events;

Expand Down Expand Up @@ -131,7 +132,12 @@ class JournallingStore {
}

async #guildMemberRemove(user: Discord.User, guildId: bigint): Promise<void> {
await this.tryLog("guildMemberRemove", { guildId, args: [user, guildId] });
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] });
}
}

async #messageDelete(
Expand All @@ -154,6 +160,15 @@ class JournallingStore {

await this.tryLog("messageUpdate", { guildId, args: [message, oldMessage] });
}

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

const auditLog = await this.#client.bot.helpers.getAuditLog(guildId, { actionType: AuditLogEvents.MemberKick });
return auditLog.auditLogEntries
.filter((entry) => snowflakeToTimestamp(BigInt(entry.id)) >= now - constants.time.second * 5)
.some((entry) => entry.targetId === user.id.toString());
}
}

export { JournallingStore };
3 changes: 3 additions & 0 deletions source/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ 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];
} & {
/** An entry request has been submitted. */
entryRequestSubmit: [user: Logos.User, entryRequest: EntryRequest];

Expand Down

0 comments on commit 6d3d557

Please sign in to comment.