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 | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 13 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger; |
TheCodedProf | a16d167 | 2023-01-18 18:58:34 -0500 | [diff] [blame] | 14 | const auditCreate = (await getAuditLog(channel.guild, AuditLogEvent.WebhookCreate)) |
| 15 | .filter((entry: GuildAuditLogsEntry) => (entry.target as Webhook)!.id === channel.id)[0] as GuildAuditLogsEntry; |
| 16 | const auditDelete = (await getAuditLog(channel.guild, AuditLogEvent.WebhookDelete)) |
| 17 | .filter((entry: GuildAuditLogsEntry) => (entry.target as Webhook)!.id === channel.id)[0] as GuildAuditLogsEntry; |
| 18 | const auditUpdate = (await getAuditLog(channel.guild, AuditLogEvent.WebhookUpdate)) |
| 19 | .filter((entry: GuildAuditLogsEntry) => (entry.target as Webhook)!.id === channel.id)[0] as GuildAuditLogsEntry; |
| 20 | |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 21 | if (!auditCreate && !auditUpdate && !auditDelete) return; |
| 22 | let audit = auditCreate; |
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> = {}; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 25 | if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) { |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 26 | const { before, after } = auditUpdate.changes.reduce((acc: accType, change) => { |
| 27 | acc.before[change.key] = change.old?.toString()!; |
| 28 | acc.after[change.key] = change.new?.toString()!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 29 | return acc; |
| 30 | }, |
| 31 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 32 | ); |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 33 | if (before["name"] !== after["name"]) |
| 34 | list["name"] = entry([before["name"]!, after["name"]!], `${before["name"]} -> ${after["name"]}`); |
| 35 | if (before["channel_id"] !== after["channel_id"]) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 36 | list["channel"] = entry( |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 37 | [before["channel_id"]!, after["channel_id"]!], |
| 38 | renderChannel(await client.channels.fetch(before["channel_id"]!) as GuildChannel) + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | " -> " + |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 40 | renderChannel(await client.channels.fetch(after["channel_id"]!) as GuildChannel) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 41 | ); |
| 42 | if (!Object.keys(list).length) return; |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 43 | list["created"] = entry( |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 44 | (auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp, |
| 45 | renderDelta((auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp) |
Skyler Grey | f21323a | 2022-08-13 23:58:22 +0100 | [diff] [blame] | 46 | ); |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 47 | list["edited"] = entry(after["editedTimestamp"]!, renderDelta(new Date().getTime())); |
| 48 | list["editedBy"] = entry(auditUpdate.executor!.id, renderUser(auditUpdate.executor!)); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 49 | audit = auditUpdate; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | action = "Update"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 51 | } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) { |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 52 | const { before } = auditDelete.changes.reduce((acc: accType, change) => { |
| 53 | acc.before[change.key] = change.old?.toString()!; |
| 54 | acc.after[change.key] = change.new?.toString()!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | return acc; |
| 56 | }, |
| 57 | { before: {}, after: {} } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 58 | ); |
| 59 | list = { |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 60 | name: entry(before["name"]!, `${before["name"]}`), |
| 61 | channel: entry(before["channel_id"]!, renderChannel((await client.channels.fetch(before["channel_id"]!)) as GuildChannel)), |
| 62 | created: entry((auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp, renderDelta((auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp)), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 63 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | deletedBy: entry( |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 65 | auditDelete.executor!.id, |
| 66 | renderUser((await channel.guild.members.fetch(auditDelete.executor!.id)).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 69 | audit = auditDelete; |
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 { |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 72 | const { before } = auditDelete.changes.reduce((acc: accType, change) => { |
| 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 | 9812b70 | 2023-01-18 21:01:04 -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 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 86 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
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", |
TheCodedProf | 9812b70 | 2023-01-18 21:01:04 -0500 | [diff] [blame^] | 99 | color: (NucleusColors as any)[cols[action]], |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 100 | emoji: "WEBHOOK." + action.toUpperCase(), |
| 101 | timestamp: new Date().getTime() |
| 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 | } |