blob: 8b3d1b1331ee926396900b11310849e8de3455ab [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;
14 if (!await isLogging(member.guild.id, "guildMemberUpdate")) return;
15 const kickAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberKick))
16 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id)[0];
17 const banAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberBanAdd))
PineaFan638eb132023-01-19 10:41:22 +000018 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id)[0];
pineafan63fc5e22022-08-04 22:04:10 +010019 let type = "leave";
TheCodedProf6ec331b2023-02-20 12:13:06 -050020 if (kickAuditLog) {
21 if (kickAuditLog.executor!.id === client.user!.id) return;
22 if (kickAuditLog.createdAt.getTime() >= startTime) {
pineafan63fc5e22022-08-04 22:04:10 +010023 type = "kick";
pineafane625d782022-05-09 18:04:32 +010024 }
pineafan63fc5e22022-08-04 22:04:10 +010025 }
TheCodedProf6ec331b2023-02-20 12:13:06 -050026 if (banAuditLog) {
27 if (banAuditLog.executor!.id === client.user!.id) return;
28 if (banAuditLog.createdAt.getTime() >= startTime) {
29 if (!kickAuditLog) {
30 return
31 } else if (kickAuditLog.createdAt.valueOf() < banAuditLog.createdAt.valueOf()) {
32 return
33 }
34 }
35 }
pineafan63fc5e22022-08-04 22:04:10 +010036 let data;
37 if (type === "kick") {
TheCodedProf6ec331b2023-02-20 12:13:06 -050038 if (!kickAuditLog) return;
39 await client.database.history.create("kick", member.guild.id, member.user, kickAuditLog.executor, kickAuditLog.reason);
pineafan63fc5e22022-08-04 22:04:10 +010040 data = {
41 meta: {
42 type: "memberKick",
43 displayName: "Member Kicked",
44 calculateType: "guildMemberPunish",
45 color: NucleusColors.red,
46 emoji: "PUNISH.KICK.RED",
TheCodedProf6ec331b2023-02-20 12:13:06 -050047 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010048 },
49 list: {
50 memberId: entry(member.id, `\`${member.id}\``),
51 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050052 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050053 kicked: entry(Date.now(), renderDelta(Date.now())),
54 kickedBy: entry(kickAuditLog.executor!.id, renderUser(kickAuditLog.executor!)),
55 reason: entry(kickAuditLog.reason, kickAuditLog.reason ? `\n> ${kickAuditLog.reason}` : "*No reason provided.*"),
TheCodedProfa16d1672023-01-18 18:58:34 -050056 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010057 serverMemberCount: member.guild.memberCount
58 },
59 hidden: {
60 guild: member.guild.id
pineafane625d782022-05-09 18:04:32 +010061 }
pineafan63fc5e22022-08-04 22:04:10 +010062 };
63 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +010064 await client.database.history.create("leave", member.guild.id, member.user, null, null);
pineafan63fc5e22022-08-04 22:04:10 +010065 data = {
66 meta: {
67 type: "memberLeave",
68 displayName: "Member Left",
69 calculateType: "guildMemberUpdate",
70 color: NucleusColors.red,
pineafan0f5cc782022-08-12 21:55:42 +010071 emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE",
TheCodedProf6ec331b2023-02-20 12:13:06 -050072 timestamp: Date.now()
pineafan63fc5e22022-08-04 22:04:10 +010073 },
74 list: {
75 memberId: entry(member.id, `\`${member.id}\``),
76 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050077 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
TheCodedProf6ec331b2023-02-20 12:13:06 -050078 left: entry(Date.now(), renderDelta(Date.now())),
TheCodedProfa16d1672023-01-18 18:58:34 -050079 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010080 serverMemberCount: member.guild.memberCount
81 },
82 hidden: {
83 guild: member.guild.id
pineafan32767212022-03-14 21:27:39 +000084 }
pineafan63fc5e22022-08-04 22:04:10 +010085 };
86 }
87 log(data);
pineafan32767212022-03-14 21:27:39 +000088}