blob: 4731d58df36d6e12bb3a47edf7011d437d8cf77f [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import type { 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) {
pineafan63fc5e22022-08-04 22:04:10 +010010 purgeByUser(member.id, member.guild);
11 await statsChannelRemove(client, member);
pineafan0f5cc782022-08-12 21:55:42 +010012 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010013 const auditLog = await getAuditLog(member.guild, "MEMBER_KICK");
pineafan0f5cc782022-08-12 21:55:42 +010014 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === member.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010015 let type = "leave";
16 if (audit) {
17 if (audit.executor.id === client.user.id) return;
18 if (audit.createdAt - 100 >= new Date().getTime()) {
19 type = "kick";
pineafane625d782022-05-09 18:04:32 +010020 }
pineafan63fc5e22022-08-04 22:04:10 +010021 }
22 let data;
23 if (type === "kick") {
Skyler Grey11236ba2022-08-08 21:13:33 +010024 await client.database.history.create("kick", member.guild.id, member.user, audit.executor, audit.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)),
37 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
Skyler Grey11236ba2022-08-08 21:13:33 +010038 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010039 kickedBy: entry(audit.executor.id, renderUser(audit.executor)),
Skyler Grey11236ba2022-08-08 21:13:33 +010040 reason: entry(audit.reason, audit.reason ? `\n> ${audit.reason}` : "*No reason provided.*"),
41 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
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)),
Skyler Grey11236ba2022-08-08 21:13:33 +010062 joined: entry(member.joinedTimestamp, renderDelta(member.joinedAt)),
63 left: entry(new Date().getTime(), renderDelta(new Date().getTime())),
64 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
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}