WOKCommands
Search…
Slash commands
Slash commands are a new system for commands within Discord bots. WOKCommands aims to help developers implement slash commands in the easiest way possible. You can view the official documentation for slash commands here.
Here is a basic ping pong slash command example:
JavaScript
TypeScript
ping.js
module.exports = {
category: 'Testing', // Required for slash commands
description: 'A simple ping pong command', // Required for slash commands
slash: true,
testOnly: true, // Ensure you have test servers setup, see the below paragraph
callback: ({ interaction }) => {
// The interaction property will be undefined if the command is
// ran as a legacy command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (interaction) {
interaction.reply({
content: 'pong'
})
}
}
}
ping.ts
import { ICommand } from 'wokcommands'
export default {
category: 'Testing', // Required for slash commands
description: 'A simple ping pong command', // Required for slash commands
slash: true,
testOnly: true, // Ensure you have test servers setup, see the below paragraph
callback: ({ interaction }) => {
// The interaction property will be undefined if the command is
// ran as a legacy command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with it.
if (interaction) {
interaction.reply({
content: 'pong'
})
}
}
} as ICommand
When running /ping it will send "pong" in the same channel.
The "testOnly" property is important when testing. Guild based slash commands will register instantly, where global slash commands may take up to an hour to be visible in servers who use your bot. To specify the test guild IDs please check the Test Servers documentation.
All of the normal WOKCommands callback arguments are available aside from message and prefix. A full list can be found here.

Slash Command Arguments

Slash commands handle arguments differently, however WOKCommands will allow you to specify your slash command arguments in the same way as normal commands. Here is an example of a "name and age" command:
JavaScript
TypeScript
nameage.js
module.exports = {
category: 'Testing',
description: 'Displays your name and age', // Required for slash commands
slash: true,
testOnly: true, // Ensure you have test servers setup
minArgs: 2,
expectedArgs: '<name> <age>',
callback: ({ interaction, args }) => {
// Destructure the name and age from the args array
const [name, age] = args
// The interaction property will be undefined if the command is
// ran as a legacy command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (interaction) {
interaction.reply({
content: `Hello my name is ${name} and I am ${age} years old.`
})
}
}
}
nameage.ts
import { ICommand } from 'wokcommands'
export default {
category: 'Testing', // Required for slash commands
description: 'Displays your name and age', // Required for slash commands
slash: true,
testOnly: true, // Ensure you have test servers setup
minArgs: 2,
expectedArgs: '<name> <age>',
callback: ({ interaction, args }) => {
// Destructure the name and age from the args array
const [name, age] = args
// The interaction property will be undefined if the command is
// ran as a legacy command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (interaction) {
interaction.reply({
content: `Hello my name is ${name} and I am ${age} years old.`
})
}
}
} as ICommand
If you were to run /nameage Alex 27 you would then see the text "Hello my name is Alex and I am 27 years old." as a reply. The minArgs property will let WOKCommands know how many arguments are required. In this case it is all of them, however if minArgs was 1 then the "Age" argument would be optional.

Support for legacy and slash commands

If you want to support the typical "!commandname" (or whatever your prefix is) syntax and slash commands you can easily do so by setting the slash property to "both" like so:
JavaScript
TypeScript
nameage.js
module.exports = {
category: 'Testing', // Required for slash commands
description: 'Displays your name and age', // Required for slash commands
slash: 'both', // Support both legacy and slash command syntax
testOnly: true, // Ensure you have test servers setup
minArgs: 2,
expectedArgs: '<name> <age>',
callback: ({ message, interaction, args }) => {
// Destructure the name and age from the args array
const [name, age] = args
// Create a string that is used in either command syntax
const reply = `Hello my name is ${name} and I am ${age} years old.`
// The message property will be undefined if the command is ran
// as a slash command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (message) {
message.reply({
content: reply
})
}
// The interaction property will be undefined if the command is
// ran as a legacy command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (interaction) {
interaction.reply({
content: reply
})
}
}
}
nameage.ts
import { ICommand } from 'wokcommands'
export default {
category: 'Testing', // Required for slash commands
description: 'Displays your name and age', // Required for slash commands
slash: 'both', // Support both legacy and slash command syntax
testOnly: true, // Ensure you have test servers setup
minArgs: 2,
expectedArgs: '<name> <age>',
callback: ({ message, interaction, args }) => {
// Destructure the name and age from the args array
const [name, age] = args
// Create a string that is used in either command syntax
const reply = `Hello my name is ${name} and I am ${age} years old.`
// The message property will be undefined if the command is ran
// as a slash command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (message) {
message.reply({
content: reply
})
}
// The interaction property will be undefined if the command is
// ran as a legacy command. It is encouraged to check if 'message' or
// 'interaction' exists before interacting with them.
if (interaction) {
interaction.reply({
content: reply
})
}
}
} as ICommand

Using your own Options for Slash Commands

Sometimes you may want to pass in your own options object to your slash command. You can easily do so by passing in an options property to your command like so:
JavaScript
TypeScript
add.js
module.exports = {
category: 'test', // Required for the slash commands
description: 'Adds two numbers', // Required for the slash commands
slash: true, // If options are given then slash must be either true or 'both'
testOnly: true, // Ensure you have test servers setup
options: [
{
name: 'num1', // Must be lower case
description: 'The first number.',
required: true,
type: 'STRING',
},
{
name: 'num2', // Must be lower case
description: 'The second number.',
required: true,
type: 'STRING',
},
],
callback: ({ interaction, args }) => {
const num1 = parseInt(args[0])
const num2 = parseInt(args[1])
if (interaction) {
interaction.reply({
content: String(num1 + num2),
})
}
},
}
add.ts
import { ICommand } from 'wokcommands'
export default {
category: 'test', // Required for the slash commands
description: 'Adds two numbers', // Required for the slash commands
slash: true, // If options are given then slash must be either true or 'both'
testOnly: true, // Ensure you have test servers setup
options: [
{
name: 'num1', // Must be lower case
description: 'The first number.',
required: true,
type: 'STRING',
},
{
name: 'num2', // Must be lower case
description: 'The second number.',
required: true,
type: 'STRING',
},
],
callback: ({ interaction, args }) => {
const num1 = parseInt(args[0])
const num2 = parseInt(args[1])
if (interaction) {
interaction.reply({
content: String(num1 + num2),
})
}
},
} as ICommand