Skip to content

Latest commit

 

History

History
59 lines (51 loc) · 1.73 KB

addCommands.md

File metadata and controls

59 lines (51 loc) · 1.73 KB

How to add commands

  1. Create a file under src/commands with any name you want (should have .ts extension)
  2. 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
  1. Launch the bot, your commands are auto registered every time you launch.

Subcommands

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

Notes