HoeenHero
HoeenHero
DIAdiscord.js - Imagine an app
Created by 0fficerSally on 10/12/2024 in #djs-questions
Command Handling With TypeScript
I personally ended up just exporting a few Collections as my command/event registries:
/**
* Command Interface - Represents a command's data and response method.
* data: A Discord.SlashCommandBuilder object that has been used to build the command's data.
* execute: The method that executes when the command is called. Provides the interaction that
* triggerd the command as an argument.
* autocomplete: An optional method that executes to provide autocomplete options to the user.
*/
export interface ICommand {
data: Discord.SlashCommandOptionsOnlyBuilder;
execute: (interaction: Discord.ChatInputCommandInteraction) => Promise<void>;
autocomplete?: (interaction: Discord.AutocompleteInteraction) => Promise<void>;
}

/**
* All commands are internally registered here.
*/
export const commands = new Discord.Collection<string, ICommand>();
/**
* Command Interface - Represents a command's data and response method.
* data: A Discord.SlashCommandBuilder object that has been used to build the command's data.
* execute: The method that executes when the command is called. Provides the interaction that
* triggerd the command as an argument.
* autocomplete: An optional method that executes to provide autocomplete options to the user.
*/
export interface ICommand {
data: Discord.SlashCommandOptionsOnlyBuilder;
execute: (interaction: Discord.ChatInputCommandInteraction) => Promise<void>;
autocomplete?: (interaction: Discord.AutocompleteInteraction) => Promise<void>;
}

/**
* All commands are internally registered here.
*/
export const commands = new Discord.Collection<string, ICommand>();
I can access them in any file I need to with
import { commands } from './common'; // relative file location may vary
import { commands } from './common'; // relative file location may vary
9 replies