blob: c4a4efbd9a3a9513a2fafdf8164eabfb665272cc [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 {
Skyler Greyda16adf2023-03-05 10:22:12 +000013 const { getAuditLog, isLogging, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } =
14 client.logger;
15 if (!(await isLogging(channel.guild.id, "webhookUpdate"))) return;
16 const auditCreate = (await getAuditLog(channel.guild, AuditLogEvent.WebhookCreate)).filter(
17 (entry: GuildAuditLogsEntry | null) =>
18 entry?.target ? (entry.target as Webhook)!.channelId === channel.id : false
19 )[0];
20 const auditDelete = (await getAuditLog(channel.guild, AuditLogEvent.WebhookDelete, 0)).filter(
21 (entry: GuildAuditLogsEntry | null) =>
22 entry?.target ? (entry.target as Webhook)!.channelId === channel.id : false
23 )[0];
24 const auditUpdate = (await getAuditLog(channel.guild, AuditLogEvent.WebhookUpdate, 0)).filter(
25 (entry: GuildAuditLogsEntry | null) =>
26 entry?.target ? (entry.target as Webhook)!.channelId === channel.id : false
27 )[0];
TheCodedProf686829f2023-02-22 15:08:01 -050028 if (!auditCreate && !auditUpdate && !auditDelete) return;
pineafan3a02ea32022-08-11 21:35:04 +010029 let action: "Create" | "Update" | "Delete" = "Create";
30 let list: Record<string, ReturnType<typeof entry> | string> = {};
TheCodedProf686829f2023-02-22 15:08:01 -050031 const createTimestamp = auditCreate ? auditCreate.createdTimestamp : 0;
PineaFan638eb132023-01-19 10:41:22 +000032 const deleteTimestamp = auditDelete ? auditDelete.createdTimestamp : 0;
33 const updateTimestamp = auditUpdate ? auditUpdate.createdTimestamp : 0;
34 if (updateTimestamp > createTimestamp && updateTimestamp > deleteTimestamp && auditUpdate) {
Skyler Greyda16adf2023-03-05 10:22:12 +000035 const { before, after } = auditUpdate.changes.reduce(
36 (acc: accType, change) => {
TheCodedProf9812b702023-01-18 21:01:04 -050037 acc.before[change.key] = change.old?.toString()!;
38 acc.after[change.key] = change.new?.toString()!;
Skyler Grey75ea9172022-08-06 10:22:23 +010039 return acc;
40 },
41 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010042 );
TheCodedProf9812b702023-01-18 21:01:04 -050043 if (before["name"] !== after["name"])
44 list["name"] = entry([before["name"]!, after["name"]!], `${before["name"]} -> ${after["name"]}`);
45 if (before["channel_id"] !== after["channel_id"])
pineafan3a02ea32022-08-11 21:35:04 +010046 list["channel"] = entry(
TheCodedProf9812b702023-01-18 21:01:04 -050047 [before["channel_id"]!, after["channel_id"]!],
Skyler Greyda16adf2023-03-05 10:22:12 +000048 renderChannel((await client.channels.fetch(before["channel_id"]!)) as GuildChannel) +
Skyler Grey75ea9172022-08-06 10:22:23 +010049 " -> " +
Skyler Greyda16adf2023-03-05 10:22:12 +000050 renderChannel((await client.channels.fetch(after["channel_id"]!)) as GuildChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010051 );
52 if (!Object.keys(list).length) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010053 list["created"] = entry(
Skyler Greyda16adf2023-03-05 10:22:12 +000054 (auditUpdate.target! as Extract<GuildAuditLogsEntry, { createdTimestamp: number }>).createdTimestamp,
55 renderDelta(
56 (auditUpdate.target! as Extract<GuildAuditLogsEntry, { createdTimestamp: number }>).createdTimestamp
57 )
Skyler Greyf21323a2022-08-13 23:58:22 +010058 );
TheCodedProf6ec331b2023-02-20 12:13:06 -050059 list["edited"] = entry(after["editedTimestamp"]!, renderDelta(Date.now()));
TheCodedProf9812b702023-01-18 21:01:04 -050060 list["editedBy"] = entry(auditUpdate.executor!.id, renderUser(auditUpdate.executor!));
pineafan63fc5e22022-08-04 22:04:10 +010061 action = "Update";
PineaFan638eb132023-01-19 10:41:22 +000062 } else if (deleteTimestamp > createTimestamp && deleteTimestamp > updateTimestamp && auditDelete) {
Skyler Greyda16adf2023-03-05 10:22:12 +000063 const { before } = auditDelete.changes.reduce(
64 (acc: accType, change) => {
TheCodedProf9812b702023-01-18 21:01:04 -050065 acc.before[change.key] = change.old?.toString()!;
66 acc.after[change.key] = change.new?.toString()!;
Skyler Grey75ea9172022-08-06 10:22:23 +010067 return acc;
68 },
69 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010070 );
71 list = {
TheCodedProf9812b702023-01-18 21:01:04 -050072 name: entry(before["name"]!, `${before["name"]}`),
Skyler Greyda16adf2023-03-05 10:22:12 +000073 channel: entry(
74 before["channel_id"]!,
75 renderChannel((await client.channels.fetch(before["channel_id"]!)) as GuildChannel)
76 ),
77 created: entry(
78 (auditDelete.target! as Extract<GuildAuditLogsEntry, { createdTimestamp: number }>)
79 .createdTimestamp,
80 renderDelta(
81 (auditDelete.target! as Extract<GuildAuditLogsEntry, { createdTimestamp: number }>)
82 .createdTimestamp
83 )
84 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -050085 deleted: entry(Date.now(), renderDelta(Date.now())),
Skyler Grey75ea9172022-08-06 10:22:23 +010086 deletedBy: entry(
TheCodedProf9812b702023-01-18 21:01:04 -050087 auditDelete.executor!.id,
88 renderUser((await channel.guild.members.fetch(auditDelete.executor!.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010089 )
pineafan63fc5e22022-08-04 22:04:10 +010090 };
pineafan63fc5e22022-08-04 22:04:10 +010091 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010092 } else {
Skyler Greyda16adf2023-03-05 10:22:12 +000093 const { before } = auditDelete!.changes.reduce(
94 (acc: accType, change) => {
TheCodedProf9812b702023-01-18 21:01:04 -050095 acc.before[change.key] = change.old?.toString()!;
96 acc.after[change.key] = change.new?.toString()!;
Skyler Grey75ea9172022-08-06 10:22:23 +010097 return acc;
98 },
99 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +0100100 );
101 list = {
TheCodedProf9812b702023-01-18 21:01:04 -0500102 name: entry(before["name"]!, `${before["name"]}`),
Skyler Greyda16adf2023-03-05 10:22:12 +0000103 channel: entry(
104 before["channel_id"]!,
105 renderChannel((await client.channels.fetch(before["channel_id"]!)) as GuildChannel)
106 ),
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 createdBy: entry(
TheCodedProf686829f2023-02-22 15:08:01 -0500108 auditCreate!.executor!.id,
109 renderUser((await channel.guild.members.fetch(auditCreate!.executor!.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100110 ),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500111 created: entry(Date.now(), renderDelta(Date.now()))
pineafan63fc5e22022-08-04 22:04:10 +0100112 };
pineafanda6e5342022-07-03 10:03:16 +0100113 }
pineafan63fc5e22022-08-04 22:04:10 +0100114 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 Create: "green",
116 Update: "yellow",
117 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100118 };
119 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100120 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100121 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100122 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100123 calculateType: "webhookUpdate",
PineaFan638eb132023-01-19 10:41:22 +0000124 color: NucleusColors[cols[action] as keyof typeof NucleusColors],
pineafanda6e5342022-07-03 10:03:16 +0100125 emoji: "WEBHOOK." + action.toUpperCase(),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500126 timestamp: Date.now()
pineafanda6e5342022-07-03 10:03:16 +0100127 },
128 list: list,
129 hidden: {
130 guild: channel.guild.id
131 }
pineafan63fc5e22022-08-04 22:04:10 +0100132 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000133 await log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 } catch (e) {
135 console.log(e);
136 }
pineafanda6e5342022-07-03 10:03:16 +0100137}