blob: 18b6d46050cd6565a6edf508f8afaf4dee8de8cd [file] [log] [blame]
TheCodedProfa16d1672023-01-18 18:58:34 -05001import { AuditLogEvent, GuildAuditLogsEntry, Webhook } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import type Discord from "discord.js";
PineaFan752af462022-12-31 21:59:38 +00003import type { NucleusClient } from "../utils/client.js";
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "webhookUpdate";
pineafanda6e5342022-07-03 10:03:16 +01005
PineaFan752af462022-12-31 21:59:38 +00006export async function callback(client: NucleusClient, channel: Discord.GuildChannel) {
pineafanda6e5342022-07-03 10:03:16 +01007 try {
Skyler Grey11236ba2022-08-08 21:13:33 +01008 const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
TheCodedProfa16d1672023-01-18 18:58:34 -05009 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
pineafanda6e5342022-07-03 10:03:16 +010016 if (!auditCreate && !auditUpdate && !auditDelete) return;
17 let audit = auditCreate;
pineafan3a02ea32022-08-11 21:35:04 +010018 let action: "Create" | "Update" | "Delete" = "Create";
19 let list: Record<string, ReturnType<typeof entry> | string> = {};
Skyler Grey11236ba2022-08-08 21:13:33 +010020 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010021 const { before, after } = auditUpdate.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010022 (
23 acc: { before: Record<string, string>; after: Record<string, string> },
24 change: { key: string; new: string; old: string }
25 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010026 acc.before[change.key] = change.old;
27 acc.after[change.key] = change.new;
28 return acc;
29 },
30 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010031 );
Skyler Grey75ea9172022-08-06 10:22:23 +010032 if (before.name !== after.name)
pineafan3a02ea32022-08-11 21:35:04 +010033 list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010034 if (before.channel_id !== after.channel_id)
pineafan3a02ea32022-08-11 21:35:04 +010035 list["channel"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010036 [before.channel_id, after.channel_id],
Skyler Grey11236ba2022-08-08 21:13:33 +010037 renderChannel(await client.channels.fetch(before.channel_id)) +
Skyler Grey75ea9172022-08-06 10:22:23 +010038 " -> " +
Skyler Grey11236ba2022-08-08 21:13:33 +010039 renderChannel(await client.channels.fetch(after.channel_id))
Skyler Grey75ea9172022-08-06 10:22:23 +010040 );
41 if (!Object.keys(list).length) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010042 list["created"] = entry(
43 auditUpdate.target.createdTimestamp,
44 renderDelta(auditUpdate.target.createdTimestamp)
45 );
pineafan3a02ea32022-08-11 21:35:04 +010046 list["edited"] = entry(after.editedTimestamp, renderDelta(new Date().getTime()));
47 list["editedBy"] = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor));
pineafanda6e5342022-07-03 10:03:16 +010048 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010049 action = "Update";
Skyler Grey11236ba2022-08-08 21:13:33 +010050 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010051 const { before } = auditDelete.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010052 (
53 acc: { before: Record<string, string>; after: Record<string, string> },
54 change: { key: string; new: string; old: string }
55 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 acc.before[change.key] = change.old;
57 acc.after[change.key] = change.new;
58 return acc;
59 },
60 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010061 );
62 list = {
63 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010064 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 Grey75ea9172022-08-06 10:22:23 +010067 deletedBy: entry(
68 auditDelete.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010069 renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010070 )
pineafan63fc5e22022-08-04 22:04:10 +010071 };
pineafanda6e5342022-07-03 10:03:16 +010072 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010073 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010074 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 const { before } = auditDelete.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010076 (
77 acc: { before: Record<string, string>; after: Record<string, string> },
78 change: { key: string; new: string; old: string }
79 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010080 acc.before[change.key] = change.old;
81 acc.after[change.key] = change.new;
82 return acc;
83 },
84 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010085 );
86 list = {
87 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010088 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
Skyler Grey75ea9172022-08-06 10:22:23 +010089 createdBy: entry(
90 auditCreate.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010091 renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010092 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010093 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan63fc5e22022-08-04 22:04:10 +010094 };
pineafanda6e5342022-07-03 10:03:16 +010095 }
pineafan63fc5e22022-08-04 22:04:10 +010096 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +010097 Create: "green",
98 Update: "yellow",
99 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100100 };
101 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100102 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100103 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100104 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100105 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100106 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 }
pineafan63fc5e22022-08-04 22:04:10 +0100114 };
pineafanda6e5342022-07-03 10:03:16 +0100115 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 } catch (e) {
117 console.log(e);
118 }
pineafanda6e5342022-07-03 10:03:16 +0100119}