From b22aef42a0b4c28552d6e3069daf8a52c3e3c1e0 Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Thu, 16 Jun 2022 00:30:39 +0300 Subject: [PATCH] Dynamic command building for the q command --- discord-bot/commands/q.ts | 38 +++++++++++++++++++++++++++++++++++--- discord-bot/types.ts | 7 +++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/discord-bot/commands/q.ts b/discord-bot/commands/q.ts index cc399be8a..afec1560f 100644 --- a/discord-bot/commands/q.ts +++ b/discord-bot/commands/q.ts @@ -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", diff --git a/discord-bot/types.ts b/discord-bot/types.ts index 376034914..eb3ce6ddb 100644 --- a/discord-bot/types.ts +++ b/discord-bot/types.ts @@ -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;