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"; |
TheCodedProf | 2ffc154 | 2023-04-23 16:42:18 -0400 | [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 | |
| 51 | export async function callback(_client: NucleusClient, channel: GuildBasedChannel) { |
| 52 | // await deleteFromGuildConfig(channel) |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 53 | const { getAuditLog, log, isLogging, NucleusColors, entry, renderDelta, renderUser } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 54 | if (!(await isLogging(channel.guild.id, "channelUpdate"))) return; |
| 55 | const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelDelete)).filter( |
| 56 | (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id |
| 57 | )[0]; |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 58 | if (!auditLog) return; |
| 59 | if (auditLog.executor!.id === client.user!.id) return; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 60 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | let emoji; |
| 62 | let readableType; |
| 63 | let displayName; |
| 64 | switch (channel.type) { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 65 | case ChannelType.GuildText: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | emoji = "CHANNEL.TEXT.DELETE"; |
| 67 | readableType = "Text"; |
| 68 | displayName = "Text Channel"; |
| 69 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 70 | } |
| 71 | case ChannelType.GuildAnnouncement: { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 72 | emoji = "CHANNEL.TEXT.DELETE"; |
| 73 | readableType = "Announcement"; |
| 74 | displayName = "Announcement Channel"; |
| 75 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 76 | } |
| 77 | case ChannelType.GuildVoice: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | emoji = "CHANNEL.VOICE.DELETE"; |
| 79 | readableType = "Voice"; |
| 80 | displayName = "Voice Channel"; |
| 81 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 82 | } |
| 83 | case ChannelType.GuildCategory: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | emoji = "CHANNEL.CATEGORY.DELETE"; |
| 85 | readableType = "Category"; |
| 86 | displayName = "Category"; |
| 87 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 88 | } |
| 89 | case ChannelType.GuildStageVoice: { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 90 | emoji = "CHANNEL.VOICE.DELETE"; |
| 91 | readableType = "Stage"; |
| 92 | displayName = "Stage Channel"; |
| 93 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 94 | } |
| 95 | case ChannelType.GuildForum: { |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 96 | emoji = "CHANNEL.TEXT.DELETE"; |
| 97 | readableType = "Forum"; |
| 98 | displayName = "Forum Channel"; |
| 99 | break; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 100 | } |
| 101 | default: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 102 | emoji = "CHANNEL.TEXT.DELETE"; |
| 103 | readableType = "Channel"; |
| 104 | displayName = "Channel"; |
| 105 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 106 | } |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 107 | const list: { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 108 | channelId: ReturnType<typeof entry>; |
| 109 | name: ReturnType<typeof entry>; |
| 110 | topic?: ReturnType<typeof entry> | null; |
| 111 | type: ReturnType<typeof entry>; |
| 112 | category: ReturnType<typeof entry>; |
| 113 | nsfw?: ReturnType<typeof entry> | null; |
| 114 | created: ReturnType<typeof entry>; |
| 115 | deleted: ReturnType<typeof entry>; |
| 116 | deletedBy: ReturnType<typeof entry>; |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 117 | } = { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | channelId: entry(channel.id, `\`${channel.id}\``), |
| 119 | name: entry(channel.id, `${channel.name}`), |
| 120 | topic: null, |
| 121 | type: entry(channel.type, readableType), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 122 | category: entry( |
| 123 | channel.parent ? channel.parent.id : null, |
| 124 | channel.parent ? channel.parent.name : "Uncategorised" |
| 125 | ), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | nsfw: null, |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 127 | created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 128 | deleted: entry(Date.now(), renderDelta(Date.now())), |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 129 | deletedBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 130 | }; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 131 | if ((channel instanceof BaseGuildTextChannel || channel instanceof StageChannel) && channel.topic !== null) |
| 132 | list.topic = entry(channel.topic, `\`\`\`\n${channel.topic.replace("`", "'")}\n\`\`\``); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | else delete list.topic; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 134 | if ( |
| 135 | channel instanceof BaseGuildTextChannel || |
| 136 | channel instanceof VoiceChannel || |
| 137 | channel instanceof ThreadChannel |
| 138 | ) { |
| 139 | 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] | 140 | list.nsfw = entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 141 | nsfw, |
| 142 | nsfw ? `${getEmojiByName("CONTROL.TICK")} Yes` : `${getEmojiByName("CONTROL.CROSS")} No` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 143 | ); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 144 | } else { |
| 145 | delete list.nsfw; |
| 146 | } |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 147 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 148 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 150 | type: "channelDelete", |
| 151 | displayName: displayName + " Deleted", |
| 152 | calculateType: "channelUpdate", |
| 153 | color: NucleusColors.red, |
| 154 | emoji: emoji, |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 155 | timestamp: auditLog.createdTimestamp |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | }, |
| 157 | list: list, |
| 158 | hidden: { |
| 159 | guild: channel.guild.id |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 160 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 161 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 162 | await log(data); |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 163 | } |