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