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