Example

Install

npm i discommand
yarn add discommand
pnpm add discommand

ChatInput Command

Usage for Javascript


If use by commonjs
const { DiscommandClient } = require('discommand')
const { GatewayIntentBits } = require('discord.js')

const client = new DiscommandClient(
  {
    intents: [GatewayIntentBits.Guilds],
  },
  {
    directory: {
      command: __dirname + '/commands',
    },
  }
)

client.login('your_bot_token')
const { Command } = require('discommand')
const { ApplicationCommandType } = require('discord.js')

module.exports = class extends Command {
  constructor() {
    super({
      name: 'ping',
      description: 'Pong',
      type: ApplicationCommandType.ChatInput,
    })
  }
  execute(interaction) {
    interaction.reply('Pong!')
  }
}

import { DiscommandClient } from 'discommand'
import { GatewayIntentBits } from 'discord.js'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const client = new DiscommandClient(
  {
    intents: [GatewayIntentBits.Guilds],
  },
  {
    directory: {
      command: dirname(fileURLToPath(import.meta.url)) + '/commands',
    },
  }
)

client.login('your_bot_token')
import { Command } from 'discommand'
import { ApplicationCommandType } from 'discord.js'

export default class extends Command {
  constructor() {
    super({
      name: 'ping',
      description: 'Pong',
      type: ApplicationCommandType.ChatInput,
    })
  }
  execute(interaction) {
    interaction.reply('Pong!')
  }
}

Usage for TypeScript


If use by commonjs
import { DiscommandClient } from 'discommand'
import { GatewayIntentBits } from 'discord.js'

const client = new DiscommandClient(
  {
    intents: [GatewayIntentBits.Guilds],
  },
  {
    directory: {
      command: __dirname + '/commands',
    },
  }
)

client.login('your_bot_token')

import { DiscommandClient } from 'discommand'
import { GatewayIntentBits } from 'discord.js'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const client = new DiscommandClient(
  {
    intents: [GatewayIntentBits.Guilds],
  },
  {
    directory: {
      command: dirname(fileURLToPath(import.meta.url)) + '/commands',
    },
  }
)

client.login('your_bot_token')
import { Command } from 'discommand'
import { ChatInputCommandInteraction, ApplicationCommandType } from 'discord.js'

export default class extends Command {
  constructor() {
    super({
      name: 'ping',
      description: 'Pong',
      type: ApplicationCommandType.ChatInput,
    })
  }
  execute(interaction: ChatInputCommandInteraction) {
    interaction.reply('Pong!')
  }
}

Context Menu

Usage for JavaScript


import { Command } from 'discommand'
import { ApplicationCommandType } from 'discord.js'

export default class extends Command {
  constructor() {
    super({
      name: 'user',
      type: ApplicationCommandType.User,
    })
  }
  execute(interaction) {
    interaction.reply(`Target User is ${interaction.targetUser}`)
  }
}
import { Command } from 'discommand'
import { ApplicationCommandType } from 'discord.js'

export default class extends Command {
  constructor() {
    super({
      name: 'message',
      type: ApplicationCommandType.Message,
    })
  }
  execute(interaction) {
    interaction.reply(`Target Message is ${interaction.targetMessage}`)
  }
}

Using for TypeScript


import { Command } from 'discommand'
import {
  UserContextMenuCommandInteraction,
  ApplicationCommandType,
} from 'discord.js'

export default class extends Command {
  constructor() {
    super({
      name: 'user',
      type: ApplicationCommandType.User,
    })
  }
  execute(interaction: UserContextMenuCommandInteraction) {
    interaction.reply(`Target User is ${interaction.targetUser}`)
  }
}
import { Command } from 'discommand'
import {
  MessageContextMenuCommandInteraction,
  ApplicationCommandType,
} from 'discord.js'

export default class extends Command {
  constructor() {
    super({
      name: 'message',
      type: ApplicationCommandType.Message,
    })
  }
  execute(interaction: MessageContextMenuCommandInteraction) {
    interaction.reply(`Target Message is ${interaction.targetMessage}`)
  }
}

Event listener

import { Listener } from 'discommand'

export default class extends Listener {
  constructor() {
    super('ready')
  }
  execute(client) {
    console.log(`Login as ${client.user?.username}`)
  }
}
import { Listener } from 'discommand'
import { type Client } from 'discord.js'

export default class extends Listener {
  constructor() {
    super('ready')
  }
  execute(client: Client) {
    console.log(`Login as ${client.user?.username}`)
  }
}