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