From 8943b5a06e741e38dc17e051148d74dcd424dc5b Mon Sep 17 00:00:00 2001 From: Jaron Pate Date: Sun, 5 Jan 2025 14:43:12 -0600 Subject: [PATCH] Fixed interaction payload/data type --- src/index.ts | 2 +- src/types/handler.ts | 10 ++++++---- testing/demo.test.ts | 19 +++++++++++-------- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index aba8e70..0bfe8f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,5 +14,5 @@ export { Message, type MessagePayload, ComponentStyle, ComponentType } from './t export { Embed } from './types/embed'; export { Emoji } from './types/emoji'; // Other -export { createCommandHandler } from './types/handler'; +export { createCommandHandler, type InteractionData, type InteractionCommpoentData, type InteractionCommandData, type InteractionHandlerFunction } from './types/handler'; export { ApplicationCommandType, ApplicationCommandOptionType as CommandOptionType } from './types/applicationCommand'; \ No newline at end of file diff --git a/src/types/handler.ts b/src/types/handler.ts index 964e29f..a943ad9 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -1,3 +1,4 @@ +import { exists } from "../utils"; import { ApplicationCommandContext, ApplicationCommandOptionType, type ApplicationCommandOption } from "./applicationCommand"; import { Trait, type EventPayload } from "./common"; import type { Context } from "./context"; @@ -160,10 +161,11 @@ export function createCommandHandler Promise | void; -export type EventHandlerFunction = (context: Context | null, payload: EventPayload) => Promise | void; +export type CommandHandlerFunction = (context: Context, args: any[]) => Promise | any; +export type EventHandlerFunction = (context: Context | null, payload: EventPayload) => Promise | any; +export type InteractionHandlerFunction

= (context: Context, payload: P) => Promise | any; export type HandlerWithoutContext = EventHandlerFunction; -export type HanderWithContext = CommandHandlerFunction | CommandHandler; +export type HanderWithContext = InteractionHandlerFunction | CommandHandlerFunction | CommandHandler; -export type Handler = EventHandlerFunction | CommandHandlerFunction | CommandHandler; +export type Handler = InteractionHandlerFunction | InteractionHandlerFunction | EventHandlerFunction | CommandHandlerFunction | CommandHandler; \ No newline at end of file diff --git a/testing/demo.test.ts b/testing/demo.test.ts index 29436d1..dac118b 100644 --- a/testing/demo.test.ts +++ b/testing/demo.test.ts @@ -1,5 +1,4 @@ -import { Client, ComponentStyle, type Context, createCommandHandler, Expectation, Intent, InteractionPayload, Message, type MessagePayload } from '../src/index'; -import { ApplicationCommandOptionType, ApplicationCommandType } from '../src/types/applicationCommand'; +import { ApplicationCommandType, Client, CommandOptionType, ComponentStyle, type Context, createCommandHandler, Expectation, Intent, InteractionCommpoentData, type InteractionData, Message, type MessagePayload } from '../src/index'; const client = new Client({ token: process.env.BOT_TOKEN!, @@ -33,21 +32,21 @@ client.applicationCommands.register('hello', ApplicationCommandType.Chat, create description: "Introduce yourself", args: [ { - type: ApplicationCommandOptionType.String, + type: CommandOptionType.String, name: "name", id: "name", description: "Your name", required: true }, { - type: ApplicationCommandOptionType.Integer, + type: CommandOptionType.Integer, name: "age", id: "age", description: "Your age", required: true }, { - type: ApplicationCommandOptionType.String, + type: CommandOptionType.String, name: "favorite_animal", id: "favorite_animal", description: "Your favorite animal", @@ -186,7 +185,7 @@ client.chatCommands.register('demo', async (context: Context, _args: any[]) => { context.reply(msg); }); -const buttonInteractionHandler = (context: Context, data: InteractionData) => { +const buttonInteractionHandler = (context: Context, data: InteractionCommpoentData) => { context.reply(`You clicked the ${data.custom_id} button!`); }; @@ -197,8 +196,12 @@ client.interactions.register('button_4', buttonInteractionHandler); client.interactions.register('button_5', buttonInteractionHandler); client.interactions.register('button_6', buttonInteractionHandler); -client.interactions.register('select_1', async (context: Context, data: InteractionData) => { - context.reply(`You selected ${data.values[0]}!`); +client.interactions.register('select_1', async (context: Context, data: InteractionCommpoentData) => { + if (data?.values !== undefined) { + context.reply(`You selected ${data.values[0]}!`); + } else { + context.reply('You selected nothing!'); + } }); client.connect(); \ No newline at end of file