Ping pong command example

NYXBCommands is easy to get setup and working. On this page you will learn how to create a simple "Ping -> Pong" command. First you must setup NYXBCommands in your main file:

Typescript
import path from 'node:path'
import { Client, IntentsBitField, Partials } from 'discord.js'
import NYXB from '@nyxb/commands'
require('dotenv/config')

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.DirectMessages,
    IntentsBitField.Flags.MessageContent,
  ],
  partials: [Partials.Channel],
})

client.on('ready', () => {
  new NYXB({
    client,
    commandsDir: path.join(__dirname, 'commands'),
  })
})

client.login(process.env.TOKEN)
Javascript
const path = require('node:path')
const { Client, IntentsBitField, Partials } = require('discord.js')
const NYXB = require('@nyxb/commands')
require('dotenv/config')

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.DirectMessages,
    IntentsBitField.Flags.MessageContent,
  ],
  partials: [Partials.Channel],
})

client.on('ready', () => {
  new NYXB({
    client,
    commandsDir: path.join(__dirname, 'commands'),
  })
})

client.login(process.env.TOKEN)

Then create a "commands" folder where you can create a "ping.js" file with the following contents:

Typescript
import { CommandObject, CommandType } from '@nyxb/commands'

export default {
// Required for slash commands
  description: 'Ping pong command',

  // Create a legacy and slash command
  type: CommandType.BOTH,

  // Invoked when a user runs the ping command
  callback: () => {
    // Return the same object you would use in
    // message.reply
    // or
    // interaction.reply
    // NYXBCommands will reply to the message or the interaction
    // depending on how the user ran the command (legacy vs slash)
    return {
      content: 'Pong!',
    }
  },
} as CommandObject
Javascript
const { CommandType } = require('@nyxb/commands')

module.exports = {
// Required for slash commands
  description: 'Ping pong command',

  // Create a legacy and slash command
  type: CommandType.BOTH,

  // Invoked when a user runs the ping command
  callback: () => {
    // Return the same object you would use in
    // message.reply
    // or
    // interaction.reply
    // NYXBCommands will reply to the message or the interaction
    // depending on how the user ran the command (legacy vs slash)
    return {
      content: 'Pong!',
    }
  },
}

After inviting your bot to a Discord server and running !ping ("!" is the default command prefix) or /ping, your bot should reply with Pong!.