From d7b5573598cb3372029f0b44c8ffe4db59f9c387 Mon Sep 17 00:00:00 2001 From: GRZ4NA Date: Sat, 11 Sep 2021 20:03:38 +0200 Subject: [PATCH] added message with select menu... accessor for children and append method --- src/commands/NestedCommand.ts | 51 +++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/commands/NestedCommand.ts b/src/commands/NestedCommand.ts index 731a01e..3177e45 100644 --- a/src/commands/NestedCommand.ts +++ b/src/commands/NestedCommand.ts @@ -1,10 +1,12 @@ -import { Message } from "discord.js"; +import { Message, MessageActionRow, MessageEmbed, MessageSelectMenu } from "discord.js"; import { BaseCommand } from "./BaseCommand.js"; +import { SubCommand } from "./SubCommand.js"; +import { SubCommandGroup } from "./SubCommandGroup.js"; import { CommandRegExps } from "./types/commands.js"; import { NestedCommandInit } from "./types/NestedCommand.js"; export class NestedCommand extends BaseCommand { - private readonly _children: any[] = []; + private readonly _children: (SubCommand | SubCommandGroup)[] = []; constructor(o: NestedCommandInit) { super("CHAT", { @@ -13,6 +15,37 @@ export class NestedCommand extends BaseCommand { announceSuccess: false, function: (i) => { if (i instanceof Message) { + const msg = new MessageEmbed({ + title: `${this.name}`, + description: "This is a nested command. Please select one of the following subcommands", + timestamp: Date.now(), + hexColor: "#ff5500", + }); + const list = new MessageSelectMenu(); + list.setPlaceholder("Select a subcommand"); + list.customId = `${i.author.id}/${i.channel.id}`; + const options = this._children + .map((c) => { + if (c instanceof SubCommand) { + return { label: c.name, description: c.description, value: c.name }; + } else { + const nested = c.children.map((ch) => { + return { + label: ch.name, + description: ch.description, + value: c.name, + }; + }); + return nested; + } + }) + .flat(1); + list.addOptions(...options); + const commandBar = new MessageActionRow({ components: [list] }); + return { + embeds: [msg], + components: [commandBar], + }; } else { throw new Error("Illegal command call"); } @@ -22,4 +55,18 @@ export class NestedCommand extends BaseCommand { throw new Error(`"${o.name}" is not a valid command name (regexp: ${CommandRegExps.chatName})`); } } + + get children() { + return Object.freeze([...this._children]); + } + + public append(sc: SubCommand | SubCommandGroup): SubCommand | SubCommandGroup { + if (this._children.find((c) => c.name === sc.name)) { + throw new Error(`The name "${sc.name}" is already registered in "${this.name}"`); + } else { + sc.parent = this; + this._children.push(sc); + return sc; + } + } }