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"; |
Skyler Grey | ecab41b | 2023-04-23 20:55:59 +0000 | [diff] [blame^] | 11 | import _client, { NucleusClient } from "../utils/client.js"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 12 | import getEmojiByName from "../utils/getEmojiByName.js"; |
| 13 | |
TheCodedProf | 2ffc154 | 2023-04-23 16:42:18 -0400 | [diff] [blame] | 14 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | export const event = "channelDelete"; |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 16 | |
TheCodedProf | 2ffc154 | 2023-04-23 16:42:18 -0400 | [diff] [blame] | 17 | // function getPropFromObject(splitProp: string[], object: Record<string, unknown>) { |
| 18 | // if (splitProp.length === 0) return null |
| 19 | // if (splitProp.length === 1) { |
| 20 | // return object[splitProp[0]!] |
| 21 | // } |
| 22 | // const property: string = splitProp[0]! |
| 23 | // if (! Object.keys(object).includes(property)) return null; |
| 24 | // splitProp = splitProp.splice(1) |
| 25 | // return getPropFromObject(splitProp, object[property] as Record<string, unknown>) |
| 26 | // } |
| 27 | |
| 28 | // async function deleteFromGuildConfig(channel: GuildBasedChannel) { |
| 29 | // const guildConfig = await client.database.guilds.read(channel.guild.id); |
| 30 | // const lists = [ |
| 31 | // "filters.wordFilter.allowed.channels", |
| 32 | // "filters.invite.allowed.channels", |
| 33 | // "filters.pings.allowed.channels", |
| 34 | // "filters.clean.allowed.channels", |
| 35 | // "filters.autoPublish.allowed.channels" |
| 36 | // ] |
| 37 | // const single = [ |
| 38 | // "welcome.channel", |
| 39 | // "logging.logs.channel", |
| 40 | // "logging.staff.channel", |
| 41 | // "logging.attachments.channel", |
| 42 | // "tickets.category" |
| 43 | // ] |
| 44 | // console.log(guildConfig, lists, single) |
| 45 | // // for (const list of lists) { |
| 46 | // // const index = guildConfig[list].indexOf(channel.id); |
| 47 | // // if (index !== -1) guildConfig[list].splice(index, 1); |
| 48 | // // } |
| 49 | // }; |
| 50 | |
Skyler Grey | ecab41b | 2023-04-23 20:55:59 +0000 | [diff] [blame^] | 51 | export async function callback(client: NucleusClient, channel: GuildBasedChannel) { |
| 52 | // In future, please avoid using client from the outer scope. If you import client separately this |
| 53 | // parameter should shadow it. |
| 54 | |
TheCodedProf | 2ffc154 | 2023-04-23 16:42:18 -0400 | [diff] [blame] | 55 | // await deleteFromGuildConfig(channel) |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 56 | const { getAuditLog, log, isLogging, NucleusColors, entry, renderDelta, renderUser } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 57 | if (!(await isLogging(channel.guild.id, "channelUpdate"))) return; |
| 58 | const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete)).filter( |
| 59 | (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id |
| 60 | )[0]; |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 61 | if (!auditLog) return; |
| 62 | if (auditLog.executor!.id === client.user!.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 63 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 64 | let emoji; |
| 65 | let readableType; |
| 66 | let displayName; |
| 67 | switch (channel.type) { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 68 | case ChannelType.GuildText: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | emoji = "CHANNEL.TEXT.DELETE"; |
| 70 | readableType = "Text"; |
| 71 | displayName = "Text Channel"; |
| 72 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 73 | } |
| 74 | case ChannelType.GuildAnnouncement: { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 75 | emoji = "CHANNEL.TEXT.DELETE"; |
| 76 | readableType = "Announcement"; |
| 77 | displayName = "Announcement Channel"; |
| 78 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 79 | } |
| 80 | case ChannelType.GuildVoice: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 81 | emoji = "CHANNEL.VOICE.DELETE"; |
| 82 | readableType = "Voice"; |
| 83 | displayName = "Voice Channel"; |
| 84 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 85 | } |
| 86 | case ChannelType.GuildCategory: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 87 | emoji = "CHANNEL.CATEGORY.DELETE"; |
| 88 | readableType = "Category"; |
| 89 | displayName = "Category"; |
| 90 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 91 | } |
| 92 | case ChannelType.GuildStageVoice: { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 93 | emoji = "CHANNEL.VOICE.DELETE"; |
| 94 | readableType = "Stage"; |
| 95 | displayName = "Stage Channel"; |
| 96 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 97 | } |
| 98 | case ChannelType.GuildForum: { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 99 | emoji = "CHANNEL.TEXT.DELETE"; |
| 100 | readableType = "Forum"; |
| 101 | displayName = "Forum Channel"; |
| 102 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 103 | } |
| 104 | default: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | emoji = "CHANNEL.TEXT.DELETE"; |
| 106 | readableType = "Channel"; |
| 107 | displayName = "Channel"; |
| 108 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | } |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 110 | const list: { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 111 | channelId: ReturnType<typeof entry>; |
| 112 | name: ReturnType<typeof entry>; |
| 113 | topic?: ReturnType<typeof entry> | null; |
| 114 | type: ReturnType<typeof entry>; |
| 115 | category: ReturnType<typeof entry>; |
| 116 | nsfw?: ReturnType<typeof entry> | null; |
| 117 | created: ReturnType<typeof entry>; |
| 118 | deleted: ReturnType<typeof entry>; |
| 119 | deletedBy: ReturnType<typeof entry>; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 120 | } = { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 121 | channelId: entry(channel.id, `\`${channel.id}\``), |
| 122 | name: entry(channel.id, `${channel.name}`), |
| 123 | topic: null, |
| 124 | type: entry(channel.type, readableType), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 125 | category: entry( |
| 126 | channel.parent ? channel.parent.id : null, |
| 127 | channel.parent ? channel.parent.name : "Uncategorised" |
| 128 | ), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 129 | nsfw: null, |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 130 | created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 131 | deleted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 132 | deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | }; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 134 | if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null) |
| 135 | list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | else delete list.topic; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 137 | if ( |
| 138 | channel instanceof BaseGuildTextChannel || |
| 139 | channel instanceof VoiceChannel || |
| 140 | channel instanceof ThreadChannel |
| 141 | ) { |
| 142 | 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] | 143 | list.nsfw = entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 144 | nsfw, |
| 145 | nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | ); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 147 | } else { |
| 148 | delete list.nsfw; |
| 149 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 150 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 151 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 152 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | type: "channelDelete", |
| 154 | displayName: displayName + " Deleted", |
| 155 | calculateType: "channelUpdate", |
| 156 | color: NucleusColors.red, |
| 157 | emoji: emoji, |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 158 | timestamp: auditLog.createdTimestamp |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 159 | }, |
| 160 | list: list, |
| 161 | hidden: { |
| 162 | guild: channel.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 163 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 164 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 165 | await log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 166 | } |