Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 1 | import { |
| 2 | BaseGuildTextChannel, |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 3 | GuildAuditLogsEntry, |
| 4 | GuildBasedChannel, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 5 | StageChannel, |
| 6 | ThreadChannel, |
| 7 | VoiceChannel |
| 8 | } from "discord.js"; |
| 9 | // @ts-expect-error |
| 10 | import type { HaikuClient } from "jshaiku"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 11 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 12 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 13 | export const event = "channelDelete"; |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 14 | |
| 15 | export async function callback(client: HaikuClient, channel: GuildBasedChannel) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 16 | const { getAuditLog, log, NucleusColors, entry, renderDelta, renderUser } = client.logger; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 17 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 18 | const auditLog = await getAuditLog(channel.guild, "CHANNEL_DELETE"); |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 19 | const audit = auditLog.entries.filter((entry: GuildAuditLogsEntry) => entry.target!.id === channel.id).first(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 20 | if (audit.executor.id === client.user.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 21 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 22 | let emoji; |
| 23 | let readableType; |
| 24 | let displayName; |
| 25 | switch (channel.type) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | case "GUILD_TEXT": { |
| 27 | emoji = "CHANNEL.TEXT.DELETE"; |
| 28 | readableType = "Text"; |
| 29 | displayName = "Text Channel"; |
| 30 | break; |
| 31 | } |
| 32 | case "GUILD_VOICE": { |
| 33 | emoji = "CHANNEL.VOICE.DELETE"; |
| 34 | readableType = "Voice"; |
| 35 | displayName = "Voice Channel"; |
| 36 | break; |
| 37 | } |
| 38 | case "GUILD_CATEGORY": { |
| 39 | emoji = "CHANNEL.CATEGORY.DELETE"; |
| 40 | readableType = "Category"; |
| 41 | displayName = "Category"; |
| 42 | break; |
| 43 | } |
| 44 | default: { |
| 45 | emoji = "CHANNEL.TEXT.DELETE"; |
| 46 | readableType = "Channel"; |
| 47 | displayName = "Channel"; |
| 48 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | } |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 50 | const list: { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 51 | channelId: ReturnType<typeof entry>; |
| 52 | name: ReturnType<typeof entry>; |
| 53 | topic?: ReturnType<typeof entry> | null; |
| 54 | type: ReturnType<typeof entry>; |
| 55 | category: ReturnType<typeof entry>; |
| 56 | nsfw?: ReturnType<typeof entry> | null; |
| 57 | created: ReturnType<typeof entry>; |
| 58 | deleted: ReturnType<typeof entry>; |
| 59 | deletedBy: ReturnType<typeof entry>; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 60 | } = { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | channelId: entry(channel.id, `\`${channel.id}\``), |
| 62 | name: entry(channel.id, `${channel.name}`), |
| 63 | topic: null, |
| 64 | type: entry(channel.type, readableType), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 65 | category: entry( |
| 66 | channel.parent ? channel.parent.id : null, |
| 67 | channel.parent ? channel.parent.name : "Uncategorised" |
| 68 | ), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 69 | nsfw: null, |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 70 | created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 71 | deleted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 72 | deletedBy: entry(audit.executor.id, renderUser(audit.executor)) |
| 73 | }; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 74 | if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null) |
| 75 | list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | else delete list.topic; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 77 | if ( |
| 78 | channel instanceof BaseGuildTextChannel || |
| 79 | channel instanceof VoiceChannel || |
| 80 | channel instanceof ThreadChannel |
| 81 | ) { |
| 82 | const nsfw = channel instanceof ThreadChannel ? (channel as ThreadChannel).parent?.nsfw ?? false : channel.nsfw; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | list.nsfw = entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 84 | nsfw, |
| 85 | nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | ); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 87 | } else { |
| 88 | delete list.nsfw; |
| 89 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 90 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 92 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | type: "channelDelete", |
| 94 | displayName: displayName + " Deleted", |
| 95 | calculateType: "channelUpdate", |
| 96 | color: NucleusColors.red, |
| 97 | emoji: emoji, |
| 98 | timestamp: audit.createdTimestamp |
| 99 | }, |
| 100 | list: list, |
| 101 | hidden: { |
| 102 | guild: channel.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 103 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | }; |
| 105 | log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 106 | } |