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