blob: e4e96596ceef04328cd5bc92da5c5007c8b861ad [file] [log] [blame]
pineafan3a02ea32022-08-11 21:35:04 +01001import type { GuildAuditLogsEntry, Webhook } from "discord.js";
pineafan63fc5e22022-08-04 22:04:10 +01002import type Discord from "discord.js";
PineaFan64486c42022-12-28 09:21:04 +00003import type { HaikuClient } from "../utils/haiku/index.js";
pineafan63fc5e22022-08-04 22:04:10 +01004export const event = "webhookUpdate";
pineafanda6e5342022-07-03 10:03:16 +01005
Skyler Grey11236ba2022-08-08 21:13:33 +01006export async function callback(client: HaikuClient, 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;
pineafan63fc5e22022-08-04 22:04:10 +01009 let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE");
10 let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE");
11 let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE");
Skyler Grey75ea9172022-08-06 10:22:23 +010012 [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([
13 auditLogCreate,
14 auditLogUpdate,
15 auditLogDelete
16 ]);
Skyler Greyf21323a2022-08-13 23:58:22 +010017 const auditCreate = auditLogCreate.entries
18 .filter((entry: GuildAuditLogsEntry | null) => {
19 if (entry === null) return false;
20 return (entry.target! as Webhook).channelId === channel.id;
21 })
22 .first();
23 const auditUpdate = auditLogUpdate.entries
24 .filter((entry: GuildAuditLogsEntry | null) => {
25 if (entry === null) return false;
26 return (entry.target! as Webhook).channelId === channel.id;
27 })
28 .first();
29 const auditDelete = auditLogDelete.entries
30 .filter((entry: GuildAuditLogsEntry | null) => {
31 if (entry === null) return false;
32 return (entry.target! as Webhook).channelId === channel.id;
33 })
34 .first();
pineafanda6e5342022-07-03 10:03:16 +010035 if (!auditCreate && !auditUpdate && !auditDelete) return;
36 let audit = auditCreate;
pineafan3a02ea32022-08-11 21:35:04 +010037 let action: "Create" | "Update" | "Delete" = "Create";
38 let list: Record<string, ReturnType<typeof entry> | string> = {};
Skyler Grey11236ba2022-08-08 21:13:33 +010039 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010040 const { before, after } = auditUpdate.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010041 (
42 acc: { before: Record<string, string>; after: Record<string, string> },
43 change: { key: string; new: string; old: string }
44 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010045 acc.before[change.key] = change.old;
46 acc.after[change.key] = change.new;
47 return acc;
48 },
49 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010050 );
Skyler Grey75ea9172022-08-06 10:22:23 +010051 if (before.name !== after.name)
pineafan3a02ea32022-08-11 21:35:04 +010052 list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010053 if (before.channel_id !== after.channel_id)
pineafan3a02ea32022-08-11 21:35:04 +010054 list["channel"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010055 [before.channel_id, after.channel_id],
Skyler Grey11236ba2022-08-08 21:13:33 +010056 renderChannel(await client.channels.fetch(before.channel_id)) +
Skyler Grey75ea9172022-08-06 10:22:23 +010057 " -> " +
Skyler Grey11236ba2022-08-08 21:13:33 +010058 renderChannel(await client.channels.fetch(after.channel_id))
Skyler Grey75ea9172022-08-06 10:22:23 +010059 );
60 if (!Object.keys(list).length) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010061 list["created"] = entry(
62 auditUpdate.target.createdTimestamp,
63 renderDelta(auditUpdate.target.createdTimestamp)
64 );
pineafan3a02ea32022-08-11 21:35:04 +010065 list["edited"] = entry(after.editedTimestamp, renderDelta(new Date().getTime()));
66 list["editedBy"] = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor));
pineafanda6e5342022-07-03 10:03:16 +010067 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010068 action = "Update";
Skyler Grey11236ba2022-08-08 21:13:33 +010069 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010070 const { before } = auditDelete.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010071 (
72 acc: { before: Record<string, string>; after: Record<string, string> },
73 change: { key: string; new: string; old: string }
74 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010075 acc.before[change.key] = change.old;
76 acc.after[change.key] = change.new;
77 return acc;
78 },
79 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010080 );
81 list = {
82 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010083 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
84 created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)),
85 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey75ea9172022-08-06 10:22:23 +010086 deletedBy: entry(
87 auditDelete.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010088 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 };
pineafanda6e5342022-07-03 10:03:16 +010091 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010092 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010093 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010094 const { before } = auditDelete.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010095 (
96 acc: { before: Record<string, string>; after: Record<string, string> },
97 change: { key: string; new: string; old: string }
98 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 acc.before[change.key] = change.old;
100 acc.after[change.key] = change.new;
101 return acc;
102 },
103 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +0100104 );
105 list = {
106 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +0100107 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 createdBy: entry(
109 auditCreate.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +0100110 renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100111 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100112 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan63fc5e22022-08-04 22:04:10 +0100113 };
pineafanda6e5342022-07-03 10:03:16 +0100114 }
pineafan63fc5e22022-08-04 22:04:10 +0100115 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 Create: "green",
117 Update: "yellow",
118 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100119 };
120 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100121 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100122 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100123 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100124 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100125 color: NucleusColors[cols[action]],
126 emoji: "WEBHOOK." + action.toUpperCase(),
127 timestamp: new Date().getTime()
128 },
129 list: list,
130 hidden: {
131 guild: channel.guild.id
132 }
pineafan63fc5e22022-08-04 22:04:10 +0100133 };
pineafanda6e5342022-07-03 10:03:16 +0100134 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 } catch (e) {
136 console.log(e);
137 }
pineafanda6e5342022-07-03 10:03:16 +0100138}