- Create a file under
src/commands
with any name you want (should have .ts extension) - Add the next code block to the file
import { ApplicationCommandType } from "discord.js"
// you should always import this for intellisense
import command from "../lib/interfaces"
const cmd: command = {
// command name
name: "ping",
// command description
description: "Replies to ping",
// type, could be a message command, a chat input command or a user command
type: ApplicationCommandType.ChatInput,
// the function that is going to execute
run: (client, interaction, args) => {
interaction.reply("Pong!")
}
}
export default cmd
- Launch the bot, your commands are auto registered every time you launch.
If you want to add subcommands you need to add the options
property to cmd
import { ApplicationCommandType } from "discord.js"
import command from "../lib/interfaces"
const cmd: command = {
name: "ping",
description: "Replies to ping",
type: ApplicationCommandType.ChatInput,
// "options" should be an array of objects with the Application Command Options syntax
options: [{
name: "reply",
description: "Should i reply to this message?"
type: ApplicationCommandOptionType.Boolean,
required: true
}]
run: (client, interaction, args) => {
// args is an array of subcommands & arguments used when executing the commands
if (args[0] === true) {
interaction.reply("Pong!")
} else {
interaction.reply({ content: "I'm not allowed to reply to this", ephemeral: true })
}
}
}
export default cmd
- If you need documentation about all of these things you can read the Application Commands docs