blob: 50901dc40acf581bda56dd2f92667d3949361e0d [file] [log] [blame]
pineafan32767212022-03-14 21:27:39 +00001import humanizeDuration from 'humanize-duration';
pineafan377794f2022-04-18 19:01:01 +01002import { purgeByUser } from '../automations/tickets/delete.js';
pineafan32767212022-03-14 21:27:39 +00003import { callback as statsChannelRemove } from '../automations/statsChannelRemove.js';
4
5export const event = 'guildMemberRemove'
6
pineafane625d782022-05-09 18:04:32 +01007export async function callback(client, member) {
8 try { await statsChannelRemove(client, member); } catch {}
9 try { purgeByUser(member.id, member.guild); } catch {}
pineafan32767212022-03-14 21:27:39 +000010 try {
pineafane625d782022-05-09 18:04:32 +010011 const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger
12 let auditLog = await getAuditLog(member.guild, 'MEMBER_KICK');
13 let audit = auditLog.entries.filter(entry => entry.target.id == member.id).first();
14 let type = "kick"
15 if (audit) {
16 if (audit.createdAt - 100 < new Date().getTime()) {
17 type = "leave"
18 } else if (audit.executor.id == client.user.id) return
19 }
20 let data
21 if (type == "kick") {
pineafan4edb7762022-06-26 19:21:04 +010022 try { await client.database.history.create("kick", member.guild.id, member.user, audit.executor, audit.reason) } catch {}
pineafane625d782022-05-09 18:04:32 +010023 data = {
24 meta: {
25 type: 'memberKick',
26 displayName: 'Member Kicked',
27 calculateType: 'guildMemberPunish',
28 color: NucleusColors.red,
29 emoji: "PUNISH.KICK.RED",
30 timestamp: new Date().getTime()
31 },
32 list: {
33 id: entry(member.id, `\`${member.id}\``),
34 name: entry(member.id, renderUser(member.user)),
35 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
36 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
37 kickedBy: entry(audit.executor.id, renderUser(audit.executor)),
38 reason: entry(audit.reason, audit.reason ? `\n> ${audit.reason}` : "*No reason provided.*"),
39 timeInServer: entry(new Date().getTime() - member.joinedAt, humanizeDuration(new Date().getTime() - member.joinedAt, { round: true })),
40 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
41 serverMemberCount: member.guild.memberCount,
42 },
43 hidden: {
44 guild: member.guild.id
45 }
46 }
47 } else {
pineafan4edb7762022-06-26 19:21:04 +010048 try { await client.database.history.create("leave", member.guild.id, member.user, null, null) } catch {}
pineafane625d782022-05-09 18:04:32 +010049 data = {
50 meta: {
51 type: 'memberLeave',
52 displayName: 'Member Left',
53 calculateType: 'guildMemberUpdate',
54 color: NucleusColors.red,
55 emoji: "MEMBER." + (member.bot ? "BOT." : "") + "LEAVE",
56 timestamp: new Date().getTime()
57 },
58 list: {
59 id: entry(member.id, `\`${member.id}\``),
60 name: entry(member.id, renderUser(member.user)),
61 joined: entry(member.joinedTimestamp, renderDelta(member.joinedAt)),
62 left: entry(new Date().getTime(), renderDelta(new Date().getTime())),
63 timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedAt, { round: true })),
64 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
65 serverMemberCount: member.guild.memberCount,
66 },
67 hidden: {
68 guild: member.guild.id
69 }
pineafan32767212022-03-14 21:27:39 +000070 }
71 }
pineafan4edb7762022-06-26 19:21:04 +010072 log(data);
pineafan32767212022-03-14 21:27:39 +000073 } catch {}
74}