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"; |
TheCodedProf | e35c459 | 2023-02-20 12:47:12 -0500 | [diff] [blame] | 3 | import type { LoggerOptions } from "../utils/log.js"; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 4 | import { generalException } from "../utils/createTemporaryStorage.js"; |
pineafan | 1e462ab | 2023-03-07 21:34:06 +0000 | [diff] [blame] | 5 | import { doMemberChecks } from "../reflex/scanners.js"; |
pineafan | 0f5cc78 | 2022-08-12 21:55:42 +0100 | [diff] [blame] | 6 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | export const event = "guildMemberUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 8 | |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 9 | export async function callback(client: NucleusClient, before: GuildMember, after: GuildMember) { |
TheCodedProf | e35c459 | 2023-02-20 12:47:12 -0500 | [diff] [blame] | 10 | const { log, NucleusColors, entry, renderUser, renderDelta, getAuditLog } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 11 | if (before.guild.id === "684492926528651336") { |
| 12 | await client.database.premium.checkAllPremium(after); |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 13 | } |
TheCodedProf | e35c459 | 2023-02-20 12:47:12 -0500 | [diff] [blame] | 14 | |
Skyler Grey | 017c372 | 2023-03-09 20:09:57 +0000 | [diff] [blame^] | 15 | if (before.displayAvatarURL({ forceStatic: true }) !== after.displayAvatarURL({ forceStatic: true })) |
| 16 | await doMemberChecks(after); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 17 | if (!before.roles.cache.equals(after.roles.cache)) { |
| 18 | const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberRoleUpdate)).filter( |
| 19 | (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id |
| 20 | )[0]; |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 21 | if (!auditLog) return; |
| 22 | if (client.noLog.includes(`${after.guild.id}${after.id}${auditLog.id}`)) return; |
| 23 | generalException(`${after.guild.id}${after.id}${auditLog.id}`); |
| 24 | if (auditLog.executor!.id !== client.user!.id) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 25 | const rolesAdded = after.roles.cache.filter((role) => !before.roles.cache.has(role.id)); |
| 26 | const rolesRemoved = before.roles.cache.filter((role) => !after.roles.cache.has(role.id)); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 27 | let displayName = "Roles Removed"; |
| 28 | let color = NucleusColors.red; |
| 29 | let emoji = "GUILD.ROLES.DELETE"; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 30 | if (rolesAdded.size > 0 && rolesRemoved.size > 0) { |
| 31 | displayName = "Roles Changed"; |
| 32 | color = NucleusColors.yellow; |
| 33 | emoji = "GUILD.ROLES.EDIT"; |
| 34 | } else if (rolesAdded.size > 0) { |
| 35 | displayName = "Roles Added"; |
| 36 | color = NucleusColors.green; |
| 37 | emoji = "GUILD.ROLES.CREATE"; |
| 38 | } |
| 39 | const removedEntry = rolesRemoved.map((role) => role.id); |
| 40 | const addedEntry = rolesAdded.map((role) => role.id); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 41 | |
| 42 | let list = { |
| 43 | memberId: entry(after.id, `\`${after.id}\``), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 44 | name: entry(after.user.id, renderUser(after.user)) |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | if (rolesAdded.size > 0) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 48 | list = Object.assign(list, { |
| 49 | rolesAdded: entry(addedEntry, addedEntry.map((id) => `<@&${id}>`).join(", ")) |
| 50 | }); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 51 | } |
| 52 | if (rolesRemoved.size > 0) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 53 | list = Object.assign(list, { |
| 54 | rolesRemoved: entry(removedEntry, removedEntry.map((id) => `<@&${id}>`).join(", ")) |
| 55 | }); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | list = Object.assign(list, { |
| 59 | changed: entry(Date.now(), renderDelta(Date.now())), |
| 60 | changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
| 61 | }); |
| 62 | |
| 63 | let data: LoggerOptions = { |
| 64 | meta: { |
| 65 | type: "memberUpdate", |
| 66 | displayName: displayName, |
| 67 | calculateType: "guildMemberUpdate", |
| 68 | color: color, |
| 69 | emoji: emoji, |
| 70 | timestamp: Date.now() |
| 71 | }, |
| 72 | list: {}, |
| 73 | hidden: { |
| 74 | guild: after.guild.id |
| 75 | } |
| 76 | }; |
| 77 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 78 | if (rolesAdded.size > 0) { |
| 79 | list = Object.assign(list, { |
| 80 | rolesAdded: entry(addedEntry, addedEntry.map((id) => `<@&${id}>`).join(", ")) |
| 81 | }); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 82 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 83 | if (rolesRemoved.size > 0) { |
| 84 | list = Object.assign(list, { |
| 85 | rolesRemoved: entry(removedEntry, removedEntry.map((id) => `<@&${id}>`).join(", ")) |
| 86 | }); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 87 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 88 | data = Object.assign(data, { list: list }); |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 89 | await log(data); |
TheCodedProf | 1807fb3 | 2023-02-20 14:33:48 -0500 | [diff] [blame] | 90 | } |
| 91 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 92 | const auditLog = (await getAuditLog(after.guild, AuditLogEvent.MemberUpdate)).filter( |
| 93 | (entry: GuildAuditLogsEntry) => (entry.target as GuildMember)!.id === after.id |
| 94 | )[0]; |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 95 | if (!auditLog) return; |
| 96 | if (auditLog.executor!.id === client.user!.id) return; |
| 97 | if (before.nickname !== after.nickname) { |
Skyler Grey | 0d88522 | 2023-03-08 21:46:37 +0000 | [diff] [blame] | 98 | await doMemberChecks(after); |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 99 | await client.database.history.create( |
| 100 | "nickname", |
| 101 | after.guild.id, |
| 102 | after.user, |
| 103 | auditLog.executor, |
| 104 | null, |
| 105 | before.nickname ?? before.user.username, |
| 106 | after.nickname ?? after.user.username |
| 107 | ); |
| 108 | const data = { |
| 109 | meta: { |
| 110 | type: "memberUpdate", |
| 111 | displayName: "Nickname Changed", |
| 112 | calculateType: "guildMemberUpdate", |
| 113 | color: NucleusColors.yellow, |
| 114 | emoji: "PUNISH.NICKNAME.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 115 | timestamp: Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 116 | }, |
| 117 | list: { |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 118 | name: entry(after.user.id, renderUser(after.user)), |
| 119 | before: entry(before.nickname, before.nickname ? before.nickname : "*None*"), |
| 120 | after: entry(after.nickname, after.nickname ? after.nickname : "*None*"), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 121 | changed: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 122 | changedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
| 123 | }, |
| 124 | hidden: { |
| 125 | guild: after.guild.id |
| 126 | } |
| 127 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 128 | await log(data); |
TheCodedProf | e35c459 | 2023-02-20 12:47:12 -0500 | [diff] [blame] | 129 | } |
| 130 | if ( |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 131 | (before.communicationDisabledUntilTimestamp ?? 0) < Date.now() && |
pineafan | 96228bd | 2023-02-21 14:22:55 +0000 | [diff] [blame] | 132 | new Date(after.communicationDisabledUntil ?? 0).getTime() > Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 133 | ) { |
| 134 | await client.database.history.create( |
| 135 | "mute", |
| 136 | after.guild.id, |
| 137 | after.user, |
| 138 | auditLog.executor, |
| 139 | auditLog.reason, |
| 140 | null, |
| 141 | null, |
| 142 | null |
| 143 | ); |
| 144 | const data = { |
| 145 | meta: { |
| 146 | type: "memberMute", |
| 147 | displayName: "Muted", |
| 148 | calculateType: "guildMemberPunish", |
| 149 | color: NucleusColors.yellow, |
| 150 | emoji: "PUNISH.MUTE.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 151 | timestamp: Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 152 | }, |
| 153 | list: { |
| 154 | memberId: entry(after.id, `\`${after.id}\``), |
| 155 | name: entry(after.user.id, renderUser(after.user)), |
| 156 | mutedUntil: entry( |
| 157 | after.communicationDisabledUntilTimestamp, |
| 158 | renderDelta(after.communicationDisabledUntilTimestamp!) |
| 159 | ), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 160 | muted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 161 | mutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)), |
| 162 | reason: entry(auditLog.reason, auditLog.reason ? auditLog.reason : "\n> *No reason provided*") |
| 163 | }, |
| 164 | hidden: { |
| 165 | guild: after.guild.id |
| 166 | } |
| 167 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 168 | await log(data); |
Skyler Grey | c6a9366 | 2023-03-08 16:13:39 +0000 | [diff] [blame] | 169 | await client.database.eventScheduler.schedule( |
| 170 | "naturalUnmute", |
| 171 | after.communicationDisabledUntil?.toISOString()!, |
| 172 | { |
| 173 | guild: after.guild.id, |
| 174 | user: after.id, |
| 175 | expires: after.communicationDisabledUntilTimestamp |
| 176 | } |
| 177 | ); |
TheCodedProf | e35c459 | 2023-02-20 12:47:12 -0500 | [diff] [blame] | 178 | } |
| 179 | if ( |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 180 | after.communicationDisabledUntil === null && |
| 181 | before.communicationDisabledUntilTimestamp !== null && |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 182 | Date.now() >= auditLog.createdTimestamp |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 183 | ) { |
| 184 | await client.database.history.create( |
| 185 | "unmute", |
| 186 | after.guild.id, |
| 187 | after.user, |
| 188 | auditLog.executor, |
| 189 | null, |
| 190 | null, |
| 191 | null, |
| 192 | null |
| 193 | ); |
| 194 | const data = { |
| 195 | meta: { |
| 196 | type: "memberUnmute", |
| 197 | displayName: "Unmuted", |
| 198 | calculateType: "guildMemberPunish", |
| 199 | color: NucleusColors.green, |
| 200 | emoji: "PUNISH.MUTE.GREEN", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 201 | timestamp: Date.now() |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 202 | }, |
| 203 | list: { |
| 204 | memberId: entry(after.id, `\`${after.id}\``), |
| 205 | name: entry(after.user.id, renderUser(after.user)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 206 | unmuted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 207 | unmutedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
| 208 | }, |
| 209 | hidden: { |
| 210 | guild: after.guild.id |
| 211 | } |
| 212 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 213 | await log(data); |
| 214 | await client.database.eventScheduler.cancel("naturalUnmute", { |
PineaFan | c4d6c3f | 2023-01-19 12:17:25 +0000 | [diff] [blame] | 215 | guild: after.guild.id, |
| 216 | user: after.id, |
| 217 | expires: before.communicationDisabledUntilTimestamp |
| 218 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 219 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 220 | } |