Main File
The main file for running your bot.
Our main bot file is the file that contains our function to start our bot. For the sake of this tutorial, we will name it index.js.
Inside your source directory, create a new file called index.js
Open this file and we can begin coding our bot.
💿 Basic Code
The first thing we are going to do is load our environment variables, aka, our bot token. To do this, we need to add the following line to the very top of our index.js:
const config = require("../config.json");This will load our variables for our entire bot.
Next, we want to require Discord.js and CommandKit, so our file should now follow this structure:
const config = require("../config.json");
const { Client, GatewayIntentBits } = require("discord.js");
const { CommandKit } = require("commandkit");We can also save our token as a variable:
const config = require("../config.json");
const { Client, GatewayIntentBits } = require("discord.js");
const { CommandKit } = require("commandkit");
const token = config.token;Continuing, we need to make sure our bot has the correct intents, and utilizing GatewayIntentBits, we can do this.
The following file will now look like this:
const config = require("../config.json");
const { Client, GatewayIntentBits } = require("discord.js");
const { CommandKit } = require("commandkit");
const token = config.token;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
],
});Finally, we can define our CommandKit handler and paths, to do this, we want to create 2 more directories inside our source folder.
Name these directories commands and events, respectively.
We can finally change our index.js to include all of our directories for looking for commands and events, so:
const config = require("../config.json");
const { Client, GatewayIntentBits } = require("discord.js");
const { CommandKit } = require("commandkit");
const token = config.token;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
],
})
new CommandKit({
client,
eventsPath: `${__dirname}/events`,
commandsPath: `${__dirname}/commands`,
bulkRegister: true,
});If you happen to encounter errors with registering commands, change bulkRegister: to false to better point our what is happening and what errors there are.
The last step it to start our bot, so we need to add the line:
client.login(token)All together, our bot file should look like:
const config = require("../config.json"); // Load our environment variables
const { Client, GatewayIntentBits } = require("discord.js");
const { CommandKit } = require("commandkit");
const token = config.token; // Fetch our token
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent,
],
}) // Create a new client with these intents
new CommandKit({
client,
eventsPath: `${__dirname}/events`,
commandsPath: `${__dirname}/commands`,
bulkRegister: true,
}) // Create your CommandKit instance
client.login(token) // Start/Login to your botNow save your file and open your terminal, (Node.js terminal if you are using an editor without a built-in terminal)
We can now run node source/index.js, and this will start our bot. You will not get any message in console but you can always invite the bot to a server on Discord, and check to see if it is online that way.
To close your bot, press Ctrl+C or close the terminal.
Now we can move on to our ready event listener.
ReadyLast updated