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