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 | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 6 | export async function callback(client: HaikuClient, channel: Discord.GuildChannel) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 7 | try { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 8 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 9 | let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE"); |
| 10 | let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE"); |
| 11 | let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([ |
| 13 | auditLogCreate, |
| 14 | auditLogUpdate, |
| 15 | auditLogDelete |
| 16 | ]); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 17 | const auditCreate = auditLogCreate.entries.filter((entry) => entry.target.channelId === channel.id).first(); |
| 18 | const auditUpdate = auditLogUpdate.entries.filter((entry) => entry.target.channelId === channel.id).first(); |
| 19 | const auditDelete = auditLogDelete.entries.filter((entry) => entry.target.channelId === channel.id).first(); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 20 | if (!auditCreate && !auditUpdate && !auditDelete) return; |
| 21 | let audit = auditCreate; |
| 22 | let action = "Create"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | let list = {} as { |
| 24 | created: { value: string; displayValue: string }; |
| 25 | updated: { value: string; displayValue: string }; |
| 26 | deleted: { value: string; displayValue: string }; |
| 27 | }; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 28 | if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 29 | const { before, after } = auditUpdate.changes.reduce( |
| 30 | (acc, change) => { |
| 31 | acc.before[change.key] = change.old; |
| 32 | acc.after[change.key] = change.new; |
| 33 | return acc; |
| 34 | }, |
| 35 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 36 | ); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 37 | if (before.name !== after.name) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 38 | list.name = entry([before.name, after.name], `${before.name} -> ${after.name}`); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | if (before.channel_id !== after.channel_id) |
| 40 | list.channel = entry( |
| 41 | [before.channel_id, after.channel_id], |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 42 | renderChannel(await client.channels.fetch(before.channel_id)) + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 43 | " -> " + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 44 | renderChannel(await client.channels.fetch(after.channel_id)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 45 | ); |
| 46 | if (!Object.keys(list).length) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 47 | list.created = entry(auditUpdate.target.createdTimestamp, renderDelta(auditUpdate.target.createdTimestamp)); |
| 48 | list.edited = entry(after.editedTimestamp, renderDelta(new Date().getTime())); |
| 49 | list.editedBy = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor)); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 50 | audit = auditUpdate; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | action = "Update"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 52 | } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | const { before } = auditDelete.changes.reduce( |
| 54 | (acc, change) => { |
| 55 | acc.before[change.key] = change.old; |
| 56 | acc.after[change.key] = change.new; |
| 57 | return acc; |
| 58 | }, |
| 59 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 60 | ); |
| 61 | list = { |
| 62 | name: entry(before.name, `${before.name}`), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 63 | channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))), |
| 64 | created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)), |
| 65 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | deletedBy: entry( |
| 67 | auditDelete.executor.id, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 68 | renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 71 | audit = auditDelete; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | action = "Delete"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 73 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 74 | const { before } = auditDelete.changes.reduce( |
| 75 | (acc, change) => { |
| 76 | acc.before[change.key] = change.old; |
| 77 | acc.after[change.key] = change.new; |
| 78 | return acc; |
| 79 | }, |
| 80 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 81 | ); |
| 82 | list = { |
| 83 | name: entry(before.name, `${before.name}`), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 84 | channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | createdBy: entry( |
| 86 | auditCreate.executor.id, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 87 | renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 89 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 90 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 91 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 92 | const cols = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | Create: "green", |
| 94 | Update: "yellow", |
| 95 | Delete: "red" |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 96 | }; |
| 97 | const data = { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 98 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 99 | type: "webhook" + action, |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 100 | displayName: `Webhook ${action}d`, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 101 | calculateType: "webhookUpdate", |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 102 | color: NucleusColors[cols[action]], |
| 103 | emoji: "WEBHOOK." + action.toUpperCase(), |
| 104 | timestamp: new Date().getTime() |
| 105 | }, |
| 106 | list: list, |
| 107 | hidden: { |
| 108 | guild: channel.guild.id |
| 109 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 110 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 111 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | } catch (e) { |
| 113 | console.log(e); |
| 114 | } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 115 | } |