pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 1 | import humanizeDuration from 'humanize-duration'; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 2 | import { purgeByUser } from '../actions/tickets/delete.js'; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 3 | import { callback as statsChannelRemove } from '../reflex/statsChannelUpdate.js'; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 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) { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 8 | try { purgeByUser(member.id, member.guild); } catch {} |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 9 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 10 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = member.client.logger |
| 11 | let auditLog = await getAuditLog(member.guild, 'MEMBER_KICK'); |
| 12 | let audit = auditLog.entries.filter(entry => entry.target.id == member.id).first(); |
pineafan | 52906cd | 2022-06-29 22:02:36 +0100 | [diff] [blame] | 13 | let type = "leave" |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 14 | if (audit) { |
pineafan | 52906cd | 2022-06-29 22:02:36 +0100 | [diff] [blame] | 15 | if (audit.executor.id === client.user.id) return |
| 16 | if (audit.createdAt - 100 >= new Date().getTime()) { |
| 17 | type = "kick" |
| 18 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 19 | } |
| 20 | let data |
| 21 | if (type == "kick") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 22 | 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] | 23 | 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: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 33 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 48 | 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] | 49 | 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: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 59 | memberId: entry(member.id, `\`${member.id}\``), |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 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 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 72 | log(data); |
pineafan | 52906cd | 2022-06-29 22:02:36 +0100 | [diff] [blame] | 73 | } catch (e) { console.log(e) } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 74 | try { await statsChannelRemove(client, member); } catch {} |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 75 | } |