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