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