pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 1 | import Discord, { CategoryChannel, CommandInteraction, GuildMember, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 4 | import { WrappedCheck } from "jshaiku"; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 5 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import pageIndicator from "../../utils/createPageIndicator.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 7 | |
| 8 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 9 | builder |
| 10 | .setName("viewas") |
| 11 | .setDescription("View the server as a specific member") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 12 | .addUserOption(option => option.setName("member").setDescription("The member to view as").setRequired(true)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 13 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 14 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
| 15 | let channels = []; |
| 16 | let m; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 17 | interaction.guild.channels.cache.forEach(channel => { |
| 18 | if (!channel.parent && channel.type !== "GUILD_CATEGORY") channels.push(channel) |
| 19 | }) |
| 20 | channels = [channels] |
| 21 | channels = channels.concat(interaction.guild.channels.cache |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 22 | .filter(c => c.type === "GUILD_CATEGORY") |
| 23 | .map(c => (c as CategoryChannel).children.map(c => c)) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 24 | ) |
| 25 | let autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"] |
| 26 | channels = channels.map(c => c.sort((a, b) => { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 27 | if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.position - b.position |
| 28 | if (autoSortBelow.includes(a.type)) return 1 |
| 29 | if (autoSortBelow.includes(b.type)) return -1 |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 30 | return a.position - b.position |
| 31 | })) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 32 | // Sort all arrays by the position of the first channels parent position |
| 33 | channels = channels.sort((a, b) => { |
| 34 | if (!a[0].parent) return -1 |
| 35 | if (!b[0].parent) return 1 |
| 36 | return a[0].parent.position - b[0].parent.position |
| 37 | }) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 38 | let member = interaction.options.getMember("member") as Discord.GuildMember |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 39 | m = await interaction.reply({embeds: [new EmojiEmbed() |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 40 | .setEmoji("MEMBER.JOIN") |
| 41 | .setTitle("Viewing as " + member.displayName) |
| 42 | .setStatus("Success") |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 43 | ], ephemeral: true, fetchReply: true}) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 44 | let page = 0; |
| 45 | while (true) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 46 | m = await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 47 | .setEmoji("MEMBER.JOIN") |
| 48 | .setTitle("Viewing as " + member.displayName) |
| 49 | .setStatus("Success") |
| 50 | .setDescription( |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 51 | `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` + "\n" + |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 52 | channels[page].map(c => { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | let channelType = c.type |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 54 | if (interaction.guild.rulesChannelId === c.id) channelType = "RULES" |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 55 | else if ("nsfw" in c && c.nsfw) channelType += "_NSFW" |
| 56 | return c.permissionsFor(member).has("VIEW_CHANNEL") ? ( |
| 57 | `${getEmojiByName("ICONS.CHANNEL." + channelType)} ${c.name}\n` + (() => { |
| 58 | if ("threads" in c && c.threads.cache.size > 0) { |
| 59 | return c.threads.cache.map(t => ` ${ |
| 60 | getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") + " " + |
| 61 | getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL")} ${t.name}`).join("\n") + "\n" |
| 62 | }return "" |
| 63 | })()) : "" |
| 64 | }).join("") + "\n" + pageIndicator(channels.length, page) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 65 | ) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 66 | ], components: [ |
| 67 | new MessageActionRow().addComponents([new MessageSelectMenu().setOptions(channels.map((c, index) => ({ |
| 68 | label: c[0].parent ? c[0].parent.name : "Uncategorised", |
| 69 | value: index.toString(), |
| 70 | default: page === index |
| 71 | }))).setCustomId("select").setMaxValues(1).setMinValues(1).setPlaceholder("Select a category")]), |
| 72 | new MessageActionRow().addComponents([ |
| 73 | new MessageButton() |
| 74 | .setLabel(page === 0 ? "" : (channels[page - 1][0].parent ? channels[page - 1][0].parent.name : "Uncategorised")) |
| 75 | .setDisabled(page === 0) |
| 76 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 77 | .setStyle("PRIMARY") |
| 78 | .setCustomId("previous"), |
| 79 | new MessageButton() |
| 80 | .setLabel(page === channels.length - 1 ? "" : (channels[page + 1][0].parent ? channels[page + 1][0].parent.name : "Uncategorised")) |
| 81 | .setDisabled(page === channels.length - 1) |
| 82 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 83 | .setStyle("PRIMARY") |
| 84 | .setCustomId("next") |
| 85 | ]) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 86 | ]}) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 87 | let i; |
| 88 | try { |
| 89 | i = await m.awaitMessageComponent({ time: 300000 }); |
| 90 | } catch (e) { return } |
| 91 | i.deferUpdate() |
| 92 | if (i.customId === "next") { page++; } |
| 93 | else if (i.customId === "previous") { page--; } |
| 94 | else if (i.customId === "select") { page = parseInt(i.values[0]); } |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 95 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 98 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 99 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 100 | let member = (interaction.member as GuildMember) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 101 | if (! member.permissions.has("MANAGE_ROLES")) throw "You do not have the *Manage Roles* permission"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 102 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 105 | export { command, callback, check }; |