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