Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 1 | import { LoadingEmbed } from "./../../utils/defaults.js"; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | import Discord, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 3 | CommandInteraction, |
| 4 | GuildMember, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 5 | ActionRowBuilder, |
| 6 | ButtonBuilder, |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 7 | ButtonStyle, |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 8 | NonThreadGuildBasedChannel, |
| 9 | StringSelectMenuOptionBuilder, |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 10 | StringSelectMenuBuilder, |
| 11 | APIMessageComponentEmoji |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | } from "discord.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 13 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 14 | import type { GuildBasedChannel } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 15 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 16 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 17 | import pageIndicator from "../../utils/createPageIndicator.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | |
| 19 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 20 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 21 | .setName("viewas") |
| 22 | .setDescription("View the server as a specific member") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 23 | .addUserOption((option) => option.setName("member").setDescription("The member to view as").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 24 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 25 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 26 | const m = await interaction.reply({ embeds: LoadingEmbed, ephemeral: true, fetchReply: true }); |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 27 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 28 | let channels: Record<string, GuildBasedChannel[]> = { "": [] }; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 29 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 30 | const channelCollection = await interaction.guild!.channels.fetch(); |
| 31 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 32 | channelCollection.forEach((channel) => { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 33 | if (!channel) return; // if no channel |
| 34 | if (channel.type === Discord.ChannelType.GuildCategory) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 35 | if (!channels[channel!.id]) channels[channel!.id] = []; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 36 | } else if (channel.parent) { |
| 37 | if (!channels[channel.parent.id]) channels[channel.parent.id] = [channel]; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 38 | else channels[channel.parent.id as string]!.push(channel); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 39 | } else { |
| 40 | channels[""]!.push(channel); |
| 41 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 43 | |
| 44 | const member = interaction.options.getMember("member") as Discord.GuildMember; |
| 45 | const autoSortBelow = [Discord.ChannelType.GuildVoice, Discord.ChannelType.GuildStageVoice]; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 46 | |
| 47 | for (const category in channels) { |
| 48 | channels[category] = channels[category]!.sort((a: GuildBasedChannel, b: GuildBasedChannel) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 49 | const disallowedTypes = [ |
| 50 | Discord.ChannelType.PublicThread, |
| 51 | Discord.ChannelType.PrivateThread, |
| 52 | Discord.ChannelType.AnnouncementThread |
| 53 | ]; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 54 | if (disallowedTypes.includes(a.type) || disallowedTypes.includes(b.type)) return 0; |
| 55 | a = a as NonThreadGuildBasedChannel; |
| 56 | b = b as NonThreadGuildBasedChannel; |
| 57 | if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.position - b.position; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | if (autoSortBelow.includes(a.type)) return 1; |
| 59 | if (autoSortBelow.includes(b.type)) return -1; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 60 | return a.position - b.position; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | }); |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 62 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 63 | for (const category in channels) { |
| 64 | channels[category] = channels[category]!.filter((c) => { |
| 65 | return c.permissionsFor(member).has("ViewChannel"); |
| 66 | }); |
| 67 | } |
| 68 | for (const category in channels) { |
| 69 | channels[category] = channels[category]!.filter((c) => { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 70 | return !( |
| 71 | c.type === Discord.ChannelType.PublicThread || |
| 72 | c.type === Discord.ChannelType.PrivateThread || |
| 73 | c.type === Discord.ChannelType.AnnouncementThread |
| 74 | ); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 75 | }); |
| 76 | } |
| 77 | channels = Object.fromEntries(Object.entries(channels).filter(([_, v]) => v.length > 0)); |
| 78 | let page = 0; |
| 79 | let closed = false; |
| 80 | const categoryIDs = Object.keys(channels); |
| 81 | const categoryNames = Object.values(channels).map((c) => { |
| 82 | return c[0]!.parent?.name ?? "Uncategorised"; |
| 83 | }); |
| 84 | // Split the category names into the first and last 25, ignoring the last 25 if there are 25 or less |
| 85 | const first25 = categoryNames.slice(0, 25); |
| 86 | const last25 = categoryNames.slice(25); |
| 87 | const categoryNames25: string[][] = [first25]; |
| 88 | if (last25.length > 0) categoryNames25.push(last25); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 89 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 90 | const channelTypeEmoji: Record<number, string> = { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 91 | 0: "GUILD_TEXT", // Text channel |
| 92 | 2: "GUILD_VOICE", // Voice channel |
| 93 | 5: "GUILD_NEWS", // Announcement channel |
| 94 | 13: "GUILD_STAGE_VOICE", // Stage channel |
| 95 | 15: "FORUM", // Forum channel |
| 96 | 99: "RULES" // Rules channel |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 97 | }; |
| 98 | const NSFWAvailable: number[] = [0, 2, 5, 13]; |
| 99 | const rulesChannel = interaction.guild!.rulesChannel?.id; |
| 100 | |
| 101 | async function nameFromChannel(channel: GuildBasedChannel): Promise<string> { |
PineaFan | 638eb13 | 2023-01-19 10:41:22 +0000 | [diff] [blame] | 102 | let channelType: Discord.ChannelType | 99 = channel.type; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 103 | if (channelType === Discord.ChannelType.GuildCategory) return ""; |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 104 | if (channel.id === rulesChannel) channelType = 99; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 105 | let threads: Discord.ThreadChannel[] = []; |
| 106 | if ("threads" in channel) { |
| 107 | threads = channel.threads.cache.toJSON().map((t) => t as Discord.ThreadChannel); |
| 108 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 109 | const nsfw = ("nsfw" in channel ? channel.nsfw : false) && NSFWAvailable.includes(channelType); |
TheCodedProf | 5129610 | 2023-01-18 22:35:02 -0500 | [diff] [blame] | 110 | const emojiName = channelTypeEmoji[channelType.valueOf()] + (nsfw ? "_NSFW" : ""); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 111 | const emoji = getEmojiByName("ICONS.CHANNEL." + (threads.length ? "THREAD_CHANNEL" : emojiName)); |
| 112 | let current = `${emoji} ${channel.name}`; |
| 113 | if (threads.length) { |
| 114 | for (const thread of threads) { |
| 115 | current += `\n${getEmojiByName("ICONS.CHANNEL.THREAD_PIPE")} ${thread.name}`; |
| 116 | } |
| 117 | } |
| 118 | return current; |
| 119 | } |
| 120 | |
| 121 | while (!closed) { |
| 122 | const category = categoryIDs[page]!; |
| 123 | let description = ""; |
| 124 | for (const channel of channels[category]!) { |
| 125 | description += `${await nameFromChannel(channel)}\n`; |
| 126 | } |
| 127 | |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 128 | const parsedCategorySelectMenu: ActionRowBuilder<StringSelectMenuBuilder | ButtonBuilder>[] = |
| 129 | categoryNames25.map((categories, set) => { |
| 130 | return new ActionRowBuilder<StringSelectMenuBuilder>().addComponents( |
| 131 | new StringSelectMenuBuilder() |
| 132 | .setCustomId("category") |
| 133 | .setMinValues(1) |
| 134 | .setMaxValues(1) |
| 135 | .setOptions( |
| 136 | categories.map((c, i) => { |
| 137 | return new StringSelectMenuOptionBuilder() |
| 138 | .setLabel(c) |
| 139 | .setValue((set * 25 + i).toString()) |
| 140 | .setEmoji( |
| 141 | getEmojiByName("ICONS.CHANNEL.CATEGORY", "id") as APIMessageComponentEmoji |
| 142 | ) // Again, this is valid but TS doesn't think so |
| 143 | .setDefault(set * 25 + i === page); |
| 144 | }) |
| 145 | ) |
| 146 | ); |
| 147 | }); |
| 148 | |
| 149 | const components: ActionRowBuilder<ButtonBuilder | StringSelectMenuBuilder>[] = parsedCategorySelectMenu; |
| 150 | components.push( |
| 151 | new ActionRowBuilder<ButtonBuilder>().addComponents( |
| 152 | new ButtonBuilder() |
| 153 | .setCustomId("back") |
| 154 | .setStyle(ButtonStyle.Secondary) |
| 155 | .setDisabled(page === 0) |
| 156 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")), |
| 157 | new ButtonBuilder() |
| 158 | .setCustomId("right") |
| 159 | .setStyle(ButtonStyle.Secondary) |
| 160 | .setDisabled(page === categoryIDs.length - 1) |
| 161 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 162 | ) |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 163 | ); |
| 164 | |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 165 | await interaction.editReply({ |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 166 | embeds: [ |
| 167 | new EmojiEmbed() |
| 168 | .setEmoji("MEMBER.JOIN") |
| 169 | .setTitle("Viewing as " + member.displayName) |
| 170 | .setStatus("Success") |
| 171 | .setDescription(description + "\n" + pageIndicator(categoryIDs.length, page)) |
| 172 | ], |
| 173 | components: components |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 174 | }); |
| 175 | let i; |
| 176 | try { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame^] | 177 | i = await m.awaitMessageComponent({ |
| 178 | filter: (i) => i.user.id === interaction.user.id && i.message.id === m.id, |
| 179 | time: 30000 |
| 180 | }); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 181 | } catch (e) { |
| 182 | closed = true; |
| 183 | continue; |
| 184 | } |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 185 | await i.deferUpdate(); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 186 | if (i.customId === "back") page--; |
| 187 | else if (i.customId === "right") page++; |
| 188 | else if (i.customId === "category" && i.isStringSelectMenu()) page = parseInt(i.values[0]!); |
| 189 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 190 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 191 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 192 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 193 | const member = interaction.member as GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 194 | if (!member.permissions.has("ManageRoles")) return "You do not have the *Manage Roles* permission"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 195 | return true; |
| 196 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 197 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 198 | export { command, callback, check }; |