PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 1 | import { AuditLogEvent, ChannelType, GuildAuditLogsEntry } from "discord.js"; |
pineafan | 4e42594 | 2022-08-08 22:01:47 +0100 | [diff] [blame] | 2 | import type { GuildBasedChannel } from "discord.js"; |
PineaFan | 752af46 | 2022-12-31 21:59:38 +0000 | [diff] [blame] | 3 | import type { NucleusClient } from "../utils/client.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 4 | export const event = "channelCreate"; |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 5 | |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 6 | export async function callback(client: NucleusClient, channel: GuildBasedChannel) { |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 7 | const { getAuditLog, log, isLogging, NucleusColors, entry, renderUser, renderDelta, renderChannel } = client.logger; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 8 | if (!(await isLogging(channel.guild.id, "channelUpdate"))) return; |
| 9 | const auditLog = (await getAuditLog(channel.guild, AuditLogEvent.ChannelCreate)).filter( |
| 10 | (entry: GuildAuditLogsEntry) => (entry.target as GuildBasedChannel)!.id === channel.id |
| 11 | )[0]; |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 12 | if (!auditLog) return; |
| 13 | if (auditLog.executor!.id === client.user!.id) return; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | let emoji; |
| 15 | let readableType; |
| 16 | let displayName; |
| 17 | switch (channel.type) { |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 18 | case ChannelType.GuildText: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | emoji = "CHANNEL.TEXT.CREATE"; |
| 20 | readableType = "Text"; |
| 21 | displayName = "Text Channel"; |
| 22 | break; |
| 23 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 24 | case ChannelType.GuildAnnouncement: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 25 | emoji = "CHANNEL.TEXT.CREATE"; |
| 26 | readableType = "Announcement"; |
| 27 | displayName = "Announcement Channel"; |
| 28 | break; |
| 29 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 30 | case ChannelType.GuildVoice: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 31 | emoji = "CHANNEL.VOICE.CREATE"; |
| 32 | readableType = "Voice"; |
| 33 | displayName = "Voice Channel"; |
| 34 | break; |
| 35 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 36 | case ChannelType.GuildStageVoice: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 37 | emoji = "CHANNEL.VOICE.CREATE"; |
| 38 | readableType = "Stage"; |
| 39 | displayName = "Stage Channel"; |
| 40 | break; |
| 41 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 42 | case ChannelType.GuildCategory: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 43 | emoji = "CHANNEL.CATEGORY.CREATE"; |
| 44 | readableType = "Category"; |
| 45 | displayName = "Category"; |
| 46 | break; |
| 47 | } |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 48 | case ChannelType.GuildForum: { |
| 49 | emoji = "CHANNEL.TEXT.CREATE"; |
| 50 | readableType = "Forum"; |
| 51 | displayName = "Forum Channel"; |
| 52 | break; |
| 53 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 54 | default: { |
| 55 | emoji = "CHANNEL.TEXT.CREATE"; |
| 56 | readableType = "Channel"; |
| 57 | displayName = "Channel"; |
| 58 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | } |
| 60 | const data = { |
| 61 | meta: { |
| 62 | type: "channelCreate", |
| 63 | displayName: displayName + " Created", |
| 64 | calculateType: "channelUpdate", |
| 65 | color: NucleusColors.green, |
| 66 | emoji: emoji, |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 67 | timestamp: channel.createdTimestamp ?? Date.now() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | }, |
| 69 | list: { |
| 70 | channelId: entry(channel.id, `\`${channel.id}\``), |
| 71 | name: entry(channel.name, renderChannel(channel)), |
| 72 | type: entry(channel.type, readableType), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | category: entry( |
| 74 | channel.parent ? channel.parent.id : null, |
| 75 | channel.parent ? channel.parent.name : "Uncategorised" |
| 76 | ), |
PineaFan | 538d375 | 2023-01-12 21:48:23 +0000 | [diff] [blame] | 77 | createdBy: entry(auditLog.executor!.id, renderUser(auditLog.executor!)), |
| 78 | created: entry(channel.createdTimestamp, renderDelta(channel.createdTimestamp!)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | }, |
| 80 | hidden: { |
| 81 | guild: channel.guild.id |
pineafan | 3276721 | 2022-03-14 21:27:39 +0000 | [diff] [blame] | 82 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | }; |
Skyler Grey | f4f21c4 | 2023-03-08 14:36:29 +0000 | [diff] [blame] | 84 | await log(data); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 85 | } |