JSPM

  • Created
  • Published
  • Downloads 408
  • Score
    100M100P100Q76221F
  • License MIT

A package for handling discord.js commands with TypeScript.

Package Exports

  • command-ts
  • command-ts/out/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (command-ts) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

command-ts

Discord GitHub Action NPM Version NPM Downloads NPM Unpacked Size NPM License

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-ts

PNPM

pnpm add command-ts

Yarn

yarn add command-ts

Usage

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) => any

Contributing

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.