pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 1 | import type { GuildAuditLogsEntry, GuildMember } from "discord.js"; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame^] | 2 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 3 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | import { purgeByUser } from "../actions/tickets/delete.js"; |
| 5 | import { callback as statsChannelRemove } from "../reflex/statsChannelUpdate.js"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 6 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | export const event = "guildMemberRemove"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 8 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame^] | 9 | export async function callback(client: NucleusClient, member: GuildMember) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | purgeByUser(member.id, member.guild); |
| 11 | await statsChannelRemove(client, member); |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 12 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | const auditLog = await getAuditLog(member.guild, "MEMBER_KICK"); |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 14 | const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === member.id).first(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | let type = "leave"; |
| 16 | if (audit) { |
| 17 | if (audit.executor.id === client.user.id) return; |
| 18 | if (audit.createdAt - 100 >= new Date().getTime()) { |
| 19 | type = "kick"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 20 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | } |
| 22 | let data; |
| 23 | if (type === "kick") { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 24 | await client.database.history.create("kick", member.guild.id, member.user, audit.executor, audit.reason); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | data = { |
| 26 | meta: { |
| 27 | type: "memberKick", |
| 28 | displayName: "Member Kicked", |
| 29 | calculateType: "guildMemberPunish", |
| 30 | color: NucleusColors.red, |
| 31 | emoji: "PUNISH.KICK.RED", |
| 32 | timestamp: new Date().getTime() |
| 33 | }, |
| 34 | list: { |
| 35 | memberId: entry(member.id, `\`${member.id}\``), |
| 36 | name: entry(member.id, renderUser(member.user)), |
| 37 | joined: entry(member.joinedAt, renderDelta(member.joinedAt)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 38 | kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | kickedBy: entry(audit.executor.id, renderUser(audit.executor)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 40 | reason: entry(audit.reason, audit.reason ? `\n> ${audit.reason}` : "*No reason provided.*"), |
| 41 | accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | serverMemberCount: member.guild.memberCount |
| 43 | }, |
| 44 | hidden: { |
| 45 | guild: member.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 46 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 47 | }; |
| 48 | } else { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 49 | await client.database.history.create("leave", member.guild.id, member.user, null, null); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | data = { |
| 51 | meta: { |
| 52 | type: "memberLeave", |
| 53 | displayName: "Member Left", |
| 54 | calculateType: "guildMemberUpdate", |
| 55 | color: NucleusColors.red, |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 56 | emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | timestamp: new Date().getTime() |
| 58 | }, |
| 59 | list: { |
| 60 | memberId: entry(member.id, `\`${member.id}\``), |
| 61 | name: entry(member.id, renderUser(member.user)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 62 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedAt)), |
| 63 | left: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 64 | accountCreated: entry(member.user.createdAt, renderDelta(member.user.createdAt)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | serverMemberCount: member.guild.memberCount |
| 66 | }, |
| 67 | hidden: { |
| 68 | guild: member.guild.id |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 69 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | }; |
| 71 | } |
| 72 | log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 73 | } |