blob: a11fa638449fdb251ca7d5106ec6d08fab2cc671 [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
3// @ts-ignore
4import type { HaikuClient } from "jshaiku";
5export const event = "webhookUpdate";
pineafanda6e5342022-07-03 10:03:16 +01006
pineafan63fc5e22022-08-04 22:04:10 +01007export async function callback(client: HaikuClient, channel: Discord.GuildChannel) {
pineafanda6e5342022-07-03 10:03:16 +01008 try {
pineafan63fc5e22022-08-04 22:04:10 +01009 const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger;
10 let auditLogCreate = getAuditLog(channel.guild, "WEBHOOK_CREATE");
11 let auditLogUpdate = getAuditLog(channel.guild, "WEBHOOK_UPDATE");
12 let auditLogDelete = getAuditLog(channel.guild, "WEBHOOK_DELETE");
pineafanda6e5342022-07-03 10:03:16 +010013 [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([auditLogCreate, auditLogUpdate, auditLogDelete]);
pineafan63fc5e22022-08-04 22:04:10 +010014 const auditCreate = auditLogCreate.entries.filter(entry => entry.target.channelId === channel.id).first();
15 const auditUpdate = auditLogUpdate.entries.filter(entry => entry.target.channelId === channel.id).first();
16 const auditDelete = auditLogDelete.entries.filter(entry => entry.target.channelId === channel.id).first();
pineafanda6e5342022-07-03 10:03:16 +010017 if (!auditCreate && !auditUpdate && !auditDelete) return;
18 let audit = auditCreate;
19 let action = "Create";
pineafan63fc5e22022-08-04 22:04:10 +010020 let list = {} as {created: { value: string, displayValue: string }, updated: { value: string, displayValue: string }, deleted: { value: string, displayValue: string }};
pineafanda6e5342022-07-03 10:03:16 +010021 if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) {
pineafan63fc5e22022-08-04 22:04:10 +010022 const {before, after} = auditUpdate.changes.reduce(
pineafanda6e5342022-07-03 10:03:16 +010023 (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; },
24 {before: {}, after: {}}
25 );
pineafan63fc5e22022-08-04 22:04:10 +010026 if (before.name !== after.name) list["name"] = entry([before.name, after.name], `${before.name} -> ${after.name}`);
27 if (before.channel_id !== after.channel_id) list["channel"] = entry([before.channel_id, after.channel_id], renderChannel(await client.channels.fetch(before.channel_id)) + " -> " + renderChannel(await client.channels.fetch(after.channel_id)));
pineafanda6e5342022-07-03 10:03:16 +010028 if (!(Object.keys(list)).length) return;
29 list.created = entry(auditUpdate.target.createdTimestamp, renderDelta(auditUpdate.target.createdTimestamp));
30 list.edited = entry(after.editedTimestamp, renderDelta(new Date().getTime()));
31 list.editedBy = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor));
32 audit = auditUpdate;
pineafan63fc5e22022-08-04 22:04:10 +010033 action = "Update";
pineafanda6e5342022-07-03 10:03:16 +010034 } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) {
pineafan63fc5e22022-08-04 22:04:10 +010035 const {before} = auditDelete.changes.reduce(
pineafanda6e5342022-07-03 10:03:16 +010036 (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; },
37 {before: {}, after: {}}
38 );
39 list = {
40 name: entry(before.name, `${before.name}`),
41 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
42 created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)),
43 deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010044 deletedBy: entry(auditDelete.executor.id, renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user))
45 };
pineafanda6e5342022-07-03 10:03:16 +010046 audit = auditDelete;
pineafan63fc5e22022-08-04 22:04:10 +010047 action = "Delete";
pineafanda6e5342022-07-03 10:03:16 +010048 } else {
pineafan63fc5e22022-08-04 22:04:10 +010049 const {before} = auditDelete.changes.reduce(
pineafanda6e5342022-07-03 10:03:16 +010050 (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; },
51 {before: {}, after: {}}
52 );
53 list = {
54 name: entry(before.name, `${before.name}`),
55 channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))),
56 createdBy: entry(auditCreate.executor.id, renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)),
pineafan63fc5e22022-08-04 22:04:10 +010057 created: entry(new Date().getTime(), renderDelta(new Date().getTime()))
58 };
pineafanda6e5342022-07-03 10:03:16 +010059 }
pineafan63fc5e22022-08-04 22:04:10 +010060 const cols = {
pineafanda6e5342022-07-03 10:03:16 +010061 "Create": "green",
62 "Update": "yellow",
pineafan63fc5e22022-08-04 22:04:10 +010063 "Delete": "red"
64 };
65 const data = {
pineafanda6e5342022-07-03 10:03:16 +010066 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010067 type: "webhook" + action,
pineafanda6e5342022-07-03 10:03:16 +010068 displayName: `Webhook ${action}d`,
pineafan63fc5e22022-08-04 22:04:10 +010069 calculateType: "webhookUpdate",
pineafanda6e5342022-07-03 10:03:16 +010070 color: NucleusColors[cols[action]],
71 emoji: "WEBHOOK." + action.toUpperCase(),
72 timestamp: new Date().getTime()
73 },
74 list: list,
75 hidden: {
76 guild: channel.guild.id
77 }
pineafan63fc5e22022-08-04 22:04:10 +010078 };
pineafanda6e5342022-07-03 10:03:16 +010079 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010080 } catch(e) { console.log(e); }
pineafanda6e5342022-07-03 10:03:16 +010081}