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