Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 1 | import Discord, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 2 | CommandInteraction, |
| 3 | GuildMember, |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 4 | ActionRowBuilder, |
| 5 | ButtonBuilder, |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame^] | 6 | ButtonStyle, |
| 7 | NonThreadGuildBasedChannel |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 8 | } from "discord.js"; |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 9 | import type { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame^] | 10 | import type { GuildBasedChannel } from "discord.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 11 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 12 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 13 | import pageIndicator from "../../utils/createPageIndicator.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 14 | |
| 15 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 16 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 17 | .setName("viewas") |
| 18 | .setDescription("View the server as a specific member") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 19 | .addUserOption((option) => option.setName("member").setDescription("The member to view as").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 20 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 21 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame^] | 22 | /* |
| 23 | * { |
| 24 | categoryObject: channel[], |
| 25 | categoryObject: channel[], |
| 26 | "null": channel[] |
| 27 | } |
| 28 | */ |
| 29 | |
| 30 | const channels: Record<string, GuildBasedChannel[]> = {"": [] as GuildBasedChannel[]}; |
| 31 | |
| 32 | interaction.guild!.channels.fetch().then(channelCollection => { |
| 33 | channelCollection.forEach(channel => { |
| 34 | if (!channel) return; // if no channel |
| 35 | if (channel.type === Discord.ChannelType.GuildCategory) { |
| 36 | if(!channels[channel!.id]) channels[channel!.id] = [channel]; |
| 37 | } else if (channel.parent) { |
| 38 | if (!channels[channel.parent.id]) channels[channel.parent.id] = [channel]; |
| 39 | else (channels[channel.parent.id as string])!.push(channel); |
| 40 | } else { |
| 41 | channels[""]!.push(channel); |
| 42 | } |
| 43 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | }); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame^] | 45 | |
| 46 | const member = interaction.options.getMember("member") as Discord.GuildMember; |
| 47 | const autoSortBelow = [Discord.ChannelType.GuildVoice, Discord.ChannelType.GuildStageVoice]; |
| 48 | // for each category, sort its channels. This should be based on the order of the channels, with voice and stage channels sorted below text |
| 49 | channels = Object.values(channels).map((c) => { |
| 50 | return c.sort((a: GuildBasedChannel, b: GuildBasedChannel) => { |
| 51 | if (a.type === Discord.ChannelType.PrivateThread || b.type === Discord.ChannelType.PrivateThread) |
| 52 | if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.position ?? 0 - b.position ; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | if (autoSortBelow.includes(a.type)) return 1; |
| 54 | if (autoSortBelow.includes(b.type)) return -1; |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame^] | 55 | return a - b; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | }); |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 57 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame^] | 58 | |
| 59 | |
| 60 | //OLD CODE START |
| 61 | // const unprocessedChannels: GuildBasedChannel[] = []; |
| 62 | // let m; |
| 63 | // interaction.guild!.channels.cache.forEach((channel) => { |
| 64 | // if (!channel.parent && channel.type !== Discord.ChannelType.GuildCategory) unprocessedChannels.push(channel); |
| 65 | // }); |
| 66 | // let channels: GuildBasedChannel[][] = [unprocessedChannels]; |
| 67 | // channels = channels.concat( |
| 68 | // interaction.guild!.channels.cache |
| 69 | // .filter((c) => c.type === Discord.ChannelType.GuildCategory) |
| 70 | // .map((c) => (c as CategoryChannel).children.map((c) => c)) |
| 71 | // ); |
| 72 | // const autoSortBelow = ["GUILD_VOICE", "GUILD_STAGE_VOICE"]; |
| 73 | // channels = channels.map((c) => |
| 74 | // c.sort((a, b) => { |
| 75 | // if (autoSortBelow.includes(a.type) && autoSortBelow.includes(b.type)) return a.position - b.position; |
| 76 | // if (autoSortBelow.includes(a.type)) return 1; |
| 77 | // if (autoSortBelow.includes(b.type)) return -1; |
| 78 | // return a.position - b.position; |
| 79 | // }) |
| 80 | // ); |
| 81 | // // Sort all arrays by the position of the first channels parent position |
| 82 | // channels = channels.sort((a, b) => { |
| 83 | // if (!a[0].parent) return -1; |
| 84 | // if (!b[0].parent) return 1; |
| 85 | // return a[0].parent.position - b[0].parent.position; |
| 86 | // }); |
| 87 | // const member = interaction.options.getMember("member") as Discord.GuildMember; |
| 88 | // m = await interaction.reply({ |
| 89 | // embeds: [ |
| 90 | // new EmojiEmbed() |
| 91 | // .setEmoji("MEMBER.JOIN") |
| 92 | // .setTitle("Viewing as " + member.displayName) |
| 93 | // .setStatus("Success") |
| 94 | // ], |
| 95 | // ephemeral: true, |
| 96 | // fetchReply: true |
| 97 | // }); |
| 98 | // let page = 0; |
| 99 | // let timedOut = false; |
| 100 | // while (!timedOut) { |
| 101 | // m = await interaction.editReply({ |
| 102 | // embeds: [ |
| 103 | // new EmojiEmbed() |
| 104 | // .setEmoji("MEMBER.JOIN") |
| 105 | // .setTitle("Viewing as " + member.displayName) |
| 106 | // .setStatus("Success") |
| 107 | // .setDescription( |
| 108 | // `**${channels[page][0].parent ? channels[page][0].parent.name : "Uncategorised"}**` + |
| 109 | // "\n" + |
| 110 | // channels[page] |
| 111 | // .map((c) => { |
| 112 | // let channelType = c.type; |
| 113 | // if (interaction.guild.rulesChannelId === c.id) channelType = "RULES"; |
| 114 | // else if ("nsfw" in c && c.nsfw) channelType += "_NSFW"; |
| 115 | // return c.permissionsFor(member).has("VIEW_CHANNEL") |
| 116 | // ? `${getEmojiByName("ICONS.CHANNEL." + channelType)} ${c.name}\n` + |
| 117 | // (() => { |
| 118 | // if ("threads" in c && c.threads.cache.size > 0) { |
| 119 | // return ( |
| 120 | // c.threads.cache |
| 121 | // .map( |
| 122 | // (t) => |
| 123 | // ` ${ |
| 124 | // getEmojiByName("ICONS.CHANNEL.THREAD_PIPE") + |
| 125 | // " " + |
| 126 | // getEmojiByName("ICONS.CHANNEL.THREAD_CHANNEL") |
| 127 | // } ${t.name}` |
| 128 | // ) |
| 129 | // .join("\n") + "\n" |
| 130 | // ); |
| 131 | // } |
| 132 | // return ""; |
| 133 | // })() |
| 134 | // : ""; |
| 135 | // }) |
| 136 | // .join("") + |
| 137 | // "\n" + |
| 138 | // pageIndicator(channels.length, page) |
| 139 | // ) |
| 140 | // ], |
| 141 | // components: [ |
| 142 | // new ActionRowBuilder().addComponents([ |
| 143 | // new SelectMenuBuilder() |
| 144 | // .setOptions( |
| 145 | // channels.map((c, index) => ({ |
| 146 | // label: c[0].parent ? c[0].parent.name : "Uncategorised", |
| 147 | // value: index.toString(), |
| 148 | // default: page === index |
| 149 | // })) |
| 150 | // ) |
| 151 | // .setCustomId("select") |
| 152 | // .setMaxValues(1) |
| 153 | // .setMinValues(1) |
| 154 | // .setPlaceholder("Select a category") |
| 155 | // ]), |
| 156 | // new ActionRowBuilder().addComponents([ |
| 157 | // new ButtonBuilder() |
| 158 | // .setLabel( |
| 159 | // page === 0 |
| 160 | // ? "" |
| 161 | // : channels[page - 1][0].parent |
| 162 | // ? channels[page - 1][0].parent.name |
| 163 | // : "Uncategorised" |
| 164 | // ) |
| 165 | // .setDisabled(page === 0) |
| 166 | // .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 167 | // .setStyle(ButtonStyle.Primary) |
| 168 | // .setCustomId("previous"), |
| 169 | // new ButtonBuilder() |
| 170 | // .setLabel( |
| 171 | // page === channels.length - 1 |
| 172 | // ? "" |
| 173 | // : channels[page + 1][0].parent |
| 174 | // ? channels[page + 1][0].parent.name |
| 175 | // : "Uncategorised" |
| 176 | // ) |
| 177 | // .setDisabled(page === channels.length - 1) |
| 178 | // .setEmoji(getEmojiByName("CONTROL.RIGHT", "id")) |
| 179 | // .setStyle(ButtonStyle.Primary) |
| 180 | // .setCustomId("next") |
| 181 | // ]) |
| 182 | // ] |
| 183 | // }); |
| 184 | // let i; |
| 185 | // try { |
| 186 | // i = await m.awaitMessageComponent({ time: 300000 }); |
| 187 | // } catch (e) { |
| 188 | // timedOut = true; |
| 189 | // continue; |
| 190 | // } |
| 191 | // i.deferUpdate(); |
| 192 | // if (i.customId === "next") { |
| 193 | // page++; |
| 194 | // } else if (i.customId === "previous") { |
| 195 | // page--; |
| 196 | // } else if (i.customId === "select") { |
| 197 | // page = parseInt(i.values[0]); |
| 198 | // } |
| 199 | // } |
| 200 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 201 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 202 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 203 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 204 | const member = interaction.member as GuildMember; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 205 | if (!member.permissions.has("MANAGE_ROLES")) throw new Error("You do not have the *Manage Roles* permission"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 206 | return true; |
| 207 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 208 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 209 | export { command, callback, check }; |