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