Creating your first command
Creating a new command
Let's start by creating a new file in your commands directory and initializing a new class extending from the Command
class.
const { Command } = require('gcommands');
module.exports = class extends Command {}
// or
const { Command } = require('gcommands');
class Hello extends Command {}
module.exports = Hello;
2
3
4
5
6
7
8
9
10
This creates a new class extending from the Command
class, and exports it for use.
Setting the name and description of the command
Next we need to set the name
and description
of the command, we can do this by using the constructor()
and super()
. You can also create new CommandOptions
by using the CommandOptionsBuilder
, explained here.
const { Command } = require('gcommands');
module.exports = class extends Command {
constructor(client) {
super(client, {
name: 'hello', // Set the name of the command
description: 'Hello!', // Set the description of the command
});
}
}
2
3
4
5
6
7
8
9
10
Responding to the command
Now we need to actualy respond to the user. We can do this by creating the run()
function in our command.
WARNING
Ephemeral messages only work on slash commands.
run({ respond, author }) {
respond(`Hello **${author.tag}**!`); // Send a response
}
2
3
The respond
function allows you to send responses with message, slash and context menu commands.
The respond function works the same way as TextBasedChannel.send
function but has more options. You can find them hereopen in new window
Resulting code
const { Command } = require('gcommands');
module.exports = class extends Command {
constructor(client) {
super(client, {
name: 'hello',
description: 'Hello!',
});
}
run({ respond, author }) {
respond(`Hello **${author.tag}**!`);
}
}
2
3
4
5
6
7
8
9
10
11
12
13