blob: 6187253b864339fa66c01d6c1332fca768456079 [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 ]);
Skyler Greyf21323a2022-08-13 23:58:22 +010018 const auditCreate = auditLogCreate.entries
19 .filter((entry: GuildAuditLogsEntry | null) => {
20 if (entry === null) return false;
21 return (entry.target! as Webhook).channelId === channel.id;
22 })
23 .first();
24 const auditUpdate = auditLogUpdate.entries
25 .filter((entry: GuildAuditLogsEntry | null) => {
26 if (entry === null) return false;
27 return (entry.target! as Webhook).channelId === channel.id;
28 })
29 .first();
30 const auditDelete = auditLogDelete.entries
31 .filter((entry: GuildAuditLogsEntry | null) => {
32 if (entry === null) return false;
33 return (entry.target! as Webhook).channelId === channel.id;
34 })
35 .first();
pineafanda6e5342022-07-03 10:03:16 +010036 if (!auditCreate && !auditUpdate && !auditDelete) return;
37 let audit = auditCreate;
pineafan3a02ea32022-08-11 21:35:04 +010038 let action: "Create" | "Update" | "Delete" = "Create";
39 let list: Record<string, ReturnType<typeof entry> | string> = {};
Skyler Grey11236ba2022-08-08 21:13:33 +010040 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010041 const { before, after } = auditUpdate.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010042 (
43 acc: { before: Record<string, string>; after: Record<string, string> },
44 change: { key: string; new: string; old: string }
45 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010046 acc.before[change.key] = change.old;
47 acc.after[change.key] = change.new;
48 return acc;
49 },
50 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010051 );
Skyler Grey75ea9172022-08-06 10:22:23 +010052 if (before.name !== after.name)
pineafan3a02ea32022-08-11 21:35:04 +010053 list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
Skyler Grey75ea9172022-08-06 10:22:23 +010054 if (before.channel_id !== after.channel_id)
pineafan3a02ea32022-08-11 21:35:04 +010055 list["channel"] = entry(
Skyler Grey75ea9172022-08-06 10:22:23 +010056 [before.channel_id, after.channel_id],
Skyler Grey11236ba2022-08-08 21:13:33 +010057 renderChannel(await client.channels.fetch(before.channel_id)) +
Skyler Grey75ea9172022-08-06 10:22:23 +010058 " -> " +
Skyler Grey11236ba2022-08-08 21:13:33 +010059 renderChannel(await client.channels.fetch(after.channel_id))
Skyler Grey75ea9172022-08-06 10:22:23 +010060 );
61 if (!Object.keys(list).length) return;
Skyler Greyf21323a2022-08-13 23:58:22 +010062 list["created"] = entry(
63 auditUpdate.target.createdTimestamp,
64 renderDelta(auditUpdate.target.createdTimestamp)
65 );
pineafan3a02ea32022-08-11 21:35:04 +010066 list["edited"] = entry(after.editedTimestamp, renderDelta(new Date().getTime()));
67 list["editedBy"] = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor));
pineafanda6e5342022-07-03 10:03:16 +010068 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010069 action = "Update";
Skyler Grey11236ba2022-08-08 21:13:33 +010070 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
Skyler Grey75ea9172022-08-06 10:22:23 +010071 const { before } = auditDelete.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010072 (
73 acc: { before: Record<string, string>; after: Record<string, string> },
74 change: { key: string; new: string; old: string }
75 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010076 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))),
85 created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)),
86 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
Skyler Grey75ea9172022-08-06 10:22:23 +010087 deletedBy: entry(
88 auditDelete.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +010089 renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +010090 )
pineafan63fc5e22022-08-04 22:04:10 +010091 };
pineafanda6e5342022-07-03 10:03:16 +010092 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010093 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010094 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010095 const { before } = auditDelete.changes.reduce(
Skyler Greyf21323a2022-08-13 23:58:22 +010096 (
97 acc: { before: Record<string, string>; after: Record<string, string> },
98 change: { key: string; new: string; old: string }
99 ) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 acc.before[change.key] = change.old;
101 acc.after[change.key] = change.new;
102 return acc;
103 },
104 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +0100105 );
106 list = {
107 name: entry(before.name, `${before.name}`),
Skyler Grey11236ba2022-08-08 21:13:33 +0100108 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 createdBy: entry(
110 auditCreate.executor.id,
Skyler Grey11236ba2022-08-08 21:13:33 +0100111 renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)
Skyler Grey75ea9172022-08-06 10:22:23 +0100112 ),
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
pineafan63fc5e22022-08-04 22:04:10 +0100114 };
pineafanda6e5342022-07-03 10:03:16 +0100115 }
pineafan63fc5e22022-08-04 22:04:10 +0100116 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 Create: "green",
118 Update: "yellow",
119 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100120 };
121 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100122 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100123 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100124 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100125 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100126 color: NucleusColors[cols[action]],
127 emoji: "WEBHOOK." + action.toUpperCase(),
128 timestamp: new Date().getTime()
129 },
130 list: list,
131 hidden: {
132 guild: channel.guild.id
133 }
pineafan63fc5e22022-08-04 22:04:10 +0100134 };
pineafanda6e5342022-07-03 10:03:16 +0100135 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 } catch (e) {
137 console.log(e);
138 }
pineafanda6e5342022-07-03 10:03:16 +0100139}