diff options
| author | Fuwn <[email protected]> | 2025-11-07 21:33:56 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-11-07 21:33:56 -0800 |
| commit | bb45526764444fb383ab0fc895786280c9f4571e (patch) | |
| tree | 8bb133a680bb55edb47365e9bf037d71810d5b71 | |
| parent | feat(gateway:listeners): Add image manager for #nsfw-text (diff) | |
| download | umabotdiscord-bb45526764444fb383ab0fc895786280c9f4571e.tar.xz umabotdiscord-bb45526764444fb383ab0fc895786280c9f4571e.zip | |
feat(gateway:memberLeave): Log leaving users
| -rw-r--r-- | packages/gateway/src/listeners/memberLeave.ts | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/packages/gateway/src/listeners/memberLeave.ts b/packages/gateway/src/listeners/memberLeave.ts index 3fdef88..b78e86e 100644 --- a/packages/gateway/src/listeners/memberLeave.ts +++ b/packages/gateway/src/listeners/memberLeave.ts @@ -2,6 +2,50 @@ import { Client, Events, GuildMember } from "discord.js"; import { RolePersistenceService } from "../database/rolePersistence"; import { logUnexpectedDiscordAPIError } from "../utilities"; +const STAFF_GOODBYE_CHANNEL_ID = "1436588402052042874"; + +const formatDuration = (milliseconds: number): string => { + const seconds = Math.floor(milliseconds / 1000); + const minutes = Math.floor(seconds / 60); + const hours = Math.floor(minutes / 60); + const days = Math.floor(hours / 24); + const months = Math.floor(days / 30); + const years = Math.floor(days / 365); + + if (years > 0) { + const remainingMonths = Math.floor((days % 365) / 30); + + if (remainingMonths > 0) + return `${years} year${years > 1 ? "s" : ""}, ${remainingMonths} month${remainingMonths > 1 ? "s" : ""}`; + + return `${years} year${years > 1 ? "s" : ""}`; + } + + if (months > 0) { + const remainingDays = days % 30; + + if (remainingDays > 0) + return `${months} month${months > 1 ? "s" : ""}, ${remainingDays} day${remainingDays > 1 ? "s" : ""}`; + + return `${months} month${months > 1 ? "s" : ""}`; + } + + if (days > 0) { + const remainingHours = hours % 24; + + if (remainingHours > 0) + return `${days} day${days > 1 ? "s" : ""}, ${remainingHours} hour${remainingHours > 1 ? "s" : ""}`; + + return `${days} day${days > 1 ? "s" : ""}`; + } + + if (hours > 0) return `${hours} hour${hours > 1 ? "s" : ""}`; + + if (minutes > 0) return `${minutes} minute${minutes > 1 ? "s" : ""}`; + + return `${seconds} second${seconds > 1 ? "s" : ""}`; +}; + export const handleMemberLeave = (client: Client) => { client.on(Events.GuildMemberRemove, async (member) => { try { @@ -9,6 +53,32 @@ export const handleMemberLeave = (client: Client) => { if ("roles" in member) await RolePersistenceService.saveUserRoles(member as GuildMember); + + try { + const goodbyeChannel = client.channels.cache.get( + STAFF_GOODBYE_CHANNEL_ID, + ); + + if ( + goodbyeChannel && + goodbyeChannel.isTextBased() && + !goodbyeChannel.isDMBased() + ) { + const joinedAt = member.joinedAt; + + let message = `${member.user} left the server.`; + + if (joinedAt) { + const membershipDuration = Date.now() - joinedAt.getTime(); + + message += ` They were a member for ${formatDuration(membershipDuration)}.`; + } + + await goodbyeChannel.send(message); + } + } catch (error) { + logUnexpectedDiscordAPIError(error); + } } catch (error) { logUnexpectedDiscordAPIError(error); } |