djshandler-core
Version:
Core command handler framework for Discord.js bots
214 lines (208 loc) • 6.28 kB
TypeScript
import { GatewayIntentBits, SlashCommandBuilder, CommandInteraction, ClientEvents, Client, Collection } from 'discord.js';
/**
* Configuration options for the command handler
*/
interface CommandHandlerOptions {
/** Bot token for authentication */
token?: string;
/** Client ID for deploying slash commands */
clientId?: string;
/** Discord gateway intents */
intents?: GatewayIntentBits[];
/** Whether to automatically deploy commands on start */
autoDeploy?: boolean;
/** Guild ID for guild-specific commands (optional) */
guildId?: string;
}
/**
* Represents a slash command
*/
interface Command {
/** Command data using SlashCommandBuilder */
data: SlashCommandBuilder;
/** Function to execute when command is called */
execute: (interaction: CommandInteraction) => Promise<void> | void;
/** Command category for organization */
category?: string;
/** Whether command is disabled */
disabled?: boolean;
/** Cooldown in seconds */
cooldown?: number;
}
/**
* Represents an event handler
*/
interface Event {
/** Event name from Discord.js ClientEvents */
name: keyof ClientEvents;
/** Whether this is a once event */
once?: boolean;
/** Function to execute when event is triggered */
execute: (...args: any[]) => Promise<void> | void;
}
/**
* Command handler interface
*/
interface CommandHandler {
registerCommand(command: Command): this;
registerEvent(event: Event): this;
loadCommands(path: string): Promise<this>;
loadEvents(path: string): Promise<this>;
deployCommands(guildId?: string): Promise<void>;
start(): Promise<void>;
}
/**
* Event handler interface
*/
interface EventHandler {
name: keyof ClientEvents;
once?: boolean;
execute: (...args: any[]) => Promise<void> | void;
}
/**
* Command execution context
*/
interface CommandContext {
interaction: CommandInteraction;
args: any[];
handler: CommandHandler;
}
/**
* Command metadata for decorators
*/
interface CommandMetadata {
name: string;
description: string;
category?: string;
cooldown?: number;
disabled?: boolean;
}
/**
* Event metadata for decorators
*/
interface EventMetadata {
name: keyof ClientEvents;
once?: boolean;
}
/**
* The main command handler class that manages Discord.js bot commands and events
*/
declare class DJSHandler {
private options;
private client;
private commands;
private events;
private rest?;
constructor(options: CommandHandlerOptions);
/**
* Register a command with the handler
*/
registerCommand(command: Command): this;
/**
* Register an event with the handler
*/
registerEvent(event: Event): this;
/**
* Load all commands from a directory
*/
loadCommands(commandsPath: string): Promise<this>;
/**
* Load all events from a directory
*/
loadEvents(eventsPath: string): Promise<this>;
/**
* Deploy slash commands to Discord
*/
deployCommands(guildId?: string): Promise<void>;
/**
* Start the bot
*/
start(): Promise<void>;
/**
* Get the Discord.js client instance
*/
getClient(): Client;
/**
* Get registered commands
*/
getCommands(): Collection<string, Command>;
/**
* Get registered events
*/
getEvents(): Collection<string, Event>;
/**
* Setup default event handlers
*/
private setupDefaultEvents;
}
/**
* Class decorator to mark a class as a command
*/
declare function CommandClass(metadata: CommandMetadata): <T extends new (...args: any[]) => any>(constructor: T) => T;
/**
* Method decorator to mark a method as a command execute function
*/
declare function Execute(): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
/**
* Class decorator to mark a class as an event handler
*/
declare function EventClass(metadata: EventMetadata): <T extends new (...args: any[]) => any>(constructor: T) => T;
/**
* Method decorator to mark a method as an event execute function
*/
declare function On(eventName: string): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
/**
* Method decorator to mark a method as a once event execute function
*/
declare function Once(eventName: string): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
/**
* Utility function to extract command from decorated class
*/
declare function extractCommand(CommandClass: new (...args: any[]) => any): Command | null;
/**
* Utility function to extract event from decorated class
*/
declare function extractEvent(EventClass: new (...args: any[]) => any): Event | null;
/**
* Utility functions for the djshandler framework
*/
/**
* Recursively load all files from a directory with a specific extension
*/
declare function loadFiles(dirPath: string, extension?: string): Promise<string[]>;
/**
* Dynamically import a module from a file path
*/
declare function importModule<T = any>(filePath: string): Promise<T | null>;
/**
* Check if a file exists
*/
declare function fileExists(filePath: string): Promise<boolean>;
/**
* Create directory recursively if it doesn't exist
*/
declare function ensureDir(dirPath: string): Promise<void>;
/**
* Format command name to be Discord-compatible
*/
declare function formatCommandName(name: string): string;
/**
* Validate Discord command name
*/
declare function isValidCommandName(name: string): boolean;
/**
* Simple logger utility
*/
declare class Logger {
private prefix;
constructor(prefix?: string);
info(message: string, ...args: any[]): void;
warn(message: string, ...args: any[]): void;
error(message: string, ...args: any[]): void;
debug(message: string, ...args: any[]): void;
}
/**
* Default logger instance
*/
declare const logger: Logger;
export { type Command, CommandClass, type CommandContext, DJSHandler as CommandHandler, type CommandHandlerOptions, type CommandMetadata, DJSHandler, type Event, EventClass, type EventHandler, type EventMetadata, Execute, Logger, On, Once, ensureDir, extractCommand, extractEvent, fileExists, formatCommandName, importModule, isValidCommandName, loadFiles, logger };