blob: ba9bc1dc7a791a3c20773910ec8a8a377c913d08 [file] [log] [blame]
Skyler Grey75ea9172022-08-06 10:22:23 +01001import Discord, {
2 CategoryChannel,
3 CommandInteraction,
4 GuildMember,
5 MessageActionRow,
6 MessageButton,
7 MessageSelectMenu
8} from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00009import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan4edb7762022-06-26 19:21:04 +010010import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafanc6158ab2022-06-17 16:34:07 +010011import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4edb7762022-06-26 19:21:04 +010012import pageIndicator from "../../utils/createPageIndicator.js";
pineafan4f164f32022-02-26 22:07:12 +000013
14const command = (builder: SlashCommandSubcommandBuilder) =>
15 builder
pineafan63fc5e22022-08-04 22:04:10 +010016 .setName("viewas")
17 .setDescription("View the server as a specific member")
Skyler Grey11236ba2022-08-08 21:13:33 +010018 .addUserOption((option) => option.setName("member").setDescription("The member to view as").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000019
pineafanbd02b4a2022-08-05 22:01:38 +010020const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan4edb7762022-06-26 19:21:04 +010021 let channels = [];
22 let m;
Skyler Grey75ea9172022-08-06 10:22:23 +010023 interaction.guild.channels.cache.forEach((channel) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010024 if (!channel.parent && channel.type !== "GUILD_CATEGORY") channels.push(channel);
pineafan63fc5e22022-08-04 22:04:10 +010025 });
26 channels = [channels];
Skyler Grey75ea9172022-08-06 10:22:23 +010027 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))
pineafan63fc5e22022-08-04 22:04:10 +010031 );
32 const autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"];
Skyler Grey75ea9172022-08-06 10:22:23 +010033 channels = channels.map((c) =>
34 c.sort((a, b) => {
Skyler Grey11236ba2022-08-08 21:13:33 +010035 if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.position - b.position;
Skyler Grey75ea9172022-08-06 10:22:23 +010036 if (autoSortBelow.includes(a.type)) return 1;
37 if (autoSortBelow.includes(b.type)) return -1;
38 return a.position - b.position;
39 })
40 );
pineafan4edb7762022-06-26 19:21:04 +010041 // Sort all arrays by the position of the first channels parent position
42 channels = channels.sort((a, b) => {
pineafan63fc5e22022-08-04 22:04:10 +010043 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 Grey11236ba2022-08-08 21:13:33 +010047 const member = interaction.options.getMember("member") as Discord.GuildMember;
Skyler Grey75ea9172022-08-06 10:22:23 +010048 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 });
pineafanc6158ab2022-06-17 16:34:07 +010058 let page = 0;
Skyler Greyad002172022-08-16 18:48:26 +010059 let timedOut = false;
60 while (!timedOut) {
Skyler Grey75ea9172022-08-06 10:22:23 +010061 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 Grey11236ba2022-08-08 21:13:33 +010068 `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` +
Skyler Grey75ea9172022-08-06 10:22:23 +010069 "\n" +
70 channels[page]
71 .map((c) => {
72 let channelType = c.type;
Skyler Grey11236ba2022-08-08 21:13:33 +010073 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 Grey75ea9172022-08-06 10:22:23 +010077 (() => {
Skyler Grey11236ba2022-08-08 21:13:33 +010078 if ("threads" in c && c.threads.cache.size > 0) {
Skyler Grey75ea9172022-08-06 10:22:23 +010079 return (
80 c.threads.cache
81 .map(
82 (t) =>
83 ` ${
Skyler Grey11236ba2022-08-08 21:13:33 +010084 getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") +
Skyler Grey75ea9172022-08-06 10:22:23 +010085 " " +
Skyler Grey11236ba2022-08-08 21:13:33 +010086 getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL")
87 } ${t.name}`
Skyler Grey75ea9172022-08-06 10:22:23 +010088 )
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 Grey11236ba2022-08-08 21:13:33 +0100106 label: c[0].parent ? c[0].parent.name : "Uncategorised",
Skyler Grey75ea9172022-08-06 10:22:23 +0100107 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 });
pineafan4edb7762022-06-26 19:21:04 +0100144 let i;
145 try {
146 i = await m.awaitMessageComponent({ time: 300000 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 } catch (e) {
Skyler Greyad002172022-08-16 18:48:26 +0100148 timedOut = true;
149 continue;
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 }
pineafan63fc5e22022-08-04 22:04:10 +0100151 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100152 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 }
pineafanc6158ab2022-06-17 16:34:07 +0100159 }
pineafan63fc5e22022-08-04 22:04:10 +0100160};
pineafan4f164f32022-02-26 22:07:12 +0000161
pineafanbd02b4a2022-08-05 22:01:38 +0100162const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100163 const member = interaction.member as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100164 if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission");
pineafan63fc5e22022-08-04 22:04:10 +0100165 return true;
166};
pineafan4f164f32022-02-26 22:07:12 +0000167
Skyler Grey75ea9172022-08-06 10:22:23 +0100168export { command, callback, check };