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)) |
| 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") { |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 24 | 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] | 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)), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 37 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 38 | kicked: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 39 | kickedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)), |
| 40 | reason: entry(auditLog.reason, auditLog.reason ? `\n> ${auditLog.reason}` : "*No reason provided.*"), |
| 41 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
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)), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 62 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 63 | left: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame^] | 64 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
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 | } |