PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { AuditLogEvent, 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 | export const event = "guildMemberUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 5 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 6 | export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) { |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 7 | const { log, isLogging, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger; |
| 8 | if (!await isLogging(after.guild.id, "memberUpdate")) return; |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 9 | const auditLog = (await getAuditLog(after.guild, AuditLogEvent.EmojiCreate)) |
| 10 | .filter((entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id)[0]; |
| 11 | if (!auditLog) return; |
| 12 | if (auditLog.executor!.id === client.user!.id) return; |
| 13 | if (before.nickname !== after.nickname) { |
| 14 | await client.database.history.create( |
| 15 | "nickname", |
| 16 | after.guild.id, |
| 17 | after.user, |
| 18 | auditLog.executor, |
| 19 | null, |
| 20 | before.nickname ?? before.user.username, |
| 21 | after.nickname ?? after.user.username |
| 22 | ); |
| 23 | const data = { |
| 24 | meta: { |
| 25 | type: "memberUpdate", |
| 26 | displayName: "Nickname Changed", |
| 27 | calculateType: "guildMemberUpdate", |
| 28 | color: NucleusColors.yellow, |
| 29 | emoji: "PUNISH.NICKNAME.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 30 | timestamp: Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 31 | }, |
| 32 | list: { |
| 33 | memberId: entry(after.id, `\`${after.id}\``), |
| 34 | name: entry(after.user.id, renderUser(after.user)), |
| 35 | before: entry(before.nickname, before.nickname ? before.nickname : "*None*"), |
| 36 | after: entry(after.nickname, after.nickname ? after.nickname : "*None*"), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 37 | changed: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 38 | changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
| 39 | }, |
| 40 | hidden: { |
| 41 | guild: after.guild.id |
| 42 | } |
| 43 | }; |
| 44 | log(data); |
| 45 | } else if ( |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 46 | (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() && |
| 47 | (after.communicationDisabledUntil ?? 0) > Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 48 | ) { |
| 49 | await client.database.history.create( |
| 50 | "mute", |
| 51 | after.guild.id, |
| 52 | after.user, |
| 53 | auditLog.executor, |
| 54 | auditLog.reason, |
| 55 | null, |
| 56 | null, |
| 57 | null |
| 58 | ); |
| 59 | const data = { |
| 60 | meta: { |
| 61 | type: "memberMute", |
| 62 | displayName: "Muted", |
| 63 | calculateType: "guildMemberPunish", |
| 64 | color: NucleusColors.yellow, |
| 65 | emoji: "PUNISH.MUTE.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 66 | timestamp: Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 67 | }, |
| 68 | list: { |
| 69 | memberId: entry(after.id, `\`${after.id}\``), |
| 70 | name: entry(after.user.id, renderUser(after.user)), |
| 71 | mutedUntil: entry( |
| 72 | after.communicationDisabledUntilTimestamp, |
| 73 | renderDelta(after.communicationDisabledUntilTimestamp!) |
| 74 | ), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 75 | muted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 76 | mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)), |
| 77 | reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*") |
| 78 | }, |
| 79 | hidden: { |
| 80 | guild: after.guild.id |
| 81 | } |
| 82 | }; |
| 83 | log(data); |
| 84 | client.database.eventScheduler.schedule("naturalUnmute", after.communicationDisabledUntil?.toISOString()!, { |
| 85 | guild: after.guild.id, |
| 86 | user: after.id, |
| 87 | expires: after.communicationDisabledUntilTimestamp |
| 88 | }); |
| 89 | } else if ( |
| 90 | after.communicationDisabledUntil === null && |
| 91 | before.communicationDisabledUntilTimestamp !== null && |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 92 | Date.now() >= auditLog.createdTimestamp |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 93 | ) { |
| 94 | await client.database.history.create( |
| 95 | "unmute", |
| 96 | after.guild.id, |
| 97 | after.user, |
| 98 | auditLog.executor, |
| 99 | null, |
| 100 | null, |
| 101 | null, |
| 102 | null |
| 103 | ); |
| 104 | const data = { |
| 105 | meta: { |
| 106 | type: "memberUnmute", |
| 107 | displayName: "Unmuted", |
| 108 | calculateType: "guildMemberPunish", |
| 109 | color: NucleusColors.green, |
| 110 | emoji: "PUNISH.MUTE.GREEN", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 111 | timestamp: Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 112 | }, |
| 113 | list: { |
| 114 | memberId: entry(after.id, `\`${after.id}\``), |
| 115 | name: entry(after.user.id, renderUser(after.user)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame^] | 116 | unmuted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 117 | unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
| 118 | }, |
| 119 | hidden: { |
| 120 | guild: after.guild.id |
| 121 | } |
| 122 | }; |
| 123 | log(data); |
| 124 | client.database.eventScheduler.cancel("naturalUnmute", { |
| 125 | guild: after.guild.id, |
| 126 | user: after.id, |
| 127 | expires: before.communicationDisabledUntilTimestamp |
| 128 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 129 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 130 | } |