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; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 59 | let timedOut = false; |
| 60 | while (!timedOut) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | m = await interaction.editReply({ |
| 62 | embeds: [ |
| 63 | new EmojiEmbed() |
| 64 | .setEmoji("MEMBER.JOIN") |
| 65 | .setTitle("Viewing as " + member.displayName) |
| 66 | .setStatus("Success") |
| 67 | .setDescription( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 68 | `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | "\n" + |
| 70 | channels[page] |
| 71 | .map((c) => { |
| 72 | let channelType = c.type; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 73 | if (interaction.guild.rulesChannelId === c.id) channelType = "RULES"; |
| 74 | else if ("nsfw" in c && c.nsfw) channelType += "_NSFW"; |
| 75 | return c.permissionsFor(member).has("VIEW_CHANNEL") |
| 76 | ? `${getEmojiByName("ICONS.CHANNEL." + channelType)} ${c.name}\n` + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | (() => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 78 | if ("threads" in c && c.threads.cache.size > 0) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 79 | return ( |
| 80 | c.threads.cache |
| 81 | .map( |
| 82 | (t) => |
| 83 | ` ${ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 84 | getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") + |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 85 | " " + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 86 | getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL") |
| 87 | } ${t.name}` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 88 | ) |
| 89 | .join("\n") + "\n" |
| 90 | ); |
| 91 | } |
| 92 | return ""; |
| 93 | })() |
| 94 | : ""; |
| 95 | }) |
| 96 | .join("") + |
| 97 | "\n" + |
| 98 | pageIndicator(channels.length, page) |
| 99 | ) |
| 100 | ], |
| 101 | components: [ |
| 102 | new MessageActionRow().addComponents([ |
| 103 | new MessageSelectMenu() |
| 104 | .setOptions( |
| 105 | channels.map((c, index) => ({ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 106 | label: c[0].parent ? c[0].parent.name : "Uncategorised", |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | value: index.toString(), |
| 108 | default: page === index |
| 109 | })) |
| 110 | ) |
| 111 | .setCustomId("select") |
| 112 | .setMaxValues(1) |
| 113 | .setMinValues(1) |
| 114 | .setPlaceholder("Select a category") |
| 115 | ]), |
| 116 | new MessageActionRow().addComponents([ |
| 117 | new MessageButton() |
| 118 | .setLabel( |
| 119 | page === 0 |
| 120 | ? "" |
| 121 | : channels[page - 1][0].parent |
| 122 | ? channels[page - 1][0].parent.name |
| 123 | : "Uncategorised" |
| 124 | ) |
| 125 | .setDisabled(page === 0) |
| 126 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 127 | .setStyle("PRIMARY") |
| 128 | .setCustomId("previous"), |
| 129 | new MessageButton() |
| 130 | .setLabel( |
| 131 | page === channels.length - 1 |
| 132 | ? "" |
| 133 | : channels[page + 1][0].parent |
| 134 | ? channels[page + 1][0].parent.name |
| 135 | : "Uncategorised" |
| 136 | ) |
| 137 | .setDisabled(page === channels.length - 1) |
| 138 | .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 139 | .setStyle("PRIMARY") |
| 140 | .setCustomId("next") |
| 141 | ]) |
| 142 | ] |
| 143 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 144 | let i; |
| 145 | try { |
| 146 | i = await m.awaitMessageComponent({ time: 300000 }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 147 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame^] | 148 | timedOut = true; |
| 149 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 150 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 151 | i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 152 | if (i.customId === "next") { |
| 153 | page++; |
| 154 | } else if (i.customId === "previous") { |
| 155 | page--; |
| 156 | } else if (i.customId === "select") { |
| 157 | page = parseInt(i.values[0]); |
| 158 | } |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 159 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 160 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 161 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 162 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 163 | const member = interaction.member as GuildMember; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 164 | if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | return true; |
| 166 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 167 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 168 | export { command, callback, check }; |