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