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