command-ts
v0.0.2
Published
A package for handling discord.js commands with TypeScript.
Readme
command-ts
A package for handling discord.js commands with TypeScript
Installation
To install command-ts, run one of the following commands based on your preferred package manager:
NPM
npm install command-tsPNPM
pnpm add command-tsYarn
yarn add command-tsUsage
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")Command Structure
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!")
}
}API Documentation
Handler Options
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
}Command Types
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
}SelectMenu Types
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
Middleware functions can be used to add custom logic before command execution:
type Middleware = (command: Command, interaction: CommandInteraction, stop: (reason?: string) => void) => anyContributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the Apache-2.0 License. See the LICENSE file for details.
