Initializing the Object
This example includes how to initialize the Commands Object

Basic initialization

1
// Regular Discord.js Stuff
2
const Discord = require("discord.js");
3
const client = new Discord.Client({ shardCount: 1 });
4
5
// Now we initialize the Object
6
const { Commands } = require("djs-command-control");
7
const commands = new Commands(
8
client, // Passing our Discord Client
9
"src/commands", // Lets pretens our commands are located in src/commands
10
{ // Finally passing the CommandOptions Object
11
prefix: process.env.PREFIX, // We have our prefix stored in the env variables
12
prefixOnMention: true, // Yes, we want to return the prefix if someone mentions the bot
13
}
14
);
Copied!

Our bot has a customizable prefix feature

1
// Start by initializing the discord.js client and importing djs-commands-control
2
3
// Let's say we have a function that fetches the prefix from our database
4
// The arguments has to be message and options, as described in the CustomizablePrefix Type
5
function fetchPrefix(message, options) {
6
return options.database.fetchPrefix(message.id); // This has to return a string
7
}
8
9
// Let's create a CustomizablePrefix Option
10
const customizablePrefix = {
11
callback: fetchPrefix, // The above function to run
12
options: { database } // The function requires the database object
13
}
14
15
// Now we initialize the Object
16
const commands = new Commands(
17
client, // Passing our Discord Client
18
"src/commands", // Lets pretens our commands are located in src/commands
19
{ // Finally passing the CommandOptions Object
20
prefix: process.env.PREFIX, // We have our prefix stored in the env variables
21
prefixOnMention: true, // Yes, we want to return the prefix if someone mentions the bot
22
customizablePrefix, // Finally, give it to the contructor
23
}
24
);
Copied!

We do not want to have general as the default category

1
// Start by initializing the discord.js client and importing djs-commands-contro
2
3
// Let's create a defaultCategory Option
4
const defaultCategory = {
5
name: "something-else",
6
title: "Something Else"
7
}
8
9
// Now we initialize the Object
10
const commands = new Commands(
11
client, // Passing our Discord Client
12
"src/commands", // Lets pretens our commands are located in src/commands
13
{ // Finally passing the CommandOptions Object
14
prefix: process.env.PREFIX, // We have our prefix stored in the env variables
15
prefixOnMention: true, // Yes, we want to return the prefix if someone mentions the bot
16
defaultCategory , // Finally, give it to the contructor
17
}
18
);
Copied!

We wish to include a help command that is part of our new default category

1
// Start by initializing the discord.js client and importing djs-commands-contro
2
3
// Let's create a helpOptions Object
4
const helpOptions = {
5
include: true, // yes we want it included
6
omitFromHelp: true, // no need to show the help commands inside of the help command, helpception.
7
category: "something-else" // The name of the category we want the help command to appear in
8
}
9
10
// Now we initialize the Object
11
const commands = new Commands(
12
client, // Passing our Discord Client
13
"src/commands", // Lets pretens our commands are located in src/commands
14
{ // Finally passing the CommandOptions Object
15
prefix: process.env.PREFIX, // We have our prefix stored in the env variables
16
prefixOnMention: true, // Yes, we want to return the prefix if someone mentions the bot
17
help: helpOptions, // Finally, give it to the contructor
18
}
19
);
Copied!

We don't want people spamming the bot, so let's add a ratelimiter

1
// Start by initializing the discord.js client and importing djs-commands-contro
2
3
// Let's create a rateLimiterOptions Object
4
const rateLimiterOptions = {
5
enabled: true, // yes, we want to use the ratelimiter
6
amount: 2, // Lets say we want to allow 2 commands in a timeframe
7
interval: 6000 // Lets make said timeframe 6 seconds
8
}
9
// Now we have a ratelimiter that will allow 2 commands every 6 seconds
10
11
// Now we initialize the Object
12
const commands = new Commands(
13
client, // Passing our Discord Client
14
"src/commands", // Lets pretens our commands are located in src/commands
15
{ // Finally passing the CommandOptions Object
16
prefix: process.env.PREFIX, // We have our prefix stored in the env variables
17
prefixOnMention: true, // Yes, we want to return the prefix if someone mentions the bot
18
rateLimiter: rateLimiterOptions , // Finally, give it to the contructor
19
}
20
);
Copied!
Last modified 4mo ago