Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added example for the sub commands #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions src/routes/hello-action.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/routes/hello-action.ts

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import {
APIInteractionResponse,
ApplicationCommandOptionType,
ApplicationCommandType,
buildInteractionResponse,
DiscordActionMetadata,
DiscordActionRequest,
DiscordActionResponse,
getCommandOptionValue,
getSubCommandOption,
InteractionResponseType,
InteractionType,
MessageFlags,
Expand All @@ -22,6 +24,34 @@ const router = express.Router();

async function handle(
interaction: DiscordActionRequest<APIChatInputApplicationCommandInteraction>
): Promise<DiscordActionResponse> {
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<DiscordActionResponse> {
return {
type: InteractionResponseType.ChannelMessageWithSource,
data: {
content: "Use /hello-action ping to test action.",
flags: MessageFlags.Ephemeral,
},
};
}

async function ping(
interaction: DiscordActionRequest<APIChatInputApplicationCommandInteraction>
): Promise<DiscordActionResponse> {
/**
* Get the value of `your-name` argument for `/hello-action`
Expand Down Expand Up @@ -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,
},
],
},
],
},
Expand Down
2 changes: 1 addition & 1 deletion src/routes/index.ts

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/routes/index.ts

Original file line number Diff line number Diff line change
Expand Up @@ -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 };