A package for handling discord.js commands with TypeScript
To install command-ts, run one of the following commands based on your preferred package manager:
npm install command-ts
pnpm add command-ts
yarn add command-ts
import { handler } from "command-ts"
import { Client, GatewayIntentBits } from "discord.js"
const client = new Client({
intents: [GatewayIntentBits.Guilds]
})
handler({
client,
debug: true, // Enable debug logging
command: "./commands", // Path to commands folder
event: "./events", // Path to events folder
button: "./buttons", // Path to button handlers
modal: "./modals", // Path to modal handlers
selectmenu: "./selectmenus" // Path to select menu handlers
})
client.login("YOUR_TOKEN")
Create a command file in your commands folder:
import { Command } from "command-ts"
export const command: Command = {
data: {
name: "ping",
description: "Replies with pong!"
},
run: async (interaction) => {
await interaction.reply("Pong!")
}
}
The handler
function accepts an options object with the following properties:
type Options = {
client: Client // Discord.js client instance (required)
debug?: boolean // Enable debug logging
event?: string | EventMap // Path to events folder or event map
command?: string | CommandMap // Path to commands folder or command map
button?: string | ButtonMap // Path to button handlers or button map
modal?: string | ModalMap // Path to modal handlers or modal map
selectmenu?: SelectMenu | string // Path to select menu handlers or select menu config
middleware?: Middleware | Middleware[] // Command middleware functions
}
Commands must implement the Command
Types:
type Command = {
data: RESTPostAPIApplicationCommandsJSONBody // Command registration data
autocomplete?: (interaction: AutocompleteInteraction) => any // Optional autocomplete handler
run: (interaction: ChatInputCommandInteraction | ContextMenuCommandInteraction) => any
}
The SelectMenu configuration supports different menu types:
type SelectMenu = {
StringSelectMenu?: string | ((interaction: StringSelectMenuInteraction) => any)
UserSelectMenu?: string | ((interaction: UserSelectMenuInteraction) => any)
RoleSelectMenu?: string | ((interaction: RoleSelectMenuInteraction) => any)
MentionableSelectMenu?: string | ((interaction: MentionableSelectMenuInteraction) => any)
ChannelSelectMenu?: string | ((interaction: ChannelSelectMenuInteraction) => any)
}
Middleware functions can be used to add custom logic before command execution:
type Middleware = (command: Command, interaction: CommandInteraction, stop: (reason?: string) => void) => any
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the Apache-2.0 License. See the LICENSE file for details.