Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 1 | import Discord, { |
| 2 | CategoryChannel, |
| 3 | CommandInteraction, |
| 4 | GuildMember, |
| 5 | MessageActionRow, |
| 6 | MessageButton, |
| 7 | MessageSelectMenu |
| 8 | } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 10 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 11 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 12 | import pageIndicator from "../../utils/createPageIndicator.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 13 | |
| 14 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 15 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | .setName("viewas") |
| 17 | .setDescription("View the server as a specific member") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 18 | .addUserOption((option) => option.setName("member").setDescription("The member to view as").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 19 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 20 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 21 | let channels = []; |
| 22 | let m; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 23 | interaction.guild.channels.cache.forEach((channel) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 24 | if (!channel.parent && channel.type !== "GUILD_CATEGORY") channels.push(channel); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 25 | }); |
| 26 | channels = [channels]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | channels = channels.concat( |
| 28 | interaction.guild.channels.cache |
| 29 | .filter((c) => c.type === "GUILD_CATEGORY") |
| 30 | .map((c) => (c as CategoryChannel).children.map((c) => c)) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 31 | ); |
| 32 | const autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"]; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | channels = channels.map((c) => |
| 34 | c.sort((a, b) => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 35 | 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] | 36 | if (autoSortBelow.includes(a.type)) return 1; |
| 37 | if (autoSortBelow.includes(b.type)) return -1; |
| 38 | return a.position - b.position; |
| 39 | }) |
| 40 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 41 | // Sort all arrays by the position of the first channels parent position |
| 42 | channels = channels.sort((a, b) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | if (!a[0].parent) return -1; |
| 44 | if (!b[0].parent) return 1; |
| 45 | return a[0].parent.position - b[0].parent.position; |
| 46 | }); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 47 | const member = interaction.options.getMember("member") as Discord.GuildMember; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | m = await interaction.reply({ |
| 49 | embeds: [ |
| 50 | new EmojiEmbed() |
| 51 | .setEmoji("MEMBER.JOIN") |
| 52 | .setTitle("Viewing as " + member.displayName) |
| 53 | .setStatus("Success") |
| 54 | ], |
| 55 | ephemeral: true, |
| 56 | fetchReply: true |
| 57 | }); |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 58 | let page = 0; |
| 59 | while (true) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | m = await interaction.editReply({ |
| 61 | embeds: [ |
| 62 | new EmojiEmbed() |
| 63 | .setEmoji("MEMBER.JOIN") |
| 64 | .setTitle("Viewing as " + member.displayName) |
| 65 | .setStatus("Success") |
| 66 | .setDescription( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 67 | `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 68 | "\n" + |
| 69 | channels[page] |
| 70 | .map((c) => { |
| 71 | let channelType = c.type; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 72 | if (interaction.guild.rulesChannelId === c.id) channelType = "RULES"; |
| 73 | else if ("nsfw" in c && c.nsfw) channelType += "_NSFW"; |
| 74 | return c.permissionsFor(member).has("VIEW_CHANNEL") |
| 75 | ? `${getEmojiByName("ICONS.CHANNEL." + channelType)} ${c.name}\n` + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 76 | (() => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 77 | if ("threads" in c && c.threads.cache.size > 0) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 78 | return ( |
| 79 | c.threads.cache |
| 80 | .map( |
| 81 | (t) => |
| 82 | ` ${ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 83 | getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | " " + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 85 | getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL") |
| 86 | } ${t.name}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 87 | ) |
| 88 | .join("\n") + "\n" |
| 89 | ); |
| 90 | } |
| 91 | return ""; |
| 92 | })() |
| 93 | : ""; |
| 94 | }) |
| 95 | .join("") + |
| 96 | "\n" + |
| 97 | pageIndicator(channels.length, page) |
| 98 | ) |
| 99 | ], |
| 100 | components: [ |
| 101 | new MessageActionRow().addComponents([ |
| 102 | new MessageSelectMenu() |
| 103 | .setOptions( |
| 104 | channels.map((c, index) => ({ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 105 | label: c[0].parent ? c[0].parent.name : "Uncategorised", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 106 | value: index.toString(), |
| 107 | default: page === index |
| 108 | })) |
| 109 | ) |
| 110 | .setCustomId("select") |
| 111 | .setMaxValues(1) |
| 112 | .setMinValues(1) |
| 113 | .setPlaceholder("Select a category") |
| 114 | ]), |
| 115 | new MessageActionRow().addComponents([ |
| 116 | new MessageButton() |
| 117 | .setLabel( |
| 118 | page === 0 |
| 119 | ? "" |
| 120 | : channels[page - 1][0].parent |
| 121 | ? channels[page - 1][0].parent.name |
| 122 | : "Uncategorised" |
| 123 | ) |
| 124 | .setDisabled(page === 0) |
| 125 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 126 | .setStyle("PRIMARY") |
| 127 | .setCustomId("previous"), |
| 128 | new MessageButton() |
| 129 | .setLabel( |
| 130 | page === channels.length - 1 |
| 131 | ? "" |
| 132 | : channels[page + 1][0].parent |
| 133 | ? channels[page + 1][0].parent.name |
| 134 | : "Uncategorised" |
| 135 | ) |
| 136 | .setDisabled(page === channels.length - 1) |
| 137 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 138 | .setStyle("PRIMARY") |
| 139 | .setCustomId("next") |
| 140 | ]) |
| 141 | ] |
| 142 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 143 | let i; |
| 144 | try { |
| 145 | i = await m.awaitMessageComponent({ time: 300000 }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | } catch (e) { |
| 147 | return; |
| 148 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | if (i.customId === "next") { |
| 151 | page++; |
| 152 | } else if (i.customId === "previous") { |
| 153 | page--; |
| 154 | } else if (i.customId === "select") { |
| 155 | page = parseInt(i.values[0]); |
| 156 | } |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 157 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 158 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 159 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 160 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 161 | const member = interaction.member as GuildMember; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 162 | if (!member.permissions.has("MANAGE_ROLES")) throw "You do not have the *Manage Roles* permission"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 163 | return true; |
| 164 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 165 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 166 | export { command, callback, check }; |