Getting started

Installing dependencies

Before continuing make sure you have NodeJSopen in new window v16.6 or higher installed on your machine. You can verify your installation with node -v in your terminal.

npm install gcommands discord.js @discordjs/rest discord-api-types
npm install gcommands@next discord.js @discordjs/rest discord-api-types # for unstable, development next version
yarn add gcommands discord.js @discordjs/rest discord-api-types
yarn add gcommands@next discord.js @discordjs/rest discord-api-types # for unstable, development next version
pnpm add gcommands discord.js @discordjs/rest discord-api-types
yarn add gcommands@next discord.js @discordjs/rest discord-api-types # for unstable, development next version

WARNING

You need at least Node.js version 16.6.0 to use GCommands.

Basic client

We start by creating a master file for the bot we're going to run. We'll call it index.js.

Once created, we import GClientopen in new window from gcommands and initialize it. With this, you will be able to add additional settings for GCommands functionality. GClientopen in new window extends the discord.js clientopen in new window, so you can use all the settings from the discord.js clientopen in new window in GClientopen in new window.

require('dotenv').config();
const { GClient, Plugins, Command, Component } = require('gcommands');
const { GatewayIntentBits } = require('discord.js');
const { join } = require('path');

// Set the default cooldown for commands
Command.setDefaults({
	cooldown: '20s',
});

// Set the default onError function for components
Component.setDefaults({
	onError: (ctx, error) => {
		return ctx.reply('Oops! Something went wrong')
	} 
});


// Search for plugins in node_modules (folder names starting with gcommands-plugin-) or plugins folder
Plugins.search(__dirname);

const client = new GClient({
	// Register the directories where your commands/components/listeners will be located.
	dirs: [
		join(__dirname, 'commands'),
		join(__dirname, 'components'),
		join(__dirname, 'listeners')
	],
	// Enable message support
	messageSupport: true,
	// Set the prefix for message commands
	messagePrefix: '!',
	// Set the guild where you will be developing your bot. This is usefull cause guild slash commands update instantly.
	devGuildId: process.env.DEV_SERVER,
	// Set the intents you will be using (https://discordjs.guide/popular-topics/intents.html#gateway-intents)
	intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});

// Login to the discord API
client.login(process.env.TOKEN);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
TOKEN=your discord bot token from discord.dev
DEV_SERVER=your discord guild id here
1
2

GCommands also detects mention prefixes if you use message commands. However, we strongly recommend that you only switch to interactions that are constantly improving and bring a better and easier experience to your users.

Don't forget to create commands, components, listeners folders. If you don't want all the folders, just delete the folders you don't want in GClient#dirsopen in new window

DANGER

In any case, do not store the token in index.js and preferably not in any configs.js,ts,json. Use the enviroment fileopen in new window. Read more hereopen in new window

WARNING

If you have slash commands, don't forget to invite bot with application.commands!
Example Url: https://discord.com/api/oauth2/authorize?client_id={YOUR_CLIENT_ID}&permissions=0&scope=bot%20applications.commandsopen in new window

That's it! Try running node index.js in your terminal!