blob: 1120f72b4473f7f0c0fe8bace7b52de5e8a9cbb9 [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;
59 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +010060 m = await interaction.editReply({
61 embeds: [
62 new EmojiEmbed()
63 .setEmoji("MEMBER.JOIN")
64 .setTitle("Viewing as " + member.displayName)
65 .setStatus("Success")
66 .setDescription(
Skyler Grey11236ba2022-08-08 21:13:33 +010067 `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` +
Skyler Grey75ea9172022-08-06 10:22:23 +010068 "\n" +
69 channels[page]
70 .map((c) => {
71 let channelType = c.type;
Skyler Grey11236ba2022-08-08 21:13:33 +010072 if (interaction.guild.rulesChannelId === c.id) channelType = "RULES";
73 else if ("nsfw" in c && c.nsfw) channelType += "_NSFW";
74 return c.permissionsFor(member).has("VIEW_CHANNEL")
75 ? `${getEmojiByName("ICONS.CHANNEL." + channelType)} ${c.name}\n` +
Skyler Grey75ea9172022-08-06 10:22:23 +010076 (() => {
Skyler Grey11236ba2022-08-08 21:13:33 +010077 if ("threads" in c && c.threads.cache.size > 0) {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 return (
79 c.threads.cache
80 .map(
81 (t) =>
82 ` ${
Skyler Grey11236ba2022-08-08 21:13:33 +010083 getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") +
Skyler Grey75ea9172022-08-06 10:22:23 +010084 " " +
Skyler Grey11236ba2022-08-08 21:13:33 +010085 getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL")
86 } ${t.name}`
Skyler Grey75ea9172022-08-06 10:22:23 +010087 )
88 .join("\n") + "\n"
89 );
90 }
91 return "";
92 })()
93 : "";
94 })
95 .join("") +
96 "\n" +
97 pageIndicator(channels.length, page)
98 )
99 ],
100 components: [
101 new MessageActionRow().addComponents([
102 new MessageSelectMenu()
103 .setOptions(
104 channels.map((c, index) => ({
Skyler Grey11236ba2022-08-08 21:13:33 +0100105 label: c[0].parent ? c[0].parent.name : "Uncategorised",
Skyler Grey75ea9172022-08-06 10:22:23 +0100106 value: index.toString(),
107 default: page === index
108 }))
109 )
110 .setCustomId("select")
111 .setMaxValues(1)
112 .setMinValues(1)
113 .setPlaceholder("Select a category")
114 ]),
115 new MessageActionRow().addComponents([
116 new MessageButton()
117 .setLabel(
118 page === 0
119 ? ""
120 : channels[page - 1][0].parent
121 ? channels[page - 1][0].parent.name
122 : "Uncategorised"
123 )
124 .setDisabled(page === 0)
125 .setEmoji(getEmojiByName("CONTROL.LEFT", "id"))
126 .setStyle("PRIMARY")
127 .setCustomId("previous"),
128 new MessageButton()
129 .setLabel(
130 page === channels.length - 1
131 ? ""
132 : channels[page + 1][0].parent
133 ? channels[page + 1][0].parent.name
134 : "Uncategorised"
135 )
136 .setDisabled(page === channels.length - 1)
137 .setEmoji(getEmojiByName("CONTROL.RIGHT", "id"))
138 .setStyle("PRIMARY")
139 .setCustomId("next")
140 ])
141 ]
142 });
pineafan4edb7762022-06-26 19:21:04 +0100143 let i;
144 try {
145 i = await m.awaitMessageComponent({ time: 300000 });
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 } catch (e) {
147 return;
148 }
pineafan63fc5e22022-08-04 22:04:10 +0100149 i.deferUpdate();
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 if (i.customId === "next") {
151 page++;
152 } else if (i.customId === "previous") {
153 page--;
154 } else if (i.customId === "select") {
155 page = parseInt(i.values[0]);
156 }
pineafanc6158ab2022-06-17 16:34:07 +0100157 }
pineafan63fc5e22022-08-04 22:04:10 +0100158};
pineafan4f164f32022-02-26 22:07:12 +0000159
pineafanbd02b4a2022-08-05 22:01:38 +0100160const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100161 const member = interaction.member as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100162 if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission");
pineafan63fc5e22022-08-04 22:04:10 +0100163 return true;
164};
pineafan4f164f32022-02-26 22:07:12 +0000165
Skyler Grey75ea9172022-08-06 10:22:23 +0100166export { command, callback, check };