WOKCommands
Search…
Command handler
The command handler class is what handles all of the command functionality. You can access it's instance with the following:
JavaScript
TypeScript
index.js
const DiscordJS = require('discord.js')
const WOKCommands = require('wokcommands')
const path = require('path')
const { Intents } = DiscordJS
const client = new DiscordJS.Client({
// These intents are recommended for the built in help menu
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
})
client.on('ready', () => {
const wok = new WOKCommands(client, {
// The name of the local folder for your command files
commandsDir: path.join(__dirname, 'commands')
})
const { commandHandler } = wok
})
client.login('YOUR TOKEN HERE')
index.ts
import DiscordJS, { Intents } from 'discord.js'
import WOKCommands from 'wokcommands'
import path from 'path'
const client = new DiscordJS.Client({
// These intents are recommended for the built in help menu
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
})
client.on('ready', () => {
const wok = new WOKCommands(client, {
// The name of the local folder for your command files
commandsDir: path.join(__dirname, 'commands'),
// Allow importing of .ts files if you are using ts-node
typeScript: true,
})
const { commandHandler } = wok
})
client.login('YOUR TOKEN HERE')
Once you have access to the commandHandler instance you can use the following methods:
The structure of this code is for demonstration purposes only. Do not copy/paste this code, it will not work.
class CommandHandler {
// Returns all commands as "ICommands"
// See the bottom of this page for more details
public get commands(): ICommand[];
// Returns all commands from a specific category
public getCommandsByCategory(
category: string,
visibleOnly?: boolean
): ICommand[];
// This returns a "Command" object which is different than an ICommand
// See the bottom of this page for more details
public getCommand(name: string): Command | undefined;
// Returns the ICommand for a specific command
public getICommand(name: string): ICommand | undefined;
}
Commands often use the "ICommand" TypeScript interface to define what properties they should have. When accessing an ICommand using the above methods you will also gain access to these properties:
export interface ICommand {
names?: string[] | string
category: string
description: string
callback?(obj: ICallbackObject): any
error?(obj: IErrorObject): any
minArgs?: number
maxArgs?: number
syntaxError?: { [key: string]: string }
expectedArgs?: string
syntax?: string
requiredPermissions?: PermissionString[]
permissions?: PermissionString[]
cooldown?: string
globalCooldown?: string
ownerOnly?: boolean
hidden?: boolean
guildOnly?: boolean
testOnly?: boolean
slash?: boolean | 'both'
options?: ApplicationCommandOptionData[]
}
There is also a "Command" object which is it's own class. Here are the methods for that class:
class Command {
// Returns all names and aliases for the command
public get names(): string[];
// Returns the name of the category and description
public get category(): string;
public get description(): string;
// Returns the minimum and maximum number of arguments
public get minArgs(): number;
public get maxArgs(): number; // -1 by default
// Gets the syntax error and expected arguments
public get syntaxError(): {[key: string]: string};
public get expectedArgs(): string | undefined;
// Returns what Discord permissions are assigned
public get requiredPermissions(): permissions | undefined;
// Gets the cooldown value, duration type, and full string
public get cooldownDuration(): number;
public get cooldownChar(): string;
public get cooldown(): string;
public get globalCooldown(): string;
// Will ensure the cooldown string is valid
public verifyCooldown(cooldown: string, type: string);
// Is this command test only?
public get testOnly(): boolean;
// Is this command guild only?
public get guildOnly(): boolean;
// Is this a hidden command?
public get hidden(): boolean;
// Is this a slash command?
// The string will be "both"
public get slash(): boolean | string;
}
Copy link