blob: 21fec73117109535814bb436ce78a605c1214a81 [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 Grey11236ba2022-08-08 21:13:33 +010013 const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
TheCodedProfa16d1672023-01-18 18:58:34 -050014 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
pineafanda6e5342022-07-03 10:03:16 +010021 if (!auditCreate && !auditUpdate && !auditDelete) return;
22 let audit = auditCreate;
pineafan3a02ea32022-08-11 21:35:04 +010023 let action: "Create" | "Update" | "Delete" = "Create";
24 let list: Record<string, ReturnType<typeof entry> | string> = {};
Skyler Grey11236ba2022-08-08 21:13:33 +010025 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
TheCodedProf9812b702023-01-18 21:01:04 -050026 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 Grey75ea9172022-08-06 10:22:23 +010029 return acc;
30 },
31 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010032 );
TheCodedProf9812b702023-01-18 21:01:04 -050033 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"])
pineafan3a02ea32022-08-11 21:35:04 +010036 list["channel"] = entry(
TheCodedProf9812b702023-01-18 21:01:04 -050037 [before["channel_id"]!, after["channel_id"]!],
38 renderChannel(await client.channels.fetch(before["channel_id"]!) as GuildChannel) +
Skyler Grey75ea9172022-08-06 10:22:23 +010039 " -> " +
TheCodedProf9812b702023-01-18 21:01:04 -050040 renderChannel(await client.channels.fetch(after["channel_id"]!) as GuildChannel)
Skyler Grey75ea9172022-08-06 10:22:23 +010041 );
42 if (!Object.keys(list).length) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010043 list["created"] = entry(
TheCodedProf9812b702023-01-18 21:01:04 -050044 (auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp,
45 renderDelta((auditUpdate.target! as Extract<GuildAuditLogsEntry, {createdTimestamp: number}>).createdTimestamp)
Skyler Greyf21323a2022-08-13 23:58:22 +010046 );
TheCodedProf9812b702023-01-18 21:01:04 -050047 list["edited"] = entry(after["editedTimestamp"]!, renderDelta(new Date().getTime()));
48 list["editedBy"] = entry(auditUpdate.executor!.id, renderUser(auditUpdate.executor!));
pineafanda6e5342022-07-03 10:03:16 +010049 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010050 action = "Update";
Skyler Grey11236ba2022-08-08 21:13:33 +010051 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
TheCodedProf9812b702023-01-18 21:01:04 -050052 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 Grey75ea9172022-08-06 10:22:23 +010055 return acc;
56 },
57 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010058 );
59 list = {
TheCodedProf9812b702023-01-18 21:01:04 -050060 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 Grey11236ba2022-08-08 21:13:33 +010063 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey75ea9172022-08-06 10:22:23 +010064 deletedBy: entry(
TheCodedProf9812b702023-01-18 21:01:04 -050065 auditDelete.executor!.id,
66 renderUser((await channel.guild.members.fetch(auditDelete.executor!.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010067 )
pineafan63fc5e22022-08-04 22:04:10 +010068 };
pineafanda6e5342022-07-03 10:03:16 +010069 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010070 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010071 } else {
TheCodedProf9812b702023-01-18 21:01:04 -050072 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 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(
TheCodedProf9812b702023-01-18 21:01:04 -050083 auditCreate.executor!.id,
84 renderUser((await channel.guild.members.fetch(auditCreate.executor!.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010085 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010086 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
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",
TheCodedProf9812b702023-01-18 21:01:04 -050099 color: (NucleusColors as any)[cols[action]],
pineafanda6e5342022-07-03 10:03:16 +0100100 emoji: "WEBHOOK." + action.toUpperCase(),
101 timestamp: new Date().getTime()
102 },
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}