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