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