hide unavailable commands and add subcommands (fix #47)
All checks were successful
PR Check / lint-and-format (pull_request) Successful in 27s

This commit is contained in:
Mylloon 2024-11-01 20:29:23 +01:00
parent 750c6b4447
commit 97566bc0c5
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 31 additions and 8 deletions

View file

@ -1,5 +1,11 @@
import { SlashCommandBuilder } from "@discordjs/builders";
import { ChatInputCommandInteraction, Client, Colors, EmbedBuilder } from "discord.js";
import {
ApplicationCommandOptionType,
ChatInputCommandInteraction,
Client,
Colors,
EmbedBuilder,
} from "discord.js";
import "../../modules/string";
import { getLocale, getLocalizations } from "../../utils/locales";
import { getFilename } from "../../utils/misc";
@ -47,16 +53,31 @@ export default {
}[] = [];
// Load all the command per categories
// TODO: Check if the command exist in the context (guild)
// TODO: List subcommands too
// https://git.mylloon.fr/ConfrerieDuKassoulait/Botanique/issues/47
client.commands.categories.forEach((commands_name, category) => {
const commands = commands_name.reduce((data, command_name) => {
return data + `\`/${command_name}\`, `;
}, "");
// Check if the command exist in the context (guild)
commands_name = commands_name.filter((command) => {
const scope = client.commands.list.get(command)?.scope();
return scope!.length === 0 || scope?.find((v) => v === interaction.guildId) !== undefined;
});
// Add subcommands
const all_commands: string[] = [];
commands_name.forEach((command) => {
const json = client.commands.list.get(command)?.data.toJSON();
all_commands.push(command);
json?.options
?.filter((option) => option.type === ApplicationCommandOptionType.Subcommand)
.forEach((subcommand) => all_commands.push(command + " " + subcommand.name));
});
const commands = all_commands.reduce(
(data, command_name) => data + `\`/${command_name}\`, `,
"",
);
fields.push({
name: category.capitalize() + ` (${commands_name.length})`,
name: category.capitalize() + ` (${all_commands.length})`,
value: commands.slice(0, -2),
});
});

View file

@ -75,6 +75,8 @@ declare module "discord.js" {
string,
/** Command itself */
{
/** Guilds where the command is active */
scope: () => string[];
/** Data about the command */
data: SlashCommandBuilder;
/** How the command interact */