blob: ae88cac7904a7d96c506786b4510a9d529353283 [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 Grey75ea9172022-08-06 10:22:23 +01006export async function callback(
7 client: HaikuClient,
8 channel: Discord.GuildChannel
9) {
pineafanda6e5342022-07-03 10:03:16 +010010 try {
Skyler Grey75ea9172022-08-06 10:22:23 +010011 const {
12 getAuditLog,
13 log,
14 NucleusColors,
15 entry,
16 renderUser,
17 renderChannel,
18 renderDelta
19 } = client.logger;
pineafan63fc5e22022-08-04 22:04:10 +010020 let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE");
21 let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE");
22 let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE");
Skyler Grey75ea9172022-08-06 10:22:23 +010023 [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([
24 auditLogCreate,
25 auditLogUpdate,
26 auditLogDelete
27 ]);
28 const auditCreate = auditLogCreate.entries
29 .filter((entry) => entry.target.channelId === channel.id)
30 .first();
31 const auditUpdate = auditLogUpdate.entries
32 .filter((entry) => entry.target.channelId === channel.id)
33 .first();
34 const auditDelete = auditLogDelete.entries
35 .filter((entry) => entry.target.channelId === channel.id)
36 .first();
pineafanda6e5342022-07-03 10:03:16 +010037 if (!auditCreate && !auditUpdate && !auditDelete) return;
38 let audit = auditCreate;
39 let action = "Create";
Skyler Grey75ea9172022-08-06 10:22:23 +010040 let list = {} as {
41 created: { value: string; displayValue: string };
42 updated: { value: string; displayValue: string };
43 deleted: { value: string; displayValue: string };
44 };
45 if (
46 auditUpdate &&
47 auditUpdate.createdTimestamp > audit.createdTimestamp
48 ) {
49 const { before, after } = auditUpdate.changes.reduce(
50 (acc, change) => {
51 acc.before[change.key] = change.old;
52 acc.after[change.key] = change.new;
53 return acc;
54 },
55 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010056 );
Skyler Grey75ea9172022-08-06 10:22:23 +010057 if (before.name !== after.name)
58 list.name = entry(
59 [before.name, after.name],
60 `${before.name} -> ${after.name}`
61 );
62 if (before.channel_id !== after.channel_id)
63 list.channel = entry(
64 [before.channel_id, after.channel_id],
65 renderChannel(
66 await client.channels.fetch(before.channel_id)
67 ) +
68 " -> " +
69 renderChannel(
70 await client.channels.fetch(after.channel_id)
71 )
72 );
73 if (!Object.keys(list).length) return;
74 list.created = entry(
75 auditUpdate.target.createdTimestamp,
76 renderDelta(auditUpdate.target.createdTimestamp)
77 );
78 list.edited = entry(
79 after.editedTimestamp,
80 renderDelta(new Date().getTime())
81 );
82 list.editedBy = entry(
83 auditUpdate.executor.id,
84 renderUser(auditUpdate.executor)
85 );
pineafanda6e5342022-07-03 10:03:16 +010086 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010087 action = "Update";
Skyler Grey75ea9172022-08-06 10:22:23 +010088 } else if (
89 auditDelete &&
90 auditDelete.createdTimestamp > audit.createdTimestamp
91 ) {
92 const { before } = auditDelete.changes.reduce(
93 (acc, change) => {
94 acc.before[change.key] = change.old;
95 acc.after[change.key] = change.new;
96 return acc;
97 },
98 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +010099 );
100 list = {
101 name: entry(before.name, `${before.name}`),
Skyler Grey75ea9172022-08-06 10:22:23 +0100102 channel: entry(
103 before.channel_id,
104 renderChannel(
105 await client.channels.fetch(before.channel_id)
106 )
107 ),
108 created: entry(
109 auditDelete.target.createdTimestamp,
110 renderDelta(auditDelete.target.createdTimestamp)
111 ),
112 deleted: entry(
113 new Date().getTime(),
114 renderDelta(new Date().getTime())
115 ),
116 deletedBy: entry(
117 auditDelete.executor.id,
118 renderUser(
119 (
120 await channel.guild.members.fetch(
121 auditDelete.executor.id
122 )
123 ).user
124 )
125 )
pineafan63fc5e22022-08-04 22:04:10 +0100126 };
pineafanda6e5342022-07-03 10:03:16 +0100127 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +0100128 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +0100129 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100130 const { before } = auditDelete.changes.reduce(
131 (acc, change) => {
132 acc.before[change.key] = change.old;
133 acc.after[change.key] = change.new;
134 return acc;
135 },
136 { before: {}, after: {} }
pineafanda6e5342022-07-03 10:03:16 +0100137 );
138 list = {
139 name: entry(before.name, `${before.name}`),
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 channel: entry(
141 before.channel_id,
142 renderChannel(
143 await client.channels.fetch(before.channel_id)
144 )
145 ),
146 createdBy: entry(
147 auditCreate.executor.id,
148 renderUser(
149 (
150 await channel.guild.members.fetch(
151 auditCreate.executor.id
152 )
153 ).user
154 )
155 ),
156 created: entry(
157 new Date().getTime(),
158 renderDelta(new Date().getTime())
159 )
pineafan63fc5e22022-08-04 22:04:10 +0100160 };
pineafanda6e5342022-07-03 10:03:16 +0100161 }
pineafan63fc5e22022-08-04 22:04:10 +0100162 const cols = {
Skyler Grey75ea9172022-08-06 10:22:23 +0100163 Create: "green",
164 Update: "yellow",
165 Delete: "red"
pineafan63fc5e22022-08-04 22:04:10 +0100166 };
167 const data = {
pineafanda6e5342022-07-03 10:03:16 +0100168 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100169 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +0100170 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +0100171 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +0100172 color: NucleusColors[cols[action]],
173 emoji: "WEBHOOK." + action.toUpperCase(),
174 timestamp: new Date().getTime()
175 },
176 list: list,
177 hidden: {
178 guild: channel.guild.id
179 }
pineafan63fc5e22022-08-04 22:04:10 +0100180 };
pineafanda6e5342022-07-03 10:03:16 +0100181 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 } catch (e) {
183 console.log(e);
184 }
pineafanda6e5342022-07-03 10:03:16 +0100185}