WE ARE COOKING (slash commands with some arg validation proto working)

This commit is contained in:
2024-11-16 17:07:31 -06:00
parent 7848ea0082
commit fbdff25c14
9 changed files with 302 additions and 102 deletions
+2 -2
View File
@@ -3,8 +3,8 @@
"module": "src/index.ts", "module": "src/index.ts",
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "dotenvx run -f .env -- bun run testing/demo.test.ts", "test": "bun run testing/demo.test.ts",
"test:inspect": "dotenvx run -f .env -- bun --inspect testing/demo.test.ts" "test:inspect": "bun --inspect testing/demo.test.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/bun": "latest" "@types/bun": "latest"
+21 -12
View File
@@ -3,12 +3,12 @@ import { Gateway } from './connectors/gateway';
import { Processor } from './processor'; import { Processor } from './processor';
import { Expectation, fromDiscord, Intent, type ClientConfig } from '../types/common'; import { Expectation, fromDiscord, Intent, type ClientConfig } from '../types/common';
import { exists, isNil } from '../utils/index'; import { exists, isNil } from '../utils/index';
import type { Handler } from '../types/handler';
import { type DiscordMessage, Message } from '../types/message'; import { type DiscordMessage, Message } from '../types/message';
import { Guilds } from './caches/guilds'; import { Guilds } from './caches/guilds';
import { Messages } from './caches/messages'; import { Messages } from './caches/messages';
import { Users } from './caches/users'; import { Users } from './caches/users';
import { hydrate, hydrator, type Hydrateable, type Hydrated } from './hydrator'; import { hydrate, hydrator, type Hydrateable, type Hydrated } from './hydrator';
import type { User } from '../types/user';
export class Client { export class Client {
readonly token: string; readonly token: string;
@@ -21,7 +21,9 @@ export class Client {
protected gateway: Gateway; protected gateway: Gateway;
guilds: Guilds; guilds: Guilds;
users: Users; users: Users;
messages: Messages messages: Messages;
chatCommands: Processor['chat_commands'];
applicationCommands: Processor['application_commands'];
constructor(config: ClientConfig) { constructor(config: ClientConfig) {
// Validate the configuration // Validate the configuration
@@ -38,6 +40,9 @@ export class Client {
this.guilds = new Guilds(this.api, this.processor); this.guilds = new Guilds(this.api, this.processor);
this.users = new Users(this.api, this.processor); this.users = new Users(this.api, this.processor);
this.messages = new Messages(this, this.api, this.processor); this.messages = new Messages(this, this.api, this.processor);
// Save references to the chat and application command handlers
this.chatCommands = this.processor.chat_commands;
this.applicationCommands = this.processor.application_commands;
} }
private static validateConfig(config: ClientConfig) { private static validateConfig(config: ClientConfig) {
@@ -66,6 +71,10 @@ export class Client {
} }
} }
get self() {
return this.gateway.user;
}
/** /**
* Hydrates a given data object based on the provided expectations. * Hydrates a given data object based on the provided expectations.
* *
@@ -106,17 +115,17 @@ export class Client {
return Message[fromDiscord](await this.api.post<DiscordMessage>(`/channels/${channel_id}/messages`, message.toJSON())); return Message[fromDiscord](await this.api.post<DiscordMessage>(`/channels/${channel_id}/messages`, message.toJSON()));
} }
chatCommands = { // chatCommands = {
register: (name: string | string[], handler: Handler) => this.processor.chat_commands.register(name, handler), // register: (name: string | string[], handler: Handler) => this.processor.chat_commands.register(name, handler),
unregister: (name: string | string[]) => this.processor.chat_commands.unregister(name), // unregister: (name: string | string[]) => this.processor.chat_commands.unregister(name),
has: (name: string) => this.processor.chat_commands.has(name) // has: (name: string) => this.processor.chat_commands.has(name)
} // }
slashCommands = { // applicationCommands = {
register: (name: string, handler: Handler) => this.processor.slash_commands.register(name, handler), // register: (name: string, type: ApplicationCommandType, handler: Handler) => this.processor.application_commands.register(name, type, handler),
unregister: (name: string) => this.processor.slash_commands.unregister(name), // unregister: (name: string) => this.processor.application_commands.unregister(name),
has: (name: string) => this.processor.slash_commands.has(name) // has: (name: string) => this.processor.application_commands.has(name)
} // }
connect = () => this.gateway.connect(); connect = () => this.gateway.connect();
} }
+63 -7
View File
@@ -1,12 +1,14 @@
import { WebSocket, type Data } from 'ws'; import { WebSocket, type Data } from 'ws';
import type { Client } from "../client"; import type { Client } from "../client";
import { fromDiscord, type Payload } from "../../types/common"; import { fromDiscord, type InteractionPayload, type Payload } from "../../types/common";
import { exists, isNil, isObject, log } from "../../utils"; import { exists, isNil, isObject, log } from "../../utils";
import type { API } from "../api"; import type { API } from "../api";
import type { Processor } from '../processor'; import type { Processor } from '../processor';
import { Context } from '../../types/context'; import { Context } from '../../types/context';
import { type DiscordMessage, Message } from '../../types/message'; import { type DiscordMessage, Message } from '../../types/message';
import { DiscordUser, User } from '../../types/user'; import { DiscordUser, User } from '../../types/user';
import { Guild } from '../../types/guild';
import { Channel } from '../../types/channel';
export class Gateway { export class Gateway {
private socket: WebSocket | null = null; private socket: WebSocket | null = null;
@@ -14,7 +16,7 @@ export class Gateway {
private heartbeat_timeout: Timer | null = null; private heartbeat_timeout: Timer | null = null;
private sequence: number | null = null; private sequence: number | null = null;
// Internal state // Internal state
private user: any; user: User | null = null;
constructor(private client: Client, private processor: Processor, private api: API) {} constructor(private client: Client, private processor: Processor, private api: API) {}
@@ -126,10 +128,9 @@ export class Gateway {
} }
if (event_name === 'READY') { if (event_name === 'READY') {
this.user = event_data.user; this.user = User[fromDiscord](DiscordUser.fromAPIResponse(event_data.user));
log('User:', this.user);
} else if (event_name === 'INTERACTION_CREATE') { } else if (event_name === 'INTERACTION_CREATE') {
// this.handleInteractionCreate(payload); this.handleInteractionCreate(payload);
} else if (event_name === 'MESSAGE_CREATE') { } else if (event_name === 'MESSAGE_CREATE') {
await this.handleMessageCreate(payload as Payload); await this.handleMessageCreate(payload as Payload);
} else if (event_name === 'MESSAGE_UPDATE') { } else if (event_name === 'MESSAGE_UPDATE') {
@@ -143,7 +144,7 @@ export class Gateway {
// If the event is a message event, create a message object // If the event is a message event, create a message object
const message = Message[fromDiscord](event_data as DiscordMessage); const message = Message[fromDiscord](event_data as DiscordMessage);
// And then generate the context // And then generate the context
context = new Context(this.client, this.api, message); context = new Context(this.client, this.api, { message });
} }
// Call event handlers // Call event handlers
@@ -151,6 +152,10 @@ export class Gateway {
} }
private handleMessageCreate = async (payload: Payload) => { private handleMessageCreate = async (payload: Payload) => {
if (isNil(this.user)) {
throw new Error('Cannot handle MESSAGE_CREATE event without a user');
}
if (!isObject(payload.d)) { if (!isObject(payload.d)) {
throw new Error('Invalid MESSAGE_CREATE payload received'); throw new Error('Invalid MESSAGE_CREATE payload received');
} }
@@ -188,7 +193,7 @@ export class Gateway {
// Create a new message object from the payload // Create a new message object from the payload
const message = Message[fromDiscord](payload.d as DiscordMessage); const message = Message[fromDiscord](payload.d as DiscordMessage);
// Generate context // Generate context
const context = new Context(this.client, this.api, message); const context = new Context(this.client, this.api, { message });
// Check if command exists // Check if command exists
if (this.processor.chat_commands.has(command)) { if (this.processor.chat_commands.has(command)) {
// Execute the handler // Execute the handler
@@ -200,6 +205,57 @@ export class Gateway {
} }
}; };
private handleInteractionCreate = (payload: Payload) => {
if (isNil(payload.d)) {
throw new Error('Invalid INTERACTION_CREATE payload received');
}
if (!isObject(payload.d)) {
throw new Error('Invalid INTERACTION_CREATE data received');
}
if (typeof payload.d.type !== 'number') {
throw new Error('Invalid INTERACTION_CREATE type received');
}
if (payload.d.type === 2) {
this.handleShashInteraction(payload as Payload & { d: InteractionPayload });
} else {
// this.handleChatInteraction(payload);
}
};
private handleShashInteraction = (payload: Payload & { d: InteractionPayload }) => {
console.log('handleShashInteraction')
const command = payload.d.data.name;
const args = payload.d.data.options;
const context = new Context(this.client, this.api, { interaction: payload.d });
if (payload.d.data.guild) {
const guild = Guild[fromDiscord](payload.d.data.guild);
this.client.guilds.set(guild.id, guild);
}
if (payload.d.data.member) {
const member = User[fromDiscord](payload.d.data.member.user);
this.client.users.set(member.id, member);
}
// TODO: Fix when channel class is implemented
// if (payload.d.data.channel) {
// const channel = Channel[fromDiscord](payload.d.data.channel);
// this.client.channels.set(channel.id, channel);
// }
if (this.processor.application_commands.has(command)) {
this.processor.application_commands.execute(command, context, args);
} else {
// TODO: Custom unknown command handler
context.reply(`Unknown command: ${command}`);
}
};
async connect() { async connect() {
// Fetch the Discord gateway URL // Fetch the Discord gateway URL
const response = await this.api.request('GET', '/gateway/bot'); const response = await this.api.request('GET', '/gateway/bot');
+71 -17
View File
@@ -1,15 +1,15 @@
import type { Client } from "./client"; import type { Client } from "./client";
import type { Context } from "../types/context"; import type { Context } from "../types/context";
import { HandlerType, Trait, type EventPayload } from "../types/common"; import { HandlerType, Trait, type EventPayload } from "../types/common";
import { type CommandHandler, type Handler } from "../types/handler"; import { CommandHandler, type Handler, type ArgsFromOptions, OptionTypeMap } from "../types/handler";
import type { API } from "./api"; import type { API } from "./api";
import type { ApplicationCommandOption } from "../types/applicationCommand"; import { ApplicationCommandType, type ApplicationCommandOption, type ApplicationCommandOptionResult } from "../types/applicationCommand";
export class Processor { export class Processor {
private handlers = { private handlers = {
[HandlerType.Events]: new Map<string, Handler[]>(), [HandlerType.Events]: new Map<string, Handler[]>(),
[HandlerType.ChatCommands]: new Map<string, Handler[]>(), [HandlerType.ChatCommands]: new Map<string, Handler[]>(),
[HandlerType.SlashCommands]: new Map<string, Handler[]>(), [HandlerType.ApplicationCommands]: new Map<string, Handler[]>(),
[HandlerType.Interactions]: new Map<string, Handler[]>() [HandlerType.Interactions]: new Map<string, Handler[]>()
}; };
@@ -56,12 +56,13 @@ export class Processor {
if (handlers) { if (handlers) {
for (const handler of handlers) { for (const handler of handlers) {
if (isCommandHandler(handler)) { if (Processor.isCommandHandler(handler)) {
if (type === HandlerType.Events) { // TODO: Enforce this?
throw new Error('Event handlers cannot be a CommandHandler'); // if (type === HandlerType.Events) {
} // throw new Error('Event handlers cannot be a CommandHandler');
// }
await Promise.resolve(handler[Trait.execute](context!, argsOrPayload)); const resolvedArgs = await this.validateAndResolveArgs(argsOrPayload, handler.args);
await Promise.resolve(handler[Trait.execute](context!, resolvedArgs));
} else { } else {
// TODO: Fix ts error // TODO: Fix ts error
await Promise.resolve(handler(context, argsOrPayload)); await Promise.resolve(handler(context, argsOrPayload));
@@ -70,6 +71,33 @@ export class Processor {
} }
} }
private validateAndResolveArgs = <T extends readonly ApplicationCommandOption[]>(args: ApplicationCommandOptionResult[], definition: T): Promise<ArgsFromOptions<T>> => {
// Zip the args and definition together
const zipped: [ApplicationCommandOption, ApplicationCommandOptionResult][] = args.map((arg, i) => [definition[i], arg]);
// Validate the args
const validated: Record<string, any> = {};
for (const [def, arg] of zipped) {
if (def.choices) {
// If the definition has choices, validate the arg against them
const choice = def.choices.find((choice: any) => choice.value === arg.value);
if (!choice) {
throw new Error(`Invalid choice for ${def.name}: expected one of ${def.choices.map((c: any) => c.value).join(', ')}, got ${arg.value}`);
}
validated[def.name] = choice.value;
} else {
// Otherwise just check the type
if (arg.value instanceof OptionTypeMap[def.type]) {
// TODO: Coerce the type if possible
throw new Error(`Invalid type for ${def.name}: expected ${def.type}, got ${typeof arg}`);
} else {
validated[def.name] = arg.value;
}
}
}
// TODO: Fix types so we don't need to cast here
return Promise.resolve(validated as ArgsFromOptions<T>);
};
[HandlerType.Events] = { [HandlerType.Events] = {
register: (event: string | string[], handler: Handler) => this.register(HandlerType.Events, event, handler), register: (event: string | string[], handler: Handler) => this.register(HandlerType.Events, event, handler),
unregister: (event: string | string[], handler?: Handler) => this.unregister(HandlerType.Events, event, handler), unregister: (event: string | string[], handler?: Handler) => this.unregister(HandlerType.Events, event, handler),
@@ -84,11 +112,37 @@ export class Processor {
has: (event: string) => this.handlers[HandlerType.ChatCommands].has(event) ?? this.handlers[HandlerType.ChatCommands].get(event)?.length! > 0 has: (event: string) => this.handlers[HandlerType.ChatCommands].has(event) ?? this.handlers[HandlerType.ChatCommands].get(event)?.length! > 0
}; };
[HandlerType.SlashCommands] = { [HandlerType.ApplicationCommands] = {
register: (event: string | string[], handler: Handler) => this.register(HandlerType.SlashCommands, event, handler), register: (name: string | string[], type: ApplicationCommandType, handler: Handler) => {
unregister: (event: string | string[], handler?: Handler) => this.unregister(HandlerType.SlashCommands, event, handler), const names = Array.isArray(name) ? name : [name];
execute: async (event: string, context: Context, args: any[]) => this.execute(HandlerType.SlashCommands, event, context, args),
has: (event: string) => this.handlers[HandlerType.SlashCommands].has(event) ?? this.handlers[HandlerType.SlashCommands].get(event)?.length! > 0 // This is an application command so we need to register it with the API
// Ensure the handler is a CommandHandler
// TODO: Don't enforce this?
if (!Processor.isCommandHandler(handler)) {
throw new Error('Handler must be a CommandHandler');
}
// Ensure the command name is valid
// @see https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-naming
if (!names.every(/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u.test.bind(/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u))) {
throw new Error('Invalid application command name');
};
for (const n of names) {
// Register the command with the API
const command: Record<string, any> = handler.toJSON();
command.name = n;
command.description = handler.description;
command.type = type;
this.api.post(`/applications/${this.client.application_id}/commands`, command);
}
this.register(HandlerType.ApplicationCommands, names, handler)
},
unregister: (event: string | string[], handler?: Handler) => this.unregister(HandlerType.ApplicationCommands, event, handler),
execute: async (event: string, context: Context, args: any[]) => this.execute(HandlerType.ApplicationCommands, event, context, args),
has: (event: string) => this.handlers[HandlerType.ApplicationCommands].has(event) ?? this.handlers[HandlerType.ApplicationCommands].get(event)?.length! > 0,
}; };
[HandlerType.Interactions] = { [HandlerType.Interactions] = {
@@ -97,12 +151,12 @@ export class Processor {
execute: async (event: string, context: Context, args: any[]) => this.execute(HandlerType.Interactions, event, context, args), execute: async (event: string, context: Context, args: any[]) => this.execute(HandlerType.Interactions, event, context, args),
has: (event: string) => this.handlers[HandlerType.Interactions].has(event) ?? this.handlers[HandlerType.Interactions].get(event)?.length! > 0 has: (event: string) => this.handlers[HandlerType.Interactions].has(event) ?? this.handlers[HandlerType.Interactions].get(event)?.length! > 0
}; };
}
const isCommandHandler = (handler: Handler): handler is CommandHandler<readonly ApplicationCommandOption[]> => { static isCommandHandler = (handler: Handler): handler is CommandHandler<readonly ApplicationCommandOption[]> => {
if (typeof handler === 'object') { if (typeof handler === 'object') {
return 'args' in handler && Trait.execute in handler; return 'args' in handler && Trait.execute in handler && handler instanceof CommandHandler;
} else { } else {
return false; return false;
} }
}
} }
+7 -1
View File
@@ -2,7 +2,7 @@ import { Trait } from "./common";
// Enum for Application Command Types // Enum for Application Command Types
export enum ApplicationCommandType { export enum ApplicationCommandType {
ChatInput = 1, Chat = 1,
User = 2, User = 2,
Message = 3, Message = 3,
PrimaryEntryPoint = 4, PrimaryEntryPoint = 4,
@@ -65,6 +65,12 @@ export type ApplicationCommandOption = {
autocomplete?: boolean; autocomplete?: boolean;
}; };
export type ApplicationCommandOptionResult = {
value: any,
type: number,
name: string
};
// Define the DiscordApplicationCommand type // Define the DiscordApplicationCommand type
export type DiscordApplicationCommand = { export type DiscordApplicationCommand = {
id: string; id: string;
+9 -1
View File
@@ -15,7 +15,7 @@ export enum Expectation {
export enum HandlerType { export enum HandlerType {
Events = 'events', Events = 'events',
ChatCommands = 'chat_commands', ChatCommands = 'chat_commands',
SlashCommands = 'slash_commands', ApplicationCommands = 'application_commands',
Interactions = 'interactions' Interactions = 'interactions'
} }
@@ -52,6 +52,14 @@ export type Payload = {
t: string | null; t: string | null;
}; };
export type InteractionPayload = Record<string, any>;
export enum InteractionResponseType {
Pong = 1,
Message = 4,
DeferredMessage = 5
}
export enum Intent { export enum Intent {
GUILDS = 1 << 0, GUILDS = 1 << 0,
GUILD_MEMBERS = 1 << 1, GUILD_MEMBERS = 1 << 1,
+49 -19
View File
@@ -1,8 +1,8 @@
import type { API } from "../services/api"; import type { API } from "../services/api";
import type { Client } from "../services/client"; import type { Client } from "../services/client";
import { hydrate, hydrator, type Hydrateable, type Hydrated } from "../services/hydrator"; import { hydrate, hydrator, type Hydrateable, type Hydrated } from "../services/hydrator";
import { isNil } from "../utils"; import { isNil, exists } from "../utils";
import { fromDiscord, type Expectation } from "./common"; import { fromDiscord, InteractionResponseType, type Expectation, type InteractionPayload } from "./common";
import type { Guild } from "./guild"; import type { Guild } from "./guild";
import { type DiscordMessage, Message, type MessagePayload } from "./message"; import { type DiscordMessage, Message, type MessagePayload } from "./message";
/** /**
@@ -26,28 +26,39 @@ import { type DiscordMessage, Message, type MessagePayload } from "./message";
export type HydratedContext<T extends Array<Expectation>> = Context & { export type HydratedContext<T extends Array<Expectation>> = Context & {
guild: Extract<Expectation.Guild, T[number]> extends never ? undefined : Guild; guild: Extract<Expectation.Guild, T[number]> extends never ? undefined : Guild;
message: Extract<Expectation.Message, T[number]> extends never ? undefined : Message;
}; };
type ContextData =
| { channel_id?: string; message_id?: string; guild_id?: string; message?: MessagePayload, interaction: InteractionPayload }
| { channel_id?: string; message_id?: string; guild_id?: string; message: MessagePayload, interaction?: InteractionPayload }
export class Context { export class Context {
// Public properties // Public properties
message_id: string; message_id?: string;
channel_id: string; channel_id: string;
guild_id?: string; guild_id?: string;
message?: MessagePayload;
// TODO: Type this
interaction?: InteractionPayload;
constructor(private client: Client, private api: API, public message: MessagePayload) { constructor(private client: Client, private api: API, data: ContextData) {
console.log('Context created: ', ); const channel_id = data.channel_id ?? data.message?.channel_id ?? data.interaction?.channel_id;
if (isNil(message.id)) {
throw new Error('Message ID is required to create a context');
}
if (isNil(message.channel_id)) { if (isNil(channel_id)) {
throw new Error('Channel ID is required to create a context'); throw new Error('Channel ID is required to create a context');
} }
this.client = client; this.client = client;
this.message_id = message.id; this.message_id = data.message_id ?? data.message?.id;
this.channel_id = message.channel_id; this.channel_id = channel_id;
this.guild_id = message.guild_id; this.guild_id = data.guild_id ?? data.message?.guild_id ?? data.interaction?.guild_id;
this.message = data.message;
this.interaction = data.interaction;
}
get self() {
return this.client.self!;
} }
/** /**
@@ -63,7 +74,7 @@ export class Context {
* *
* @throws Will throw an error if an expectation cannot be resolved. * @throws Will throw an error if an expectation cannot be resolved.
*/ */
hydrate = async < public hydrate = async <
T extends Hydrateable | Hydrated<Hydrateable, Array<Expectation>>, T extends Hydrateable | Hydrated<Hydrateable, Array<Expectation>>,
K extends Array<Expectation> K extends Array<Expectation>
>(data: T, expectations: K) => { >(data: T, expectations: K) => {
@@ -81,14 +92,10 @@ export class Context {
* *
* @returns A promise that resolves to a type guard function. The type guard function returns `true` if the data object was successfully hydrated, otherwise `false`. * @returns A promise that resolves to a type guard function. The type guard function returns `true` if the data object was successfully hydrated, otherwise `false`.
*/ */
hydrator = async <T extends Hydrateable | Hydrated<Hydrateable, Array<Expectation>>, K extends Array<Expectation>>(data: T, expectations: K) => { public hydrator = async <T extends Hydrateable | Hydrated<Hydrateable, Array<Expectation>>, K extends Array<Expectation>>(data: T, expectations: K) => {
return hydrator<T, K>(data, expectations, this.client, this.api); return hydrator<T, K>(data, expectations, this.client, this.api);
} }
// get author() {
// return this.client.users.get(this.author_id);
// };
/** /**
* Reply to the message that this command * Reply to the message that this command
* @param message The message to send to the channel * @param message The message to send to the channel
@@ -96,15 +103,22 @@ export class Context {
* @returns The message that was sent * @returns The message that was sent
*/ */
public reply = async (message: Message | string, reference: boolean = true): Promise<MessagePayload> => { public reply = async (message: Message | string, reference: boolean = true): Promise<MessagePayload> => {
console.log('this.interaction', this.interaction)
if (typeof message === 'string') { if (typeof message === 'string') {
message = new Message().setContent(message); message = new Message().setContent(message);
} }
if (exists(this.message)) {
if (reference) { if (reference) {
message.setReference(this.message) message.setReference(this.message)
} }
return this.client.sendMessage(this.channel_id, message); return this.client.sendMessage(this.channel_id, message);
} else if (exists(this.interaction)) {
return this.ack(InteractionResponseType.Message, message.toJSON());
} else {
throw new Error('Cannot reply without a message or interaction payload');
}
}; };
/** /**
@@ -138,11 +152,27 @@ export class Context {
* @param {Message | string} message - The updated message content. * @param {Message | string} message - The updated message content.
* @returns {Promise<Message>} The updated message. * @returns {Promise<Message>} The updated message.
*/ */
public async editMessage (message: Message, content?: string) { public editMessage = async (message: Message, content?: string) => {
const clone = new Message(message); const clone = new Message(message);
if (content) { if (content) {
clone.setContent(content); clone.setContent(content);
} }
return Message[fromDiscord](await this.api.patch<DiscordMessage>(`/channels/${clone.channel_id}/messages/${clone.id}`, clone)); return Message[fromDiscord](await this.api.patch<DiscordMessage>(`/channels/${clone.channel_id}/messages/${clone.id}`, clone));
}; };
// Interaction methods
public ack = async (type: InteractionResponseType = InteractionResponseType.Pong, data?: Record<string, any>) => {
if (isNil(this.interaction)) {
throw new Error('Cannot acknowledge an interaction without an interaction payload');
}
return this.api.post(`/interactions/${this.interaction.id}/${this.interaction.token}/callback`, {
type,
data
});
};
public defer = async () => {
return this.ack(InteractionResponseType.DeferredMessage);
};
} }
+53 -21
View File
@@ -1,42 +1,74 @@
import type { ApplicationCommandOption, ApplicationCommandOptionType } from "./applicationCommand"; import { ApplicationCommandOptionType, type ApplicationCommandOption } from "./applicationCommand";
import { Trait, type EventPayload } from "./common"; import { Trait, type EventPayload } from "./common";
import type { Context } from "./context"; import type { Context } from "./context";
type PrimitiveTypeMap = { export const OptionTypeMap = {
[ApplicationCommandOptionType.String]: string; [ApplicationCommandOptionType.String]: String,
[ApplicationCommandOptionType.Integer]: number; [ApplicationCommandOptionType.Integer]: Number,
[ApplicationCommandOptionType.Boolean]: boolean; [ApplicationCommandOptionType.Boolean]: Boolean,
[ApplicationCommandOptionType.Number]: number; [ApplicationCommandOptionType.Number]: Number,
[ApplicationCommandOptionType.User]: string; // Could be a user ID or object depending on your use case [ApplicationCommandOptionType.User]: String, // Could be a user ID or object depending on your use case
[ApplicationCommandOptionType.Channel]: string; // Could be a channel ID or object [ApplicationCommandOptionType.Channel]: String, // Could be a channel ID or object
[ApplicationCommandOptionType.Role]: string; // Could be a role ID or object [ApplicationCommandOptionType.Role]: String, // Could be a role ID or object
[ApplicationCommandOptionType.Mentionable]: string; // Could be a mentionable ID or object [ApplicationCommandOptionType.Mentionable]: String, // Could be a mentionable ID or object
[ApplicationCommandOptionType.Attachment]: string; // Could represent attachment IDs [ApplicationCommandOptionType.Attachment]: String, // Could represent attachment IDs
[ApplicationCommandOptionType.SubCommand]: never; // Handled by `options` [ApplicationCommandOptionType.SubCommand]: String, // Handled by `options`
[ApplicationCommandOptionType.SubCommandGroup]: never; // Handled by `options` [ApplicationCommandOptionType.SubCommandGroup]: String, // Handled by `options`
}; };
// Recursive type to map ApplicationCommandOption to arguments // Recursive type to map ApplicationCommandOption to arguments
type ArgsFromOptions<T extends readonly ApplicationCommandOption[]> = { type ArgsFromOptions<T extends readonly ApplicationCommandOption[]> = {
[Option in T[number] as Option['id']]: Option extends { options: readonly ApplicationCommandOption[] } [Option in T[number] as Option['name']]: Option extends { options: readonly ApplicationCommandOption[] }
? ArgsFromOptions<Option['options']> // Recursive inference for subcommands ? ArgsFromOptions<Option['options']> // Recursive inference for subcommands
: Option['choices'] extends readonly { value: infer U }[] : Option['choices'] extends readonly { value: infer U }[]
? U // Infer from choices if defined ? Option['required'] extends true // If required, keep as is; otherwise, make optional
: Option['type'] extends keyof PrimitiveTypeMap ? U
? PrimitiveTypeMap[Option['type']] // Otherwise, map from ApplicationCommandOptionType : U | undefined
: unknown; : Option['type'] extends keyof typeof OptionTypeMap
? Option['required'] extends true // If required, keep as is; otherwise, make optional
? typeof OptionTypeMap[Option['type']]
: typeof OptionTypeMap[Option['type']] | undefined
: Option['required'] extends true
? unknown
: unknown | undefined;
}; };
export type CommandHandler<T extends readonly ApplicationCommandOption[]> = { export type CommandHandlerInput<T extends readonly ApplicationCommandOption[]> = {
args: T; args: T;
description: string;
[Trait.execute]: (context: Context, args: ArgsFromOptions<T>) => Promise<void> | void; [Trait.execute]: (context: Context, args: ArgsFromOptions<T>) => Promise<void> | void;
}; };
export class CommandHandler<T extends readonly ApplicationCommandOption[]> {
args: T;
description: string;
[Trait.execute]!: (context: Context, args: ArgsFromOptions<T>) => Promise<void> | void;
constructor(input: CommandHandlerInput<T>) {
// Validate option names
// @see https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-naming
for (const name of input.args.map((arg) => arg.name)) {
if (/^[-_\p{L}\p{N}\p{sc=Deva}\p{sc=Thai}]{1,32}$/u.test(name) === false) {
throw new Error(`Invalid option name: ${name}`);
}
}
this.args = input.args;
this.description = input.description
this[Trait.execute] = input[Trait.execute];
}
toJSON() {
return {
options: this.args,
};
}
};
// Helper function to infer type of `args` // Helper function to infer type of `args`
export function createCommandHandler<T extends readonly ApplicationCommandOption[]>( export function createCommandHandler<T extends readonly ApplicationCommandOption[]>(
handler: CommandHandler<T> handler: CommandHandlerInput<T>
): CommandHandler<T> { ): CommandHandler<T> {
return handler as CommandHandler<T>; return new CommandHandler(handler);
} }
export type CommandHandlerFunction = (context: Context, args: any[]) => Promise<void> | void; export type CommandHandlerFunction = (context: Context, args: any[]) => Promise<void> | void;
+19 -14
View File
@@ -1,5 +1,5 @@
import { Client, type Context, createCommandHandler, Expectation, Intent, type MessagePayload, Trait } from '../src/index'; import { Client, type Context, createCommandHandler, Expectation, Intent, type MessagePayload, Trait } from '../src/index';
import { ApplicationCommandOptionType } from '../src/types/applicationCommand'; import { ApplicationCommandOptionType, ApplicationCommandType } from '../src/types/applicationCommand';
const client = new Client({ const client = new Client({
token: process.env.BOT_TOKEN!, token: process.env.BOT_TOKEN!,
@@ -21,25 +21,26 @@ client.chatCommands.register('ping', async (context: Context, args: any[]) => {
await context.editMessage(message, `Pong! Latency: ${time}ms`); await context.editMessage(message, `Pong! Latency: ${time}ms`);
}); });
client.slashCommands.register('ping', createCommandHandler({ client.applicationCommands.register('hello', ApplicationCommandType.Chat, createCommandHandler({
description: "Introduce yourself",
args: [ args: [
{ {
type: ApplicationCommandOptionType.String, type: ApplicationCommandOptionType.String,
name: "Name", name: "name",
id: "name", id: "name",
description: "Your name", description: "Your name",
required: true required: true
}, },
{ {
type: ApplicationCommandOptionType.Integer, type: ApplicationCommandOptionType.Integer,
name: "Age", name: "age",
id: "age", id: "age",
description: "Your age", description: "Your age",
required: true required: true
}, },
{ {
type: ApplicationCommandOptionType.String, type: ApplicationCommandOptionType.String,
name: "Favorite Animal", name: "favorite_animal",
id: "favorite_animal", id: "favorite_animal",
description: "Your favorite animal", description: "Your favorite animal",
required: false, required: false,
@@ -56,24 +57,28 @@ client.slashCommands.register('ping', createCommandHandler({
} }
] as const, ] as const,
[Trait.execute]: async (context, args) => { [Trait.execute]: async (context, args) => {
context.reply(`You are ${args.name} and you are ${args.age} years old and your favorite animal is ${args.favorite_animal}`, true); context.reply(
`\
Hello ${args.name}! I see you are ${args.age} years old${!!args.favorite_animal ? ` and your favorite animal is ${args.favorite_animal}.`: '.'}
Nice to meet you, I'm ${context.self.username}!`
, true);
} }
})); }));
client.chatCommands.register('me', async (context: Context, _args: any[]) => { client.chatCommands.register('me', async (context: Context, _args: any[]) => {
// Note: Testing repeated hydration // Note: Testing repeated hydration
const message = await context.hydrate(context.message, [Expectation.Channel]) const hydrated = await context.hydrate(context.message, [Expectation.Channel])
const rehydrated = await context.hydrate(message, [Expectation.Guild]); const rehydrated = await context.hydrate(hydrated, [Expectation.Guild]);
rehydrated.guild.name rehydrated.guild.name
const hydrate = await context.hydrator(message, [Expectation.Guild]); const hydrate = await context.hydrator(hydrated, [Expectation.Guild]);
const hasGuild = hydrate(message); const hasGuild = hydrate(hydrated);
// if (hasGuild) { // if (hasGuild) {
// message.guild.name // hydrated.guild.name
// } else { // } else {
// message // hydrated
// } // }
// await context.reply(`You are ${message.author.username} ${hasGuild ? message.guild.name : ''}`, true); // await context.reply(`You are ${message.author.username} ${hasGuild ? message.guild.name : ''}`, true);
@@ -90,8 +95,8 @@ client.chatCommands.register('here', async (context: Context, _args: any[]) => {
}); });
client.chatCommands.register('quote', async (context: Context, _args: any[]) => { client.chatCommands.register('quote', async (context: Context, _args: any[]) => {
const { message, hydrator } = context; const message = context.message;
const reference = (await hydrator(message, [Expectation.Message]))(message); const reference = (await context.hydrator(message, [Expectation.Message]))(message);
let reply: MessagePayload; let reply: MessagePayload;