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