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 | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 10 | const startTime = Date.now() - 10 * 1000; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 11 | purgeByUser(member.id, member.guild.id); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 12 | await statsChannelRemove(client, member); |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 13 | const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderDelta } = client.logger; |
| 14 | if (!await isLogging(member.guild.id, "guildMemberUpdate")) return; |
| 15 | const kickAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberKick)) |
| 16 | .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id)[0]; |
| 17 | const banAuditLog = (await getAuditLog(member.guild as Guild, AuditLogEvent.MemberBanAdd)) |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 18 | .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === member.id)[0]; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | let type = "leave"; |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 20 | if (kickAuditLog) { |
| 21 | if (kickAuditLog.executor!.id === client.user!.id) return; |
| 22 | if (kickAuditLog.createdAt.getTime() >= startTime) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | type = "kick"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 24 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | } |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 26 | if (banAuditLog) { |
| 27 | if (banAuditLog.executor!.id === client.user!.id) return; |
| 28 | if (banAuditLog.createdAt.getTime() >= startTime) { |
| 29 | if (!kickAuditLog) { |
| 30 | return |
| 31 | } else if (kickAuditLog.createdAt.valueOf() < banAuditLog.createdAt.valueOf()) { |
| 32 | return |
| 33 | } |
| 34 | } |
| 35 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | let data; |
| 37 | if (type === "kick") { |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 38 | if (!kickAuditLog) return; |
| 39 | await client.database.history.create("kick", member.guild.id, member.user, kickAuditLog.executor, kickAuditLog.reason); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | data = { |
| 41 | meta: { |
| 42 | type: "memberKick", |
| 43 | displayName: "Member Kicked", |
| 44 | calculateType: "guildMemberPunish", |
| 45 | color: NucleusColors.red, |
| 46 | emoji: "PUNISH.KICK.RED", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 47 | timestamp: Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 48 | }, |
| 49 | list: { |
| 50 | memberId: entry(member.id, `\`${member.id}\``), |
| 51 | name: entry(member.id, renderUser(member.user)), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 52 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 53 | kicked: entry(Date.now(), renderDelta(Date.now())), |
| 54 | kickedBy: entry(kickAuditLog.executor!.id, renderUser(kickAuditLog.executor!)), |
| 55 | reason: entry(kickAuditLog.reason, kickAuditLog.reason ? `\n> ${kickAuditLog.reason}` : "*No reason provided.*"), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 56 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | serverMemberCount: member.guild.memberCount |
| 58 | }, |
| 59 | hidden: { |
| 60 | guild: member.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 61 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | }; |
| 63 | } else { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 64 | await client.database.history.create("leave", member.guild.id, member.user, null, null); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 65 | data = { |
| 66 | meta: { |
| 67 | type: "memberLeave", |
| 68 | displayName: "Member Left", |
| 69 | calculateType: "guildMemberUpdate", |
| 70 | color: NucleusColors.red, |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 71 | emoji: "MEMBER." + (member.user.bot ? "BOT." : "") + "LEAVE", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 72 | timestamp: Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | }, |
| 74 | list: { |
| 75 | memberId: entry(member.id, `\`${member.id}\``), |
| 76 | name: entry(member.id, renderUser(member.user)), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 77 | joined: entry(member.joinedTimestamp, renderDelta(member.joinedTimestamp?.valueOf()!)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 78 | left: entry(Date.now(), renderDelta(Date.now())), |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 79 | accountCreated: entry(member.user.createdTimestamp, renderDelta(member.user.createdTimestamp)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 80 | serverMemberCount: member.guild.memberCount |
| 81 | }, |
| 82 | hidden: { |
| 83 | guild: member.guild.id |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 84 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | }; |
| 86 | } |
| 87 | log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 88 | } |