pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 1 | import Discord, { CategoryChannel, CommandInteraction } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 3 | import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 4 | import { WrappedCheck } from "jshaiku"; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 5 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 6 | |
| 7 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 8 | builder |
| 9 | .setName("viewas") |
| 10 | .setDescription("View the server as a specific member") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 11 | .addUserOption(option => option.setName("member").setDescription("The member to view as").setRequired(true)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 12 | |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 13 | const 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 |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 20 | .filter(c => c.type === "GUILD_CATEGORY") |
| 21 | .map(c => (c as CategoryChannel).children.map(c => c)) |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 22 | ) |
| 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 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
| 57 | return true; |
| 58 | } |
| 59 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 60 | export { command, callback, check }; |