blob: e70fb3c1036fefb26ff20814295d33139a4152a3 [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))
PineaFan638eb132023-01-19 10:41:22 +000014 .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") {
PineaFan638eb132023-01-19 10:41:22 +000024 if (!auditLog) return;
TheCodedProfa16d1672023-01-18 18:58:34 -050025 await client.database.history.create("kick", member.guild.id, member.user, auditLog.executor, auditLog.reason);
pineafan63fc5e22022-08-04 22:04:10 +010026 data = {
27 meta: {
28 type: "memberKick",
29 displayName: "Member Kicked",
30 calculateType: "guildMemberPunish",
31 color: NucleusColors.red,
32 emoji: "PUNISH.KICK.RED",
33 timestamp: new Date().getTime()
34 },
35 list: {
36 memberId: entry(member.id, `\`${member.id}\``),
37 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050038 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
Skyler Grey11236ba2022-08-08 21:13:33 +010039 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProfa16d1672023-01-18 18:58:34 -050040 kickedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)),
41 reason: entry(auditLog.reason, auditLog.reason ? `\n> ${auditLog.reason}` : "*No reason provided.*"),
42 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010043 serverMemberCount: member.guild.memberCount
44 },
45 hidden: {
46 guild: member.guild.id
pineafane625d782022-05-09 18:04:32 +010047 }
pineafan63fc5e22022-08-04 22:04:10 +010048 };
49 } else {
Skyler Grey11236ba2022-08-08 21:13:33 +010050 await client.database.history.create("leave", member.guild.id, member.user, null, null);
pineafan63fc5e22022-08-04 22:04:10 +010051 data = {
52 meta: {
53 type: "memberLeave",
54 displayName: "Member Left",
55 calculateType: "guildMemberUpdate",
56 color: NucleusColors.red,
pineafan0f5cc782022-08-12 21:55:42 +010057 emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE",
pineafan63fc5e22022-08-04 22:04:10 +010058 timestamp: new Date().getTime()
59 },
60 list: {
61 memberId: entry(member.id, `\`${member.id}\``),
62 name: entry(member.id, renderUser(member.user)),
TheCodedProfa16d1672023-01-18 18:58:34 -050063 joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)),
Skyler Grey11236ba2022-08-08 21:13:33 +010064 left: entry(new Date().getTime(), renderDelta(new Date().getTime())),
TheCodedProfa16d1672023-01-18 18:58:34 -050065 accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)),
pineafan63fc5e22022-08-04 22:04:10 +010066 serverMemberCount: member.guild.memberCount
67 },
68 hidden: {
69 guild: member.guild.id
pineafan32767212022-03-14 21:27:39 +000070 }
pineafan63fc5e22022-08-04 22:04:10 +010071 };
72 }
73 log(data);
pineafan32767212022-03-14 21:27:39 +000074}