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