Handling Application Commands
Creating a simple command
Lets create a simple ping
command to demonstrate how it works.
import { createClient, Intents } from "lilybird";import { handler } from "@lilybird/handlers/advanced";
handler.cachePath = `${import.meta.dir}/lily-cache/handler`;await handler.scanDir(`${import.meta.dir}/commands`);
await createClient({ token: process.env.TOKEN, intents: [Intents.GUILDS], listeners: {/* your listeners */} setup: async (client) => { await handler.loadGlobalCommands(client); }, listeners: handler.getListenersObject()})
1import { $applicationCommand } from "@lilybird/handlers/advanced";2
3$applicationCommand({4 name: "ping",5 description: "pong",6 handle: async (client, interaction) => {7 const { ws, rest } = await client.ping();8 await client.rest.createInteractionResponse(interaction.id, interaction.token, {9 type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,10 data: {11 content: `π WebSocket: \`${ws}ms\` | Rest: \`${rest}ms\``12 }13 });14 },15});
Handling Sub Commands
Handling sub commands with lilybirdβs handler is extremely simple, you write it just like you would for a normal command.
1import { $applicationCommand } from "@lilybird/handlers/advanced";2import { ApplicationCommandOptionType } from "lilybird";3
4$applicationCommand({5 name: "wrapper",6 description: "A wrapper group for basic commands",7 options: [8 {9 type: ApplicationCommandOptionType.SUB_COMMAND,10 name: "ping",11 description: "pong",12 handle: async (client, interaction) => {13 const { ws, rest } = await client.ping();14 await client.rest.createInteractionResponse(interaction.id, interaction.token, {15 type: InteractionCallbackType.CHANNEL_MESSAGE_WITH_SOURCE,16 data: {17 content: `π WebSocket: \`${ws}ms\` | Rest: \`${rest}ms\``18 }19 });20 },21 }22 ]23});