blob: 004f7542ab9fda3dcf08159e2bb710ec401cfa68 [file] [log] [blame]
TheCodedProf9812b702023-01-18 21:01:04 -05001import { AuditLogEvent, GuildAuditLogsEntry, GuildChannel, 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
TheCodedProf9812b702023-01-18 21:01:04 -05006interface accType {
7 before: Record<string, string>;
8 after: Record<string, string>;
9}
10
PineaFan752af462022-12-31 21:59:38 +000011export async function callback(client: NucleusClient, channel: Discord.GuildChannel) {
pineafanda6e5342022-07-03 10:03:16 +010012 try {
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050013 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
14 if (!await isLogging(channel.guild.id, "webhookUpdate")) return;
TheCodedProfa16d1672023-01-18 18:58:34 -050015 const auditCreate = (await getAuditLog(channel.guild, AuditLogEvent.WebhookCreate))
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050016 .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;
pineafan3a02ea32022-08-11 21:35:04 +010022 let action: "Create" | "Update" | "Delete" = "Create";
23 let list: Record<string, ReturnType<typeof entry> | string> = {};
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050024 const createTimestamp = auditCreate ? auditCreate.createdTimestamp : 0;
PineaFan638eb132023-01-19 10:41:22 +000025 const deleteTimestamp = auditDelete ? auditDelete.createdTimestamp : 0;
26 const updateTimestamp = auditUpdate ? auditUpdate.createdTimestamp : 0;
27 if (updateTimestamp > createTimestamp && updateTimestamp > deleteTimestamp && auditUpdate) {
TheCodedProf9812b702023-01-18 21:01:04 -050028 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 Grey75ea9172022-08-06 10:22:23 +010031 return acc;
32 },
33 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010034 );
TheCodedProf9812b702023-01-18 21:01:04 -050035 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"])
pineafan3a02ea32022-08-11 21:35:04 +010038 list["channel"] = entry(
TheCodedProf9812b702023-01-18 21:01:04 -050039 [before["channel_id"]!, after["channel_id"]!],
40 renderChannel(await client.channels.fetch(before["channel_id"]!) as GuildChannel) +
Skyler Grey75ea9172022-08-06 10:22:23 +010041 " -> " +
TheCodedProf9812b702023-01-18 21:01:04 -050042 renderChannel(await client.channels.fetch(after["channel_id"]!) as GuildChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010043 );
44 if (!Object.keys(list).length) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010045 list["created"] = entry(
TheCodedProf9812b702023-01-18 21:01:04 -050046 (auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp,
47 renderDelta((auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp)
Skyler Greyf21323a2022-08-13 23:58:22 +010048 );
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050049 list["edited"] = entry(after["editedTimestamp"]!, renderDelta(Date.now()));
TheCodedProf9812b702023-01-18 21:01:04 -050050 list["editedBy"] = entry(auditUpdate.executor!.id, renderUser(auditUpdate.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010051 action = "Update";
PineaFan638eb132023-01-19 10:41:22 +000052 } else if (deleteTimestamp > createTimestamp && deleteTimestamp > updateTimestamp && auditDelete) {
TheCodedProf9812b702023-01-18 21:01:04 -050053 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 Grey75ea9172022-08-06 10:22:23 +010056 return acc;
57 },
58 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010059 );
60 list = {
TheCodedProf9812b702023-01-18 21:01:04 -050061 name: entry(before["name"]!, `${before["name"]}`),
62 channel: entry(before["channel_id"]!, renderChannel((await client.channels.fetch(before["channel_id"]!)) as GuildChannel)),
PineaFan638eb132023-01-19 10:41:22 +000063 created: entry((auditDelete.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp, renderDelta((auditDelete.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp)),
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050064 deleted: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey75ea9172022-08-06 10:22:23 +010065 deletedBy: entry(
TheCodedProf9812b702023-01-18 21:01:04 -050066 auditDelete.executor!.id,
67 renderUser((await channel.guild.members.fetch(auditDelete.executor!.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010068 )
pineafan63fc5e22022-08-04 22:04:10 +010069 };
pineafan63fc5e22022-08-04 22:04:10 +010070 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010071 } else {
PineaFan638eb132023-01-19 10:41:22 +000072 const { before } = auditDelete!.changes.reduce((acc: accType, change) => {
TheCodedProf9812b702023-01-18 21:01:04 -050073 acc.before[change.key] = change.old?.toString()!;
74 acc.after[change.key] = change.new?.toString()!;
Skyler Grey75ea9172022-08-06 10:22:23 +010075 return acc;
76 },
77 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010078 );
79 list = {
TheCodedProf9812b702023-01-18 21:01:04 -050080 name: entry(before["name"]!, `${before["name"]}`),
81 channel: entry(before["channel_id"]!, renderChannel(await client.channels.fetch(before["channel_id"]!) as GuildChannel)),
Skyler Grey75ea9172022-08-06 10:22:23 +010082 createdBy: entry(
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050083 auditCreate!.executor!.id,
84 renderUser((await channel.guild.members.fetch(auditCreate!.executor!.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010085 ),
Samuel Shuert27bf3cd2023-03-03 15:51:25 -050086 created: entry(Date.now(), renderDelta(Date.now()))
pineafan63fc5e22022-08-04 22:04:10 +010087 };
pineafanda6e5342022-07-03 10:03:16 +010088 }
pineafan63fc5e22022-08-04 22:04:10 +010089 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +010090 Create: "green",
91 Update: "yellow",
92 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +010093 };
94 const data = {
pineafanda6e5342022-07-03 10:03:16 +010095 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010096 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +010097 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +010098 calculateType: "webhookUpdate",
PineaFan638eb132023-01-19 10:41:22 +000099 color: NucleusColors[cols[action] as keyof typeof NucleusColors],
pineafanda6e5342022-07-03 10:03:16 +0100100 emoji: "WEBHOOK." + action.toUpperCase(),
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500101 timestamp: Date.now()
pineafanda6e5342022-07-03 10:03:16 +0100102 },
103 list: list,
104 hidden: {
105 guild: channel.guild.id
106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107 };
pineafanda6e5342022-07-03 10:03:16 +0100108 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 } catch (e) {
110 console.log(e);
111 }
pineafanda6e5342022-07-03 10:03:16 +0100112}