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