pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import type Discord from "discord.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | // @ts-expect-error |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 3 | import type { HaikuClient } from "jshaiku"; |
| 4 | export const event = "webhookUpdate"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 5 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 6 | export async function callback( |
| 7 | client: HaikuClient, |
| 8 | channel: Discord.GuildChannel |
| 9 | ) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 10 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 11 | const { |
| 12 | getAuditLog, |
| 13 | log, |
| 14 | NucleusColors, |
| 15 | entry, |
| 16 | renderUser, |
| 17 | renderChannel, |
| 18 | renderDelta |
| 19 | } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE"); |
| 21 | let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE"); |
| 22 | let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([ |
| 24 | auditLogCreate, |
| 25 | auditLogUpdate, |
| 26 | auditLogDelete |
| 27 | ]); |
| 28 | const auditCreate = auditLogCreate.entries |
| 29 | .filter((entry) => entry.target.channelId === channel.id) |
| 30 | .first(); |
| 31 | const auditUpdate = auditLogUpdate.entries |
| 32 | .filter((entry) => entry.target.channelId === channel.id) |
| 33 | .first(); |
| 34 | const auditDelete = auditLogDelete.entries |
| 35 | .filter((entry) => entry.target.channelId === channel.id) |
| 36 | .first(); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 37 | if (!auditCreate && !auditUpdate && !auditDelete) return; |
| 38 | let audit = auditCreate; |
| 39 | let action = "Create"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 40 | let list = {} as { |
| 41 | created: { value: string; displayValue: string }; |
| 42 | updated: { value: string; displayValue: string }; |
| 43 | deleted: { value: string; displayValue: string }; |
| 44 | }; |
| 45 | if ( |
| 46 | auditUpdate && |
| 47 | auditUpdate.createdTimestamp > audit.createdTimestamp |
| 48 | ) { |
| 49 | const { before, after } = auditUpdate.changes.reduce( |
| 50 | (acc, change) => { |
| 51 | acc.before[change.key] = change.old; |
| 52 | acc.after[change.key] = change.new; |
| 53 | return acc; |
| 54 | }, |
| 55 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 56 | ); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 57 | if (before.name !== after.name) |
| 58 | list.name = entry( |
| 59 | [before.name, after.name], |
| 60 | `${before.name} -> ${after.name}` |
| 61 | ); |
| 62 | if (before.channel_id !== after.channel_id) |
| 63 | list.channel = entry( |
| 64 | [before.channel_id, after.channel_id], |
| 65 | renderChannel( |
| 66 | await client.channels.fetch(before.channel_id) |
| 67 | ) + |
| 68 | " -> " + |
| 69 | renderChannel( |
| 70 | await client.channels.fetch(after.channel_id) |
| 71 | ) |
| 72 | ); |
| 73 | if (!Object.keys(list).length) return; |
| 74 | list.created = entry( |
| 75 | auditUpdate.target.createdTimestamp, |
| 76 | renderDelta(auditUpdate.target.createdTimestamp) |
| 77 | ); |
| 78 | list.edited = entry( |
| 79 | after.editedTimestamp, |
| 80 | renderDelta(new Date().getTime()) |
| 81 | ); |
| 82 | list.editedBy = entry( |
| 83 | auditUpdate.executor.id, |
| 84 | renderUser(auditUpdate.executor) |
| 85 | ); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 86 | audit = auditUpdate; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | action = "Update"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | } else if ( |
| 89 | auditDelete && |
| 90 | auditDelete.createdTimestamp > audit.createdTimestamp |
| 91 | ) { |
| 92 | const { before } = auditDelete.changes.reduce( |
| 93 | (acc, change) => { |
| 94 | acc.before[change.key] = change.old; |
| 95 | acc.after[change.key] = change.new; |
| 96 | return acc; |
| 97 | }, |
| 98 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 99 | ); |
| 100 | list = { |
| 101 | name: entry(before.name, `${before.name}`), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | channel: entry( |
| 103 | before.channel_id, |
| 104 | renderChannel( |
| 105 | await client.channels.fetch(before.channel_id) |
| 106 | ) |
| 107 | ), |
| 108 | created: entry( |
| 109 | auditDelete.target.createdTimestamp, |
| 110 | renderDelta(auditDelete.target.createdTimestamp) |
| 111 | ), |
| 112 | deleted: entry( |
| 113 | new Date().getTime(), |
| 114 | renderDelta(new Date().getTime()) |
| 115 | ), |
| 116 | deletedBy: entry( |
| 117 | auditDelete.executor.id, |
| 118 | renderUser( |
| 119 | ( |
| 120 | await channel.guild.members.fetch( |
| 121 | auditDelete.executor.id |
| 122 | ) |
| 123 | ).user |
| 124 | ) |
| 125 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 127 | audit = auditDelete; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 128 | action = "Delete"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 129 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | const { before } = auditDelete.changes.reduce( |
| 131 | (acc, change) => { |
| 132 | acc.before[change.key] = change.old; |
| 133 | acc.after[change.key] = change.new; |
| 134 | return acc; |
| 135 | }, |
| 136 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 137 | ); |
| 138 | list = { |
| 139 | name: entry(before.name, `${before.name}`), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 140 | channel: entry( |
| 141 | before.channel_id, |
| 142 | renderChannel( |
| 143 | await client.channels.fetch(before.channel_id) |
| 144 | ) |
| 145 | ), |
| 146 | createdBy: entry( |
| 147 | auditCreate.executor.id, |
| 148 | renderUser( |
| 149 | ( |
| 150 | await channel.guild.members.fetch( |
| 151 | auditCreate.executor.id |
| 152 | ) |
| 153 | ).user |
| 154 | ) |
| 155 | ), |
| 156 | created: entry( |
| 157 | new Date().getTime(), |
| 158 | renderDelta(new Date().getTime()) |
| 159 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 160 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 161 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 162 | const cols = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 163 | Create: "green", |
| 164 | Update: "yellow", |
| 165 | Delete: "red" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 166 | }; |
| 167 | const data = { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 168 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 169 | type: "webhook" + action, |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 170 | displayName: `Webhook ${action}d`, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 171 | calculateType: "webhookUpdate", |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 172 | color: NucleusColors[cols[action]], |
| 173 | emoji: "WEBHOOK." + action.toUpperCase(), |
| 174 | timestamp: new Date().getTime() |
| 175 | }, |
| 176 | list: list, |
| 177 | hidden: { |
| 178 | guild: channel.guild.id |
| 179 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 180 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 181 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 182 | } catch (e) { |
| 183 | console.log(e); |
| 184 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 185 | } |