blob: 705c04ae040651adedf59e3a265613462fb9df71 [file] [log] [blame]
pineafanc6158ab2022-06-17 16:34:07 +01001import Discord, { CategoryChannel, CommandInteraction } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafanc6158ab2022-06-17 16:34:07 +01003import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00004import { WrappedCheck } from "jshaiku";
pineafanc6158ab2022-06-17 16:34:07 +01005import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +00006
7const command = (builder: SlashCommandSubcommandBuilder) =>
8 builder
9 .setName("viewas")
10 .setDescription("View the server as a specific member")
pineafan377794f2022-04-18 19:01:01 +010011 .addUserOption(option => option.setName("member").setDescription("The member to view as").setRequired(true))
pineafan4f164f32022-02-26 22:07:12 +000012
pineafanc6158ab2022-06-17 16:34:07 +010013const callback = async (interaction: CommandInteraction) => {
14 let channels = []
15 interaction.guild.channels.cache.forEach(channel => {
16 if (!channel.parent && channel.type !== "GUILD_CATEGORY") channels.push(channel)
17 })
18 channels = [channels]
19 channels = channels.concat(interaction.guild.channels.cache
pineafan377794f2022-04-18 19:01:01 +010020 .filter(c => c.type === "GUILD_CATEGORY")
21 .map(c => (c as CategoryChannel).children.map(c => c))
pineafanc6158ab2022-06-17 16:34:07 +010022 )
23 let autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"]
24 channels = channels.map(c => c.sort((a, b) => {
25 if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.name.localeCompare(b.name)
26 if (autoSortBelow.includes(a.type)) return -1
27 if (autoSortBelow.includes(b.type)) return 1
28 return a.position - b.position
29 }))
30 let member = interaction.options.getMember("member") as Discord.GuildMember
31 await interaction.reply({embeds: [new generateEmojiEmbed()
32 .setEmoji("MEMBER.JOIN")
33 .setTitle("Viewing as " + member.displayName)
34 .setStatus("Success")
35 ], ephemeral: true})
36 let page = 0;
37 while (true) {
38 await interaction.editReply({embeds: [new generateEmojiEmbed()
39 .setEmoji("MEMBER.JOIN")
40 .setTitle("Viewing as " + member.displayName)
41 .setStatus("Success")
42 .setDescription(
43 `${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}` +
44 "Visible:\n" +
45 channels[page].map(c => {
46 console.log(c)
47 return (channels[page] as Discord.GuildChannel).permissionsFor(member).has("VIEW_CHANNEL") ?
48 `${getEmojiByName("ICONS.CHANNEL." + c.type)} ${c.name}\n` : ""
49 }).join("")
50 )
51 ]})
52 break
53 }
pineafan4f164f32022-02-26 22:07:12 +000054}
55
56const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
57 return true;
58}
59
pineafan8b4b17f2022-02-27 20:42:52 +000060export { command, callback, check };