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