TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 1 | import { AuditLogEvent, Guild, 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) { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 10 | purgeByUser(member.id, member.guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 13 | const auditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberKick)) |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 14 | .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id)[0]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | let type = "leave"; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 16 | if (auditLog) { |
| 17 | if (auditLog.executor!.id === client.user!.id) return; |
| 18 | if (auditLog.createdAt.valueOf() - 100 >= new Date().getTime()) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 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") { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame^] | 24 | if (!auditLog) return; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 25 | await client.database.history.create("kick", member.guild.id, member.user, auditLog.executor, auditLog.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)), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 38 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 39 | kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 40 | kickedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)), |
| 41 | reason: entry(auditLog.reason, auditLog.reason ? `\n> ${auditLog.reason}` : "*No reason provided.*"), |
| 42 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
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)), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 63 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 64 | left: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 65 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
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 | } |