blob: 20845d5456fb3ab933279dc9e431eaf2dab31f41 [file] [log] [blame]
pineafan0f5cc782022-08-12 21:55:42 +01001import type { GuildAuditLogsEntry, GuildMember } from "discord.js";
2// @ts-expect-error
3import type { HaikuClient } from "jshaiku";
4
pineafan63fc5e22022-08-04 22:04:10 +01005import { purgeByUser } from "../actions/tickets/delete.js";
6import { callback as statsChannelRemove } from "../reflex/statsChannelUpdate.js";
pineafan32767212022-03-14 21:27:39 +00007
pineafan63fc5e22022-08-04 22:04:10 +01008export const event = "guildMemberRemove";
pineafan32767212022-03-14 21:27:39 +00009
pineafan0f5cc782022-08-12 21:55:42 +010010export async function callback(client: HaikuClient, member: GuildMember) {
pineafan63fc5e22022-08-04 22:04:10 +010011 purgeByUser(member.id, member.guild);
12 await statsChannelRemove(client, member);
pineafan0f5cc782022-08-12 21:55:42 +010013 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010014 const auditLog = await getAuditLog(member.guild, "MEMBER_KICK");
pineafan0f5cc782022-08-12 21:55:42 +010015 const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === member.id).first();
pineafan63fc5e22022-08-04 22:04:10 +010016 let type = "leave";
17 if (audit) {
18 if (audit.executor.id === client.user.id) return;
19 if (audit.createdAt - 100 >= new Date().getTime()) {
20 type = "kick";
pineafane625d782022-05-09 18:04:32 +010021 }
pineafan63fc5e22022-08-04 22:04:10 +010022 }
23 let data;
24 if (type === "kick") {
Skyler Grey11236ba2022-08-08 21:13:33 +010025 await client.database.history.create("kick", member.guild.id, member.user, audit.executor, audit.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)),
38 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
Skyler Grey11236ba2022-08-08 21:13:33 +010039 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010040 kickedBy: entry(audit.executor.id, renderUser(audit.executor)),
Skyler Grey11236ba2022-08-08 21:13:33 +010041 reason: entry(audit.reason, audit.reason ? `\n> ${audit.reason}` : "*No reason provided.*"),
42 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
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)),
Skyler Grey11236ba2022-08-08 21:13:33 +010063 joined: entry(member.joinedTimestamp, renderDelta(member.joinedAt)),
64 left: entry(new Date().getTime(), renderDelta(new Date().getTime())),
65 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
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}