pineafan | da6e534 | 2022-07-03 10:03:16 +0100 | [diff] [blame] | 1 | import humanizeDuration from 'humanize-duration'; |
| 2 | export const event = 'webhookUpdate' |
| 3 | |
| 4 | export async function callback(client, channel) { |
| 5 | try { |
| 6 | const { getAuditLog, log, NucleusColors, entry, renderUser, renderChannel, renderDelta } = client.logger |
| 7 | let auditLogCreate = getAuditLog(channel.guild, 'WEBHOOK_CREATE'); |
| 8 | let auditLogUpdate = getAuditLog(channel.guild, 'WEBHOOK_UPDATE'); |
| 9 | let auditLogDelete = getAuditLog(channel.guild, 'WEBHOOK_DELETE'); |
| 10 | [auditLogCreate, auditLogUpdate, auditLogDelete] = await Promise.all([auditLogCreate, auditLogUpdate, auditLogDelete]); |
| 11 | let auditCreate = auditLogCreate.entries.filter(entry => entry.target.channelId == channel.id).first(); |
| 12 | let auditUpdate = auditLogUpdate.entries.filter(entry => entry.target.channelId == channel.id).first(); |
| 13 | let auditDelete = auditLogDelete.entries.filter(entry => entry.target.channelId == channel.id).first(); |
| 14 | if (!auditCreate && !auditUpdate && !auditDelete) return; |
| 15 | let audit = auditCreate; |
| 16 | let action = "Create"; |
| 17 | let list = {} as any; |
| 18 | if (auditUpdate && auditUpdate.createdTimestamp > audit.createdTimestamp) { |
| 19 | let {before, after} = auditUpdate.changes.reduce( |
| 20 | (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; }, |
| 21 | {before: {}, after: {}} |
| 22 | ); |
| 23 | if (before.name !== after.name) list['name'] = entry([before.name, after.name], `${before.name} -> ${after.name}`) |
| 24 | 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))) |
| 25 | if (!(Object.keys(list)).length) return; |
| 26 | list.created = entry(auditUpdate.target.createdTimestamp, renderDelta(auditUpdate.target.createdTimestamp)); |
| 27 | list.edited = entry(after.editedTimestamp, renderDelta(new Date().getTime())); |
| 28 | list.editedBy = entry(auditUpdate.executor.id, renderUser(auditUpdate.executor)); |
| 29 | audit = auditUpdate; |
| 30 | action = "Update" |
| 31 | } else if (auditDelete && auditDelete.createdTimestamp > audit.createdTimestamp) { |
| 32 | let {before, after} = auditDelete.changes.reduce( |
| 33 | (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; }, |
| 34 | {before: {}, after: {}} |
| 35 | ); |
| 36 | list = { |
| 37 | name: entry(before.name, `${before.name}`), |
| 38 | channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))), |
| 39 | created: entry(auditDelete.target.createdTimestamp, renderDelta(auditDelete.target.createdTimestamp)), |
| 40 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 41 | deletedBy: entry(auditDelete.executor.id, renderUser((await channel.guild.members.fetch(auditDelete.executor.id)).user)), |
| 42 | } |
| 43 | audit = auditDelete; |
| 44 | action = "Delete" |
| 45 | } else { |
| 46 | let {before, after} = auditDelete.changes.reduce( |
| 47 | (acc, change) => { acc.before[change.key] = change.old; acc.after[change.key] = change.new; return acc; }, |
| 48 | {before: {}, after: {}} |
| 49 | ); |
| 50 | list = { |
| 51 | name: entry(before.name, `${before.name}`), |
| 52 | channel: entry(before.channel_id, renderChannel(await client.channels.fetch(before.channel_id))), |
| 53 | createdBy: entry(auditCreate.executor.id, renderUser((await channel.guild.members.fetch(auditCreate.executor.id)).user)), |
| 54 | created: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 55 | } |
| 56 | } |
| 57 | let cols = { |
| 58 | "Create": "green", |
| 59 | "Update": "yellow", |
| 60 | "Delete": "red", |
| 61 | } |
| 62 | let data = { |
| 63 | meta: { |
| 64 | type: 'webhook' + action, |
| 65 | displayName: `Webhook ${action}d`, |
| 66 | calculateType: 'webhookUpdate', |
| 67 | color: NucleusColors[cols[action]], |
| 68 | emoji: "WEBHOOK." + action.toUpperCase(), |
| 69 | timestamp: new Date().getTime() |
| 70 | }, |
| 71 | list: list, |
| 72 | hidden: { |
| 73 | guild: channel.guild.id |
| 74 | } |
| 75 | } // TODO |
| 76 | log(data); |
| 77 | } catch(e) { console.log(e) } |
| 78 | } |