pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | export const event = "guildMemberUpdate"; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 2 | |
| 3 | export async function callback(client, before, after) { |
| 4 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 5 | const { |
| 6 | log, |
| 7 | NucleusColors, |
| 8 | entry, |
| 9 | renderUser, |
| 10 | renderDelta, |
| 11 | getAuditLog |
| 12 | } = after.client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | const auditLog = await getAuditLog(after.guild, "MEMBER_UPDATE"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 14 | const audit = auditLog.entries |
| 15 | .filter((entry) => entry.target.id === after.id) |
| 16 | .first(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 17 | if (audit.executor.id === client.user.id) return; |
| 18 | if (before.nickname !== after.nickname) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 19 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 20 | "nickname", |
| 21 | after.guild.id, |
| 22 | after.user, |
| 23 | audit.executor, |
| 24 | null, |
| 25 | before.nickname || before.user.username, |
| 26 | after.nickname || after.user.username |
| 27 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | const data = { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 29 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | type: "memberUpdate", |
| 31 | displayName: "Nickname Changed", |
| 32 | calculateType: "guildMemberUpdate", |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 33 | color: NucleusColors.yellow, |
| 34 | emoji: "PUNISH.NICKNAME.YELLOW", |
| 35 | timestamp: new Date().getTime() |
| 36 | }, |
| 37 | list: { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 38 | memberId: entry(after.id, `\`${after.id}\``), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 39 | name: entry(after.user.id, renderUser(after.user)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 40 | before: entry( |
| 41 | before.nickname, |
| 42 | before.nickname ? before.nickname : "*None*" |
| 43 | ), |
| 44 | after: entry( |
| 45 | after.nickname, |
| 46 | after.nickname ? after.nickname : "*None*" |
| 47 | ), |
| 48 | changed: entry( |
| 49 | new Date().getTime(), |
| 50 | renderDelta(new Date().getTime()) |
| 51 | ), |
| 52 | changedBy: entry( |
| 53 | audit.executor.id, |
| 54 | renderUser(audit.executor) |
| 55 | ) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 56 | }, |
| 57 | hidden: { |
| 58 | guild: after.guild.id |
| 59 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 61 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 62 | } else if ( |
| 63 | before.communicationDisabledUntilTimestamp < new Date().getTime() && |
| 64 | after.communicationDisabledUntil > new Date().getTime() |
| 65 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 67 | "mute", |
| 68 | after.guild.id, |
| 69 | after.user, |
| 70 | audit.executor, |
| 71 | audit.reason, |
| 72 | null, |
| 73 | null, |
| 74 | null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | ); |
| 76 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 77 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | type: "memberMute", |
| 79 | displayName: "Muted", |
| 80 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 81 | color: NucleusColors.yellow, |
| 82 | emoji: "PUNISH.MUTE.YELLOW", |
| 83 | timestamp: new Date().getTime() |
| 84 | }, |
| 85 | list: { |
| 86 | memberId: entry(after.id, `\`${after.id}\``), |
| 87 | name: entry(after.user.id, renderUser(after.user)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 88 | mutedUntil: entry( |
| 89 | after.communicationDisabledUntilTimestamp, |
| 90 | renderDelta(after.communicationDisabledUntilTimestamp) |
| 91 | ), |
| 92 | muted: entry( |
| 93 | new Date().getTime(), |
| 94 | renderDelta(new Date().getTime()) |
| 95 | ), |
| 96 | mutedBy: entry( |
| 97 | audit.executor.id, |
| 98 | renderUser(audit.executor) |
| 99 | ), |
| 100 | reason: entry( |
| 101 | audit.reason, |
| 102 | audit.reason ? audit.reason : "\n> *No reason provided*" |
| 103 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 104 | }, |
| 105 | hidden: { |
| 106 | guild: after.guild.id |
| 107 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 109 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 110 | client.database.eventScheduler.schedule( |
| 111 | "naturalUnmute", |
| 112 | after.communicationDisabledUntil, |
| 113 | { |
| 114 | guild: after.guild.id, |
| 115 | user: after.id, |
| 116 | expires: after.communicationDisabledUntilTimestamp |
| 117 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 118 | ); |
| 119 | } else if ( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 120 | after.communicationDisabledUntil === null && |
| 121 | before.communicationDisabledUntilTimestamp !== null && |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 122 | new Date().getTime() >= audit.createdTimestamp |
| 123 | ) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 124 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 125 | "unmute", |
| 126 | after.guild.id, |
| 127 | after.user, |
| 128 | audit.executor, |
| 129 | null, |
| 130 | null, |
| 131 | null, |
| 132 | null |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | ); |
| 134 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 135 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | type: "memberUnmute", |
| 137 | displayName: "Unmuted", |
| 138 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 139 | color: NucleusColors.green, |
| 140 | emoji: "PUNISH.MUTE.GREEN", |
| 141 | timestamp: new Date().getTime() |
| 142 | }, |
| 143 | list: { |
| 144 | memberId: entry(after.id, `\`${after.id}\``), |
| 145 | name: entry(after.user.id, renderUser(after.user)), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 146 | unmuted: entry( |
| 147 | new Date().getTime(), |
| 148 | renderDelta(new Date().getTime()) |
| 149 | ), |
| 150 | unmutedBy: entry( |
| 151 | audit.executor.id, |
| 152 | renderUser(audit.executor) |
| 153 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 154 | }, |
| 155 | hidden: { |
| 156 | guild: after.guild.id |
| 157 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 158 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 159 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 160 | client.database.eventScheduler.cancel("naturalUnmute", { |
| 161 | guild: after.guild.id, |
| 162 | user: after.id, |
| 163 | expires: before.communicationDisabledUntilTimestamp |
| 164 | }); |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 165 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame^] | 166 | } catch (e) { |
| 167 | console.log(e); |
| 168 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 169 | } |