Dynamic command building for the q command

This commit is contained in:
Kalle
2022-06-16 00:30:39 +03:00
parent 44072bce7f
commit b22aef42a0
2 changed files with 40 additions and 5 deletions

View File

@@ -91,13 +91,45 @@ const COMMAND_NAME = "q";
export const questionCommand: BotCommand = {
name: COMMAND_NAME,
builder: new SlashCommandBuilder()
.setName(COMMAND_NAME)
.setDescription("wohoo"),
builder: addSubcommands(
new SlashCommandBuilder()
.setName(COMMAND_NAME)
.setDescription("Find a link")
),
execute: async ({ interaction }) => {
return interaction.reply("Pong!");
},
};
function addSubcommands(builder: SlashCommandBuilder) {
for (const [key, value] of Object.entries(groupedLinks)) {
builder.addSubcommand((subcommand) => {
const result = subcommand
.setName(key)
.setDescription(`TODO: explanation for ${key} command`);
if (typeof value !== "string") {
result.addStringOption((option) =>
option
.setName("name")
.setDescription("TODO: subname desc")
.setRequired(true)
.addChoices(
...Object.keys(value).map((key) => ({
name: key.toUpperCase(),
value: key,
}))
)
);
}
return result;
});
}
return builder;
}
// const questionCommand: BotCommand = {
// name: "add",
// description: "Gives you the specified role",

View File

@@ -1,4 +1,7 @@
import type { SlashCommandBuilder } from "@discordjs/builders";
import type {
SlashCommandBuilder,
SlashCommandSubcommandsOnlyBuilder,
} from "@discordjs/builders";
import type { CommandInteraction } from "discord.js";
export interface BotCommand {
@@ -6,7 +9,7 @@ export interface BotCommand {
// description: string;
// /** ID's of guilds where this command will be set */
// guilds: string[];
builder: SlashCommandBuilder;
builder: SlashCommandBuilder | SlashCommandSubcommandsOnlyBuilder;
execute: (commandParameters: {
interaction: CommandInteraction;
}) => Promise<void>;