blob: 43e78bd87183ad758538a627a6c00c8ada5981dc [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();
pineafan52906cd2022-06-29 22:02:36 +010014 let type = "leave"
pineafane625d782022-05-09 18:04:32 +010015 if (audit) {
pineafan52906cd2022-06-29 22:02:36 +010016 if (audit.executor.id === client.user.id) return
17 if (audit.createdAt - 100 >= new Date().getTime()) {
18 type = "kick"
19 }
pineafane625d782022-05-09 18:04:32 +010020 }
21 let data
22 if (type == "kick") {
pineafan4edb7762022-06-26 19:21:04 +010023 try { await client.database.history.create("kick", member.guild.id, member.user, audit.executor, audit.reason) } catch {}
pineafane625d782022-05-09 18:04:32 +010024 data = {
25 meta: {
26 type: 'memberKick',
27 displayName: 'Member Kicked',
28 calculateType: 'guildMemberPunish',
29 color: NucleusColors.red,
30 emoji: "PUNISH.KICK.RED",
31 timestamp: new Date().getTime()
32 },
33 list: {
34 id: entry(member.id, `\`${member.id}\``),
35 name: entry(member.id, renderUser(member.user)),
36 joined: entry(member.joinedAt, renderDelta(member.joinedAt)),
37 kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())),
38 kickedBy: entry(audit.executor.id, renderUser(audit.executor)),
39 reason: entry(audit.reason, audit.reason ? `\n> ${audit.reason}` : "*No reason provided.*"),
40 timeInServer: entry(new Date().getTime() - member.joinedAt, humanizeDuration(new Date().getTime() - member.joinedAt, { round: true })),
41 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
42 serverMemberCount: member.guild.memberCount,
43 },
44 hidden: {
45 guild: member.guild.id
46 }
47 }
48 } else {
pineafan4edb7762022-06-26 19:21:04 +010049 try { await client.database.history.create("leave", member.guild.id, member.user, null, null) } catch {}
pineafane625d782022-05-09 18:04:32 +010050 data = {
51 meta: {
52 type: 'memberLeave',
53 displayName: 'Member Left',
54 calculateType: 'guildMemberUpdate',
55 color: NucleusColors.red,
56 emoji: "MEMBER." + (member.bot ? "BOT." : "") + "LEAVE",
57 timestamp: new Date().getTime()
58 },
59 list: {
60 id: entry(member.id, `\`${member.id}\``),
61 name: entry(member.id, renderUser(member.user)),
62 joined: entry(member.joinedTimestamp, renderDelta(member.joinedAt)),
63 left: entry(new Date().getTime(), renderDelta(new Date().getTime())),
64 timeInServer: entry(new Date().getTime() - member.joinedTimestamp, humanizeDuration(new Date().getTime() - member.joinedAt, { round: true })),
65 accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)),
66 serverMemberCount: member.guild.memberCount,
67 },
68 hidden: {
69 guild: member.guild.id
70 }
pineafan32767212022-03-14 21:27:39 +000071 }
72 }
pineafan4edb7762022-06-26 19:21:04 +010073 log(data);
pineafan52906cd2022-06-29 22:02:36 +010074 } catch (e) { console.log(e) }
pineafan32767212022-03-14 21:27:39 +000075}