From 1b2c27bd05ba4e0b028da15b63180b172cc8d815 Mon Sep 17 00:00:00 2001 From: Hitesh Gupta Date: Thu, 16 Feb 2023 19:02:48 +0530 Subject: [PATCH] feat: added example for the sub commands Signed-off-by: Hitesh Gupta --- src/routes/hello-action.ts | 50 +++++++++++++++++++++++++++++++++++--- src/routes/index.ts | 2 +- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/routes/hello-action.ts b/src/routes/hello-action.ts index e396251..0134172 100644 --- a/src/routes/hello-action.ts +++ b/src/routes/hello-action.ts @@ -6,10 +6,12 @@ import { APIInteractionResponse, ApplicationCommandOptionType, ApplicationCommandType, + buildInteractionResponse, DiscordActionMetadata, DiscordActionRequest, DiscordActionResponse, getCommandOptionValue, + getSubCommandOption, InteractionResponseType, InteractionType, MessageFlags, @@ -22,6 +24,34 @@ const router = express.Router(); async function handle( interaction: DiscordActionRequest +): Promise { + const subCommand = getSubCommandOption(interaction)!; + + switch (subCommand.name) { + case "help": { + return help(); + } + case "ping": { + return ping(interaction); + } + default: { + return buildInteractionResponse(`Command not found.`); + } + } +} + +async function help(): Promise { + return { + type: InteractionResponseType.ChannelMessageWithSource, + data: { + content: "Use /hello-action ping to test action.", + flags: MessageFlags.Ephemeral, + }, + }; +} + +async function ping( + interaction: DiscordActionRequest ): Promise { /** * Get the value of `your-name` argument for `/hello-action` @@ -125,10 +155,22 @@ router.get("/metadata", function (req, res) { description: "/hello-action", options: [ { - name: "your-name", - description: "Name of person we're greeting", - type: ApplicationCommandOptionType.String, - required: true, + name: "help", + description: "Know about command.", + type: 1, + }, + { + name: "ping", + description: "To check the command.", + type: 1, + options: [ + { + name: "your-name", + description: "Name of person we're greeting", + type: ApplicationCommandOptionType.String, + required: true, + }, + ], }, ], }, diff --git a/src/routes/index.ts b/src/routes/index.ts index 0f5d355..8dfad5d 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -2,4 +2,4 @@ import helloAction from "./hello-action"; import buttonAction from "./button-action"; import popupAction from "./popup-action"; -export default {helloAction, buttonAction, popupAction}; +export default { helloAction, buttonAction, popupAction };