pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 1 | import type Discord from "discord.js"; |
| 2 | // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 3 | // @ts-ignore |
| 4 | import type { HaikuClient } from "jshaiku"; |
| 5 | export const event = "webhookUpdate"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 6 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 7 | export async function callback(client: HaikuClient, channel: Discord.GuildChannel) { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 8 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 9 | 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"); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 13 | [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([auditLogCreate, auditLogUpdate, auditLogDelete]); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 14 | 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(); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 17 | if (!auditCreate && !auditUpdate && !auditDelete) return; |
| 18 | let audit = auditCreate; |
| 19 | let action = "Create"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 20 | let list = {} as {created: { value: string, displayValue: string }, updated: { value: string, displayValue: string }, deleted: { value: string, displayValue: string }}; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 21 | if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 22 | const {before, after} = auditUpdate.changes.reduce( |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 23 | (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; }, |
| 24 | {before: {}, after: {}} |
| 25 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 26 | 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))); |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 28 | 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; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 33 | action = "Update"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 34 | } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 35 | const {before} = auditDelete.changes.reduce( |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 36 | (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())), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 44 | deletedBy: entry(auditDelete.executor.id, renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user)) |
| 45 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 46 | audit = auditDelete; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 47 | action = "Delete"; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 48 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 49 | const {before} = auditDelete.changes.reduce( |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 50 | (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)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 57 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())) |
| 58 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 59 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 60 | const cols = { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 61 | "Create": "green", |
| 62 | "Update": "yellow", |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 63 | "Delete": "red" |
| 64 | }; |
| 65 | const data = { |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 66 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 67 | type: "webhook" + action, |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 68 | displayName: `Webhook ${action}d`, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 69 | calculateType: "webhookUpdate", |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 70 | 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 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 78 | }; |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 79 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame^] | 80 | } catch(e) { console.log(e); } |
pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 81 | } |