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