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