Creating your first command

Remember the path we set when creating the client? Navigate to your commands folder, or create it. In that folder create a new JS file. You will create your first command there, exited?

GCommands support two ways to create commands. You can use new Command({ ...settings }) or new class extends Command. We'll demonstrate both cases, and we'll discuss the advantages and disadvantages.

const { Command, CommandType } = require('gcommands');

// Create a new command with the name 'hello'
new Command({
	name: 'hello',
	description: 'Says hello!',
	// GCommands Next offers different types of commands, we will only use slash and message commands here.
	type: [CommandType.SLASH, CommandType.MESSAGE],
	// The function thats executed when the user uses the command.
	run: (ctx) => {
		return ctx.reply(`Hello ${ctx.user.username}!`);
	}
});
1
2
3
4
5
6
7
8
9
10
11
12
13
const { Command, CommandType } = require('gcommands');

// Create a new command with the name 'hello-class'
new class extends Command {
	constructor() {
		super({
			name: 'hello',
			description: 'Says hello!',
			// GCommands Next offers different types of commands, we will only use slash and message commands here.
			type: [CommandType.SLASH, CommandType.MESSAGE],
		});
	}

	// The function thats executed when the user uses the command.
	run(ctx) {
		return ctx.reply(`Hello ${ctx.user.username}!`);
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

The advantage of the new style is that you have straight typings for the run function. In the case of the class command, you have to add jsdocs at the run function to have auto-complete. The new style is also cleaner, shorter to type.

You insert normal code into the run function as you would with discord.jsopen in new window, except that you use ctx instead of message or interaction.
The ctx is just a variable that you can rename however you want, or you can import just certain functions using { reply, user } instead of ctx.
You may find that you don't find some parameters like author. Don't despair, just use user instead of author.

When you want to define a client, you can use this.client in case of class, or just use ctx.client.

TIP

Creating commands with new was introduced in GCommands Next with the main purpose of making your code look more clean! You can use the method your prefer.

Restart your bot, and you should see the hello slash command pop-up in your dev-server. Go ahead and give it a try!

GCommands used /hello
Guide Bot Bot 05/15/2022
Hello S222em!

You can also use the message command

User05/15/2022
.hello
Guide Bot Bot 05/15/2022
Hello S222em!