blob: 936f8c27b64378b94b60b27ae1647573f421f9be [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) {
TheCodedProfa16d1672023-01-18 18:58:34 -050010 purgeByUser(member.id, member.guild.id);
pineafan63fc5e22022-08-04 22:04:10 +010011 await statsChannelRemove(client, member);
pineafan0f5cc782022-08-12 21:55:42 +010012 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
TheCodedProfa16d1672023-01-18 18:58:34 -050013 const auditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberKick))
14 .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id)[0]!;
pineafan63fc5e22022-08-04 22:04:10 +010015 let type = "leave";
TheCodedProfa16d1672023-01-18 18:58:34 -050016 if (auditLog) {
17 if (auditLog.executor!.id === client.user!.id) return;
18 if (auditLog.createdAt.valueOf() - 100 >= new Date().getTime()) {
pineafan63fc5e22022-08-04 22:04:10 +010019 type = "kick";
pineafane625d782022-05-09 18:04:32 +010020 }
pineafan63fc5e22022-08-04 22:04:10 +010021 }
22 let data;
23 if (type === "kick") {
TheCodedProfa16d1672023-01-18 18:58:34 -050024 await client.database.history.create("kick", member.guild.id, member.user, auditLog.executor, auditLog.reason);
pineafan63fc5e22022-08-04 22:04:10 +010025 data = {
26 meta: {
27 type: "memberKick",
28 displayName: "Member Kicked",
29 calculateType: "guildMemberPunish",
30 color: NucleusColors.red,
31 emoji: "PUNISH.KICK.RED",
32 timestamp: new Date().getTime()
33 },
34 list: {
35 memberId: entry(member.id, `\`${member.id}\``),
36 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050037 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
Skyler Grey11236ba2022-08-08 21:13:33 +010038 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProfa16d1672023-01-18 18:58:34 -050039 kickedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
40 reason: entry(auditLog.reason, auditLog.reason ? `\n> ${auditLog.reason}` : "*No reason provided.*"),
41 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010042 serverMemberCount: member.guild.memberCount
43 },
44 hidden: {
45 guild: member.guild.id
pineafane625d782022-05-09 18:04:32 +010046 }
pineafan63fc5e22022-08-04 22:04:10 +010047 };
48 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +010049 await client.database.history.create("leave", member.guild.id, member.user, null, null);
pineafan63fc5e22022-08-04 22:04:10 +010050 data = {
51 meta: {
52 type: "memberLeave",
53 displayName: "Member Left",
54 calculateType: "guildMemberUpdate",
55 color: NucleusColors.red,
pineafan0f5cc782022-08-12 21:55:42 +010056 emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE",
pineafan63fc5e22022-08-04 22:04:10 +010057 timestamp: new Date().getTime()
58 },
59 list: {
60 memberId: entry(member.id, `\`${member.id}\``),
61 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050062 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
Skyler Grey11236ba2022-08-08 21:13:33 +010063 left: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProfa16d1672023-01-18 18:58:34 -050064 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010065 serverMemberCount: member.guild.memberCount
66 },
67 hidden: {
68 guild: member.guild.id
pineafan32767212022-03-14 21:27:39 +000069 }
pineafan63fc5e22022-08-04 22:04:10 +010070 };
71 }
72 log(data);
pineafan32767212022-03-14 21:27:39 +000073}