blob: 2e95c4b4f3ec046c4b83204df1ee4603e75f877f [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import type Discord from "discord.js";
Skyler Grey75ea9172022-08-06 10:22:23 +01002// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01003import type { HaikuClient } from "jshaiku";
4export 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 Grey11236ba2022-08-08 21:13:33 +010017 const auditCreate = auditLogCreate.entries.filter((entry) => entry.target.channelId === channel.id).first();
18 const auditUpdate = auditLogUpdate.entries.filter((entry) => entry.target.channelId === channel.id).first();
19 const auditDelete = auditLogDelete.entries.filter((entry) => entry.target.channelId === channel.id).first();
pineafanda6e5342022-07-03 10:03:16 +010020 if (!auditCreate && !auditUpdate && !auditDelete) return;
21 let audit = auditCreate;
22 let action = "Create";
Skyler Grey75ea9172022-08-06 10:22:23 +010023 let list = {} as {
24 created: { value: string; displayValue: string };
25 updated: { value: string; displayValue: string };
26 deleted: { value: string; displayValue: string };
27 };
Skyler Grey11236ba2022-08-08 21:13:33 +010028 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 const { before, after } = auditUpdate.changes.reduce(
30 (acc, change) => {
31 acc.before[change.key] = change.old;
32 acc.after[change.key] = change.new;
33 return acc;
34 },
35 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010036 );
Skyler Grey75ea9172022-08-06 10:22:23 +010037 if (before.name !== after.name)
Skyler Grey11236ba2022-08-08 21:13:33 +010038 list.name = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010039 if (before.channel_id !== after.channel_id)
40 list.channel = entry(
41 [before.channel_id, after.channel_id],
Skyler Grey11236ba2022-08-08 21:13:33 +010042 renderChannel(await client.channels.fetch(before.channel_id)) +
Skyler Grey75ea9172022-08-06 10:22:23 +010043 " -> " +
Skyler Grey11236ba2022-08-08 21:13:33 +010044 renderChannel(await client.channels.fetch(after.channel_id))
Skyler Grey75ea9172022-08-06 10:22:23 +010045 );
46 if (!Object.keys(list).length) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010047 list.created = entry(auditUpdate.target.createdTimestamp, renderDelta(auditUpdate.target.createdTimestamp));
48 list.edited = entry(after.editedTimestamp, renderDelta(new Date().getTime()));
49 list.editedBy = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor));
pineafanda6e5342022-07-03 10:03:16 +010050 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010051 action = "Update";
Skyler Grey11236ba2022-08-08 21:13:33 +010052 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010053 const { before } = auditDelete.changes.reduce(
54 (acc, change) => {
55 acc.before[change.key] = change.old;
56 acc.after[change.key] = change.new;
57 return acc;
58 },
59 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010060 );
61 list = {
62 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010063 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
64 created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)),
65 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey75ea9172022-08-06 10:22:23 +010066 deletedBy: entry(
67 auditDelete.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010068 renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010069 )
pineafan63fc5e22022-08-04 22:04:10 +010070 };
pineafanda6e5342022-07-03 10:03:16 +010071 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010072 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010073 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010074 const { before } = auditDelete.changes.reduce(
75 (acc, change) => {
76 acc.before[change.key] = change.old;
77 acc.after[change.key] = change.new;
78 return acc;
79 },
80 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010081 );
82 list = {
83 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010084 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
Skyler Grey75ea9172022-08-06 10:22:23 +010085 createdBy: entry(
86 auditCreate.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010087 renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010088 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010089 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan63fc5e22022-08-04 22:04:10 +010090 };
pineafanda6e5342022-07-03 10:03:16 +010091 }
pineafan63fc5e22022-08-04 22:04:10 +010092 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +010093 Create: "green",
94 Update: "yellow",
95 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +010096 };
97 const data = {
pineafanda6e5342022-07-03 10:03:16 +010098 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010099 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100100 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100101 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100102 color: NucleusColors[cols[action]],
103 emoji: "WEBHOOK." + action.toUpperCase(),
104 timestamp: new Date().getTime()
105 },
106 list: list,
107 hidden: {
108 guild: channel.guild.id
109 }
pineafan63fc5e22022-08-04 22:04:10 +0100110 };
pineafanda6e5342022-07-03 10:03:16 +0100111 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 } catch (e) {
113 console.log(e);
114 }
pineafanda6e5342022-07-03 10:03:16 +0100115}