blob: c58a3403711e05dab7debfe9414352c8b1444886 [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";
Skyler Grey75ea9172022-08-06 10:22:23 +01003// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01004import type { HaikuClient } from "jshaiku";
5export const event = "webhookUpdate";
pineafanda6e5342022-07-03 10:03:16 +01006
Skyler Grey11236ba2022-08-08 21:13:33 +01007export async function callback(client: HaikuClient, channel: Discord.GuildChannel) {
pineafanda6e5342022-07-03 10:03:16 +01008 try {
Skyler Grey11236ba2022-08-08 21:13:33 +01009 const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010010 let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE");
11 let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE");
12 let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE");
Skyler Grey75ea9172022-08-06 10:22:23 +010013 [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([
14 auditLogCreate,
15 auditLogUpdate,
16 auditLogDelete
17 ]);
pineafan3a02ea32022-08-11 21:35:04 +010018 const auditCreate = auditLogCreate.entries.filter((entry: GuildAuditLogsEntry | null) => {
19 if (entry === null) return false
20 return (entry.target! as Webhook).channelId === channel.id}
21 ).first();
22 const auditUpdate = auditLogUpdate.entries.filter((entry: GuildAuditLogsEntry | null) => {
23 if (entry === null) return false
24 return (entry.target! as Webhook).channelId === channel.id}
25 ).first();
26 const auditDelete = auditLogDelete.entries.filter((entry: GuildAuditLogsEntry | null) => {
27 if (entry === null) return false
28 return (entry.target! as Webhook).channelId === channel.id}
29 ).first();
pineafanda6e5342022-07-03 10:03:16 +010030 if (!auditCreate && !auditUpdate && !auditDelete) return;
31 let audit = auditCreate;
pineafan3a02ea32022-08-11 21:35:04 +010032 let action: "Create" | "Update" | "Delete" = "Create";
33 let list: Record<string, ReturnType<typeof entry> | string> = {};
Skyler Grey11236ba2022-08-08 21:13:33 +010034 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010035 const { before, after } = auditUpdate.changes.reduce(
pineafan3a02ea32022-08-11 21:35:04 +010036 (acc: {before: Record<string, string>, after: Record<string, string>}, change: {key: string, new: string, old: string}) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010037 acc.before[change.key] = change.old;
38 acc.after[change.key] = change.new;
39 return acc;
40 },
41 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010042 );
Skyler Grey75ea9172022-08-06 10:22:23 +010043 if (before.name !== after.name)
pineafan3a02ea32022-08-11 21:35:04 +010044 list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010045 if (before.channel_id !== after.channel_id)
pineafan3a02ea32022-08-11 21:35:04 +010046 list["channel"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010047 [before.channel_id, after.channel_id],
Skyler Grey11236ba2022-08-08 21:13:33 +010048 renderChannel(await client.channels.fetch(before.channel_id)) +
Skyler Grey75ea9172022-08-06 10:22:23 +010049 " -> " +
Skyler Grey11236ba2022-08-08 21:13:33 +010050 renderChannel(await client.channels.fetch(after.channel_id))
Skyler Grey75ea9172022-08-06 10:22:23 +010051 );
52 if (!Object.keys(list).length) return;
pineafan3a02ea32022-08-11 21:35:04 +010053 list["created"] = entry(auditUpdate.target.createdTimestamp, renderDelta(auditUpdate.target.createdTimestamp));
54 list["edited"] = entry(after.editedTimestamp, renderDelta(new Date().getTime()));
55 list["editedBy"] = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor));
pineafanda6e5342022-07-03 10:03:16 +010056 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010057 action = "Update";
Skyler Grey11236ba2022-08-08 21:13:33 +010058 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010059 const { before } = auditDelete.changes.reduce(
pineafan3a02ea32022-08-11 21:35:04 +010060 (acc: {before: Record<string, string>, after: Record<string, string>}, change: {key: string, new: string, old: string}) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010061 acc.before[change.key] = change.old;
62 acc.after[change.key] = change.new;
63 return acc;
64 },
65 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010066 );
67 list = {
68 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010069 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
70 created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)),
71 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey75ea9172022-08-06 10:22:23 +010072 deletedBy: entry(
73 auditDelete.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010074 renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010075 )
pineafan63fc5e22022-08-04 22:04:10 +010076 };
pineafanda6e5342022-07-03 10:03:16 +010077 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010078 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010079 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010080 const { before } = auditDelete.changes.reduce(
pineafan3a02ea32022-08-11 21:35:04 +010081 (acc: {before: Record<string, string>, after: Record<string, string>}, change: {key: string, new: string, old: string}) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010082 acc.before[change.key] = change.old;
83 acc.after[change.key] = change.new;
84 return acc;
85 },
86 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010087 );
88 list = {
89 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +010090 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
Skyler Grey75ea9172022-08-06 10:22:23 +010091 createdBy: entry(
92 auditCreate.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010093 renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010094 ),
Skyler Grey11236ba2022-08-08 21:13:33 +010095 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan63fc5e22022-08-04 22:04:10 +010096 };
pineafanda6e5342022-07-03 10:03:16 +010097 }
pineafan63fc5e22022-08-04 22:04:10 +010098 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +010099 Create: "green",
100 Update: "yellow",
101 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100102 };
103 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100104 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100105 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100106 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100107 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100108 color: NucleusColors[cols[action]],
109 emoji: "WEBHOOK." + action.toUpperCase(),
110 timestamp: new Date().getTime()
111 },
112 list: list,
113 hidden: {
114 guild: channel.guild.id
115 }
pineafan63fc5e22022-08-04 22:04:10 +0100116 };
pineafanda6e5342022-07-03 10:03:16 +0100117 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100118 } catch (e) {
119 console.log(e);
120 }
pineafanda6e5342022-07-03 10:03:16 +0100121}