blob: f6002479c3b1d46012c260eac2a53ddfc4535d64 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import type Discord from "discord.js";
2// eslint-disable-next-line @typescript-eslint/ban-ts-comment
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 Grey75ea9172022-08-06 10:22:23 +01007export async function callback(
8 client: HaikuClient,
9 channel: Discord.GuildChannel
10) {
pineafanda6e5342022-07-03 10:03:16 +010011 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010012 const {
13 getAuditLog,
14 log,
15 NucleusColors,
16 entry,
17 renderUser,
18 renderChannel,
19 renderDelta
20 } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010021 let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE");
22 let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE");
23 let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE");
Skyler Grey75ea9172022-08-06 10:22:23 +010024 [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([
25 auditLogCreate,
26 auditLogUpdate,
27 auditLogDelete
28 ]);
29 const auditCreate = auditLogCreate.entries
30 .filter((entry) => entry.target.channelId === channel.id)
31 .first();
32 const auditUpdate = auditLogUpdate.entries
33 .filter((entry) => entry.target.channelId === channel.id)
34 .first();
35 const auditDelete = auditLogDelete.entries
36 .filter((entry) => entry.target.channelId === channel.id)
37 .first();
pineafanda6e5342022-07-03 10:03:16 +010038 if (!auditCreate && !auditUpdate && !auditDelete) return;
39 let audit = auditCreate;
40 let action = "Create";
Skyler Grey75ea9172022-08-06 10:22:23 +010041 let list = {} as {
42 created: { value: string; displayValue: string };
43 updated: { value: string; displayValue: string };
44 deleted: { value: string; displayValue: string };
45 };
46 if (
47 auditUpdate &&
48 auditUpdate.createdTimestamp > audit.createdTimestamp
49 ) {
50 const { before, after } = auditUpdate.changes.reduce(
51 (acc, change) => {
52 acc.before[change.key] = change.old;
53 acc.after[change.key] = change.new;
54 return acc;
55 },
56 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010057 );
Skyler Grey75ea9172022-08-06 10:22:23 +010058 if (before.name !== after.name)
59 list.name = entry(
60 [before.name, after.name],
61 `${before.name} -> ${after.name}`
62 );
63 if (before.channel_id !== after.channel_id)
64 list.channel = entry(
65 [before.channel_id, after.channel_id],
66 renderChannel(
67 await client.channels.fetch(before.channel_id)
68 ) +
69 " -> " +
70 renderChannel(
71 await client.channels.fetch(after.channel_id)
72 )
73 );
74 if (!Object.keys(list).length) return;
75 list.created = entry(
76 auditUpdate.target.createdTimestamp,
77 renderDelta(auditUpdate.target.createdTimestamp)
78 );
79 list.edited = entry(
80 after.editedTimestamp,
81 renderDelta(new Date().getTime())
82 );
83 list.editedBy = entry(
84 auditUpdate.executor.id,
85 renderUser(auditUpdate.executor)
86 );
pineafanda6e5342022-07-03 10:03:16 +010087 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010088 action = "Update";
Skyler Grey75ea9172022-08-06 10:22:23 +010089 } else if (
90 auditDelete &&
91 auditDelete.createdTimestamp > audit.createdTimestamp
92 ) {
93 const { before } = auditDelete.changes.reduce(
94 (acc, change) => {
95 acc.before[change.key] = change.old;
96 acc.after[change.key] = change.new;
97 return acc;
98 },
99 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +0100100 );
101 list = {
102 name: entry(before.name, `${before.name}`),
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 channel: entry(
104 before.channel_id,
105 renderChannel(
106 await client.channels.fetch(before.channel_id)
107 )
108 ),
109 created: entry(
110 auditDelete.target.createdTimestamp,
111 renderDelta(auditDelete.target.createdTimestamp)
112 ),
113 deleted: entry(
114 new Date().getTime(),
115 renderDelta(new Date().getTime())
116 ),
117 deletedBy: entry(
118 auditDelete.executor.id,
119 renderUser(
120 (
121 await channel.guild.members.fetch(
122 auditDelete.executor.id
123 )
124 ).user
125 )
126 )
pineafan63fc5e22022-08-04 22:04:10 +0100127 };
pineafanda6e5342022-07-03 10:03:16 +0100128 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +0100129 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +0100130 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 const { before } = auditDelete.changes.reduce(
132 (acc, change) => {
133 acc.before[change.key] = change.old;
134 acc.after[change.key] = change.new;
135 return acc;
136 },
137 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +0100138 );
139 list = {
140 name: entry(before.name, `${before.name}`),
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 channel: entry(
142 before.channel_id,
143 renderChannel(
144 await client.channels.fetch(before.channel_id)
145 )
146 ),
147 createdBy: entry(
148 auditCreate.executor.id,
149 renderUser(
150 (
151 await channel.guild.members.fetch(
152 auditCreate.executor.id
153 )
154 ).user
155 )
156 ),
157 created: entry(
158 new Date().getTime(),
159 renderDelta(new Date().getTime())
160 )
pineafan63fc5e22022-08-04 22:04:10 +0100161 };
pineafanda6e5342022-07-03 10:03:16 +0100162 }
pineafan63fc5e22022-08-04 22:04:10 +0100163 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100164 Create: "green",
165 Update: "yellow",
166 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100167 };
168 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100169 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100170 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100171 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100172 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100173 color: NucleusColors[cols[action]],
174 emoji: "WEBHOOK." + action.toUpperCase(),
175 timestamp: new Date().getTime()
176 },
177 list: list,
178 hidden: {
179 guild: channel.guild.id
180 }
pineafan63fc5e22022-08-04 22:04:10 +0100181 };
pineafanda6e5342022-07-03 10:03:16 +0100182 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 } catch (e) {
184 console.log(e);
185 }
pineafanda6e5342022-07-03 10:03:16 +0100186}