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