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