pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 1 | import humanizeDuration from 'humanize-duration'; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 2 | import { purgeByUser } from '../automations/tickets/delete.js'; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 3 | import { callback as statsChannelRemove } from '../automations/statsChannelRemove.js'; |
| 4 | |
| 5 | export const event = 'guildMemberRemove' |
| 6 | |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 7 | export async function callback(client, member) { |
| 8 | try { await statsChannelRemove(client, member); } catch {} |
| 9 | try { purgeByUser(member.id, member.guild); } catch {} |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 10 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 11 | 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(); |
pineafan | 52906cd | 2022-06-29 22:02:36 +0100 | [diff] [blame] | 14 | let type = "leave" |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 15 | if (audit) { |
pineafan | 52906cd | 2022-06-29 22:02:36 +0100 | [diff] [blame] | 16 | if (audit.executor.id === client.user.id) return |
| 17 | if (audit.createdAt - 100 >= new Date().getTime()) { |
| 18 | type = "kick" |
| 19 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 20 | } |
| 21 | let data |
| 22 | if (type == "kick") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 23 | try { await client.database.history.create("kick", member.guild.id, member.user, audit.executor, audit.reason) } catch {} |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 24 | 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: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 34 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 49 | try { await client.database.history.create("leave", member.guild.id, member.user, null, null) } catch {} |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 50 | 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: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 60 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 73 | log(data); |
pineafan | 52906cd | 2022-06-29 22:02:36 +0100 | [diff] [blame] | 74 | } catch (e) { console.log(e) } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 75 | } |