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