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