-
Notifications
You must be signed in to change notification settings - Fork 0
How to develop a Discord bot using Discord.js
Well, this project uses Discord.js, I can teach you how to make commands.
In this tutorial, we will be using Visual Studio Code to develop the tutorial bot.
Before we start, you need to install: Visual Studio Code and Node.js. Once you're done, let's CODE!!111!!1!1
-
Go to Discord Developer Portal, and create an application by clicking on the "New Application" button and name it to something. Then head in the application, and click the "Bot" section then create a bot by clicking the "Build-A-Bot" button and name your bot.
-
Go to the "General Information" section, and copy the client ID, then go to Discord Permissions Calculator and paste your client ID in the placeholder named "Client ID", then click the link generated below. On the authorize page, select your server and click the "Authorize" button.
-
Make a folder for your bot to save files.
-
Open your Command Prompt (or Terminal for mac), enter the code:
mkdir FOLDER_NAME && cd FOLDER_NAME
Then, enter the code:
npm init
You don't need to enter anything the thing asks, just hit enter until done. Now, open your Visual Studio Code. (don't close your window yet! we will use this later)
-
Open Visual Studio Code, and click "Open Folder", then select the folder you've just created for the bot.
-
Create file names:
config.json
,index.js
. -
Go back to your application in Discord Developer Portal, and go to "Bot" section once again... Copy the bot token, and head to your file
config.json
, and paste in the code:
{
"token": "REPLACE_YOUR_TOKEN_HERE"
}
- In order to make your code work, we still require NPM packages for it to run! So we will need to run the code in the command prompt:
npm install discord --save
- Head to Visual Studio Code, go to your file
index.js
, paste in the code below: (it's just an example code)
// Global Definitions
const Discord = require('Discord.js');
const client = new Discord.client;
const token = require('./config.json')'
const prefix = "!"
// Console Logging + Automations
client.once("ready", () => {
console.log("Ready!");
console.log("The bot is currently on " + client.guilds.size + " severs, and will be serving " + client.users.size + " users!");
client.user.setActivity("Hello World!", {type: "PLAYING"}) // Makes the bot having a game presence
.then(presence => console.log(`Activity set to "${presence.game ? presence.game.name : "none"}"`))
.catch(console.error);
client.user.setStatus("dnd"); // It could be online, idle, or dnd!
});
// Client Responses
client.on("message", async message => {
if (message.content.startsWith(${prefix}hi) {
message.content.send("hi!")
}
}
- Once you're done, click the "Terminal" button and select "New Terminal", then enter the code:
node index.js
If you want to shut down the bot, simply just hit Control (Ctrl) + C
.
There are a ton of tutorials already on the internet. If you have some code errors that you can't fix, you can ask big coding communities like Stack Overflow or any if you want.
This coding project is owned by Raymond Hsu, and is created on 2019 September.