blob: ca52787b8c9ce464fda53c21d95ae5556104c88e [file] [log] [blame]
pineafan4edb7762022-06-26 19:21:04 +01001import Discord, { CategoryChannel, CommandInteraction, GuildMember, MessageActionRow, MessageButton, MessageSelectMenu } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan4edb7762022-06-26 19:21:04 +01003import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanc6158ab2022-06-17 16:34:07 +01004import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4edb7762022-06-26 19:21:04 +01005import pageIndicator from "../../utils/createPageIndicator.js";
pineafan4f164f32022-02-26 22:07:12 +00006
7const command = (builder: SlashCommandSubcommandBuilder) =>
8 builder
pineafan63fc5e22022-08-04 22:04:10 +01009 .setName("viewas")
10 .setDescription("View the server as a specific member")
11 .addUserOption(option => option.setName("member").setDescription("The member to view as").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000012
pineafanbd02b4a2022-08-05 22:01:38 +010013const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan4edb7762022-06-26 19:21:04 +010014 let channels = [];
15 let m;
pineafanc6158ab2022-06-17 16:34:07 +010016 interaction.guild.channels.cache.forEach(channel => {
pineafan63fc5e22022-08-04 22:04:10 +010017 if (!channel.parent && channel.type !== "GUILD_CATEGORY") channels.push(channel);
18 });
19 channels = [channels];
pineafanc6158ab2022-06-17 16:34:07 +010020 channels = channels.concat(interaction.guild.channels.cache
pineafan377794f2022-04-18 19:01:01 +010021 .filter(c => c.type === "GUILD_CATEGORY")
22 .map(c => (c as CategoryChannel).children.map(c => c))
pineafan63fc5e22022-08-04 22:04:10 +010023 );
24 const autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"];
pineafanc6158ab2022-06-17 16:34:07 +010025 channels = channels.map(c => c.sort((a, b) => {
pineafan63fc5e22022-08-04 22:04:10 +010026 if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.position - b.position;
27 if (autoSortBelow.includes(a.type)) return 1;
28 if (autoSortBelow.includes(b.type)) return -1;
29 return a.position - b.position;
30 }));
pineafan4edb7762022-06-26 19:21:04 +010031 // Sort all arrays by the position of the first channels parent position
32 channels = channels.sort((a, b) => {
pineafan63fc5e22022-08-04 22:04:10 +010033 if (!a[0].parent) return -1;
34 if (!b[0].parent) return 1;
35 return a[0].parent.position - b[0].parent.position;
36 });
37 const member = interaction.options.getMember("member") as Discord.GuildMember;
pineafan4edb7762022-06-26 19:21:04 +010038 m = await interaction.reply({embeds: [new EmojiEmbed()
pineafanc6158ab2022-06-17 16:34:07 +010039 .setEmoji("MEMBER.JOIN")
40 .setTitle("Viewing as " + member.displayName)
41 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010042 ], ephemeral: true, fetchReply: true});
pineafanc6158ab2022-06-17 16:34:07 +010043 let page = 0;
44 while (true) {
pineafan4edb7762022-06-26 19:21:04 +010045 m = await interaction.editReply({embeds: [new EmojiEmbed()
pineafanc6158ab2022-06-17 16:34:07 +010046 .setEmoji("MEMBER.JOIN")
47 .setTitle("Viewing as " + member.displayName)
48 .setStatus("Success")
49 .setDescription(
pineafan4edb7762022-06-26 19:21:04 +010050 `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` + "\n" +
pineafanc6158ab2022-06-17 16:34:07 +010051 channels[page].map(c => {
pineafan63fc5e22022-08-04 22:04:10 +010052 let channelType = c.type;
53 if (interaction.guild.rulesChannelId === c.id) channelType = "RULES";
54 else if ("nsfw" in c && c.nsfw) channelType += "_NSFW";
pineafan4edb7762022-06-26 19:21:04 +010055 return c.permissionsFor(member).has("VIEW_CHANNEL") ? (
56 `${getEmojiByName("ICONS.CHANNEL." + channelType)} ${c.name}\n` + (() => {
57 if ("threads" in c && c.threads.cache.size > 0) {
58 return c.threads.cache.map(t => ` ${
59 getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") + " " +
pineafan63fc5e22022-08-04 22:04:10 +010060 getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL")} ${t.name}`).join("\n") + "\n";
61 }return "";
62 })()) : "";
pineafan4edb7762022-06-26 19:21:04 +010063 }).join("") + "\n" + pageIndicator(channels.length, page)
pineafanc6158ab2022-06-17 16:34:07 +010064 )
pineafan4edb7762022-06-26 19:21:04 +010065 ], components: [
66 new MessageActionRow().addComponents([new MessageSelectMenu().setOptions(channels.map((c, index) => ({
67 label: c[0].parent ? c[0].parent.name : "Uncategorised",
68 value: index.toString(),
69 default: page === index
70 }))).setCustomId("select").setMaxValues(1).setMinValues(1).setPlaceholder("Select a category")]),
71 new MessageActionRow().addComponents([
72 new MessageButton()
73 .setLabel(page === 0 ? "" : (channels[page - 1][0].parent ? channels[page - 1][0].parent.name : "Uncategorised"))
74 .setDisabled(page === 0)
75 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
76 .setStyle("PRIMARY")
77 .setCustomId("previous"),
78 new MessageButton()
79 .setLabel(page === channels.length - 1 ? "" : (channels[page + 1][0].parent ? channels[page + 1][0].parent.name : "Uncategorised"))
80 .setDisabled(page === channels.length - 1)
81 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
82 .setStyle("PRIMARY")
83 .setCustomId("next")
84 ])
pineafan63fc5e22022-08-04 22:04:10 +010085 ]});
pineafan4edb7762022-06-26 19:21:04 +010086 let i;
87 try {
88 i = await m.awaitMessageComponent({ time: 300000 });
pineafan63fc5e22022-08-04 22:04:10 +010089 } catch (e) { return; }
90 i.deferUpdate();
pineafan4edb7762022-06-26 19:21:04 +010091 if (i.customId === "next") { page++; }
92 else if (i.customId === "previous") { page--; }
93 else if (i.customId === "select") { page = parseInt(i.values[0]); }
pineafanc6158ab2022-06-17 16:34:07 +010094 }
pineafan63fc5e22022-08-04 22:04:10 +010095};
pineafan4f164f32022-02-26 22:07:12 +000096
pineafan4edb7762022-06-26 19:21:04 +010097
pineafanbd02b4a2022-08-05 22:01:38 +010098const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +010099 const member = (interaction.member as GuildMember);
pineafane23c4ec2022-07-27 21:56:27 +0100100 if (! member.permissions.has("MANAGE_ROLES")) throw "You do not have the *Manage Roles* permission";
pineafan63fc5e22022-08-04 22:04:10 +0100101 return true;
102};
pineafan4f164f32022-02-26 22:07:12 +0000103
pineafan8b4b17f2022-02-27 20:42:52 +0000104export { command, callback, check };