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
1
const DiscordJS = require('discord.js')
2
const WOKCommands = require('wokcommands')
3
const path = require('path')
4
5
const { Intents } = DiscordJS
6
7
const client = new DiscordJS.Client({
8
// These intents are recommended for the built in help menu
9
intents: [
10
Intents.FLAGS.GUILDS,
11
Intents.FLAGS.GUILD_MESSAGES,
12
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
13
],
14
})
15
16
client.on('ready', () => {
17
const wok = new WOKCommands(client, {
18
// The name of the local folder for your command files
19
commandsDir: path.join(__dirname, 'commands')
20
})
21
22
const { commandHandler } = wok
23
})
24
25
client.login('YOUR TOKEN HERE')
Copied!
index.ts
1
import DiscordJS, { Intents } from 'discord.js'
2
import WOKCommands from 'wokcommands'
3
import path from 'path'
4
5
const client = new DiscordJS.Client({
6
// These intents are recommended for the built in help menu
7
intents: [
8
Intents.FLAGS.GUILDS,
9
Intents.FLAGS.GUILD_MESSAGES,
10
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
11
],
12
})
13
14
client.on('ready', () => {
15
const wok = new WOKCommands(client, {
16
// The name of the local folder for your command files
17
commandsDir: path.join(__dirname, 'commands'),
18
// Allow importing of .ts files if you are using ts-node
19
typeScript: true,
20
})
21
22
const { commandHandler } = wok
23
})
24
25
client.login('YOUR TOKEN HERE')
Copied!
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.
1
class CommandHandler {
2
// Returns all commands as "ICommands"
3
// See the bottom of this page for more details
4
public get commands(): ICommand[];
5
6
// Returns all commands from a specific category
7
public getCommandsByCategory(
8
category: string,
9
visibleOnly?: boolean
10
): ICommand[];
11
12
// This returns a "Command" object which is different than an ICommand
13
// See the bottom of this page for more details
14
public getCommand(name: string): Command | undefined;
15
16
// Returns the ICommand for a specific command
17
public getICommand(name: string): ICommand | undefined;
18
}
Copied!
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:
1
export interface ICommand {
2
names?: string[] | string
3
category: string
4
description: string
5
callback?(obj: ICallbackObject): any
6
error?(obj: IErrorObject): any
7
minArgs?: number
8
maxArgs?: number
9
syntaxError?: { [key: string]: string }
10
expectedArgs?: string
11
syntax?: string
12
requiredPermissions?: PermissionString[]
13
permissions?: PermissionString[]
14
cooldown?: string
15
globalCooldown?: string
16
ownerOnly?: boolean
17
hidden?: boolean
18
guildOnly?: boolean
19
testOnly?: boolean
20
slash?: boolean | 'both'
21
options?: ApplicationCommandOptionData[]
22
}
Copied!
There is also a "Command" object which is it's own class. Here are the methods for that class:
1
class Command {
2
// Returns all names and aliases for the command
3
public get names(): string[];
4
5
// Returns the name of the category and description
6
public get category(): string;
7
public get description(): string;
8
9
// Returns the minimum and maximum number of arguments
10
public get minArgs(): number;
11
public get maxArgs(): number; // -1 by default
12
13
// Gets the syntax error and expected arguments
14
public get syntaxError(): {[key: string]: string};
15
public get expectedArgs(): string | undefined;
16
17
// Returns what Discord permissions are assigned
18
public get requiredPermissions(): permissions | undefined;
19
20
// Gets the cooldown value, duration type, and full string
21
public get cooldownDuration(): number;
22
public get cooldownChar(): string;
23
public get cooldown(): string;
24
public get globalCooldown(): string;
25
26
// Will ensure the cooldown string is valid
27
public verifyCooldown(cooldown: string, type: string);
28
29
// Is this command test only?
30
public get testOnly(): boolean;
31
32
// Is this command guild only?
33
public get guildOnly(): boolean;
34
35
// Is this a hidden command?
36
public get hidden(): boolean;
37
38
// Is this a slash command?
39
// The string will be "both"
40
public get slash(): boolean | string;
41
}
Copied!
Copy link