blob: c4ce74e11b3002d24a577b50a29d647f44cbd1cd [file] [log] [blame]
TheCodedProfa16d1672023-01-18 18:58:34 -05001import { AuditLogEvent, Guild, GuildAuditLogsEntry, GuildMember } from "discord.js";
PineaFan752af462022-12-31 21:59:38 +00002import type { NucleusClient } from "../utils/client.js";
pineafan0f5cc782022-08-12 21:55:42 +01003
pineafan63fc5e22022-08-04 22:04:10 +01004import { purgeByUser } from "../actions/tickets/delete.js";
5import { callback as statsChannelRemove } from "../reflex/statsChannelUpdate.js";
pineafan32767212022-03-14 21:27:39 +00006
pineafan63fc5e22022-08-04 22:04:10 +01007export const event = "guildMemberRemove";
pineafan32767212022-03-14 21:27:39 +00008
PineaFan752af462022-12-31 21:59:38 +00009export async function callback(client: NucleusClient, member: GuildMember) {
TheCodedProf6ec331b2023-02-20 12:13:06 -050010 const startTime = Date.now() - 10 * 1000;
TheCodedProfa16d1672023-01-18 18:58:34 -050011 purgeByUser(member.id, member.guild.id);
pineafan63fc5e22022-08-04 22:04:10 +010012 await statsChannelRemove(client, member);
TheCodedProf6ec331b2023-02-20 12:13:06 -050013 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
Skyler Greyda16adf2023-03-05 10:22:12 +000014 if (!(await isLogging(member.guild.id, "guildMemberUpdate"))) return;
15 const kickAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberKick)).filter(
16 (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id
17 )[0];
18 const banAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberBanAdd)).filter(
19 (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id
20 )[0];
pineafan63fc5e22022-08-04 22:04:10 +010021 let type = "leave";
TheCodedProf6ec331b2023-02-20 12:13:06 -050022 if (kickAuditLog) {
23 if (kickAuditLog.executor!.id === client.user!.id) return;
24 if (kickAuditLog.createdAt.getTime() >= startTime) {
pineafan63fc5e22022-08-04 22:04:10 +010025 type = "kick";
pineafane625d782022-05-09 18:04:32 +010026 }
pineafan63fc5e22022-08-04 22:04:10 +010027 }
TheCodedProf6ec331b2023-02-20 12:13:06 -050028 if (banAuditLog) {
29 if (banAuditLog.executor!.id === client.user!.id) return;
30 if (banAuditLog.createdAt.getTime() >= startTime) {
31 if (!kickAuditLog) {
Skyler Greyda16adf2023-03-05 10:22:12 +000032 return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050033 } else if (kickAuditLog.createdAt.valueOf() < banAuditLog.createdAt.valueOf()) {
Skyler Greyda16adf2023-03-05 10:22:12 +000034 return;
TheCodedProf6ec331b2023-02-20 12:13:06 -050035 }
36 }
37 }
pineafan63fc5e22022-08-04 22:04:10 +010038 let data;
39 if (type === "kick") {
TheCodedProf6ec331b2023-02-20 12:13:06 -050040 if (!kickAuditLog) return;
Skyler Greyda16adf2023-03-05 10:22:12 +000041 await client.database.history.create(
42 "kick",
43 member.guild.id,
44 member.user,
45 kickAuditLog.executor,
46 kickAuditLog.reason
47 );
pineafan63fc5e22022-08-04 22:04:10 +010048 data = {
49 meta: {
50 type: "memberKick",
51 displayName: "Member Kicked",
52 calculateType: "guildMemberPunish",
53 color: NucleusColors.red,
54 emoji: "PUNISH.KICK.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -050055 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010056 },
57 list: {
58 memberId: entry(member.id, `\`${member.id}\``),
59 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050060 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050061 kicked: entry(Date.now(), renderDelta(Date.now())),
62 kickedBy: entry(kickAuditLog.executor!.id, renderUser(kickAuditLog.executor!)),
Skyler Greyda16adf2023-03-05 10:22:12 +000063 reason: entry(
64 kickAuditLog.reason,
65 kickAuditLog.reason ? `\n> ${kickAuditLog.reason}` : "*No reason provided.*"
66 ),
TheCodedProfa16d1672023-01-18 18:58:34 -050067 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010068 serverMemberCount: member.guild.memberCount
69 },
70 hidden: {
71 guild: member.guild.id
pineafane625d782022-05-09 18:04:32 +010072 }
pineafan63fc5e22022-08-04 22:04:10 +010073 };
74 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +010075 await client.database.history.create("leave", member.guild.id, member.user, null, null);
pineafan63fc5e22022-08-04 22:04:10 +010076 data = {
77 meta: {
78 type: "memberLeave",
79 displayName: "Member Left",
80 calculateType: "guildMemberUpdate",
81 color: NucleusColors.red,
pineafan0f5cc782022-08-12 21:55:42 +010082 emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050083 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010084 },
85 list: {
86 memberId: entry(member.id, `\`${member.id}\``),
87 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050088 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050089 left: entry(Date.now(), renderDelta(Date.now())),
TheCodedProfa16d1672023-01-18 18:58:34 -050090 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010091 serverMemberCount: member.guild.memberCount
92 },
93 hidden: {
94 guild: member.guild.id
pineafan32767212022-03-14 21:27:39 +000095 }
pineafan63fc5e22022-08-04 22:04:10 +010096 };
97 }
98 log(data);
pineafan32767212022-03-14 21:27:39 +000099}