blob: f0b2047643d1618f59f2b02e91778be6de35cefb [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 Grey75ea9172022-08-06 10:22:23 +010018 .addUserOption((option) =>
19 option
20 .setName("member")
21 .setDescription("The member to view as")
22 .setRequired(true)
23 );
pineafan4f164f32022-02-26 22:07:12 +000024
pineafanbd02b4a2022-08-05 22:01:38 +010025const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan4edb7762022-06-26 19:21:04 +010026 let channels = [];
27 let m;
Skyler Grey75ea9172022-08-06 10:22:23 +010028 interaction.guild.channels.cache.forEach((channel) => {
29 if (!channel.parent && channel.type !== "GUILD_CATEGORY")
30 channels.push(channel);
pineafan63fc5e22022-08-04 22:04:10 +010031 });
32 channels = [channels];
Skyler Grey75ea9172022-08-06 10:22:23 +010033 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))
pineafan63fc5e22022-08-04 22:04:10 +010037 );
38 const autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"];
Skyler Grey75ea9172022-08-06 10:22:23 +010039 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 );
pineafan4edb7762022-06-26 19:21:04 +010051 // Sort all arrays by the position of the first channels parent position
52 channels = channels.sort((a, b) => {
pineafan63fc5e22022-08-04 22:04:10 +010053 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 Grey75ea9172022-08-06 10:22:23 +010057 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 });
pineafanc6158ab2022-06-17 16:34:07 +010070 let page = 0;
71 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +010072 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 });
pineafan4edb7762022-06-26 19:21:04 +0100179 let i;
180 try {
181 i = await m.awaitMessageComponent({ time: 300000 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100182 } catch (e) {
183 return;
184 }
pineafan63fc5e22022-08-04 22:04:10 +0100185 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100186 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 }
pineafanc6158ab2022-06-17 16:34:07 +0100193 }
pineafan63fc5e22022-08-04 22:04:10 +0100194};
pineafan4f164f32022-02-26 22:07:12 +0000195
pineafanbd02b4a2022-08-05 22:01:38 +0100196const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100197 const member = interaction.member as GuildMember;
198 if (!member.permissions.has("MANAGE_ROLES"))
199 throw "You do not have the *Manage Roles* permission";
pineafan63fc5e22022-08-04 22:04:10 +0100200 return true;
201};
pineafan4f164f32022-02-26 22:07:12 +0000202
Skyler Grey75ea9172022-08-06 10:22:23 +0100203export { command, callback, check };