Skip to content

How to develop a Discord bot using Discord.js

Raymond Hsu edited this page Nov 30, 2019 · 2 revisions

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.

Getting Started

Before we start, you need to install: Visual Studio Code and Node.js. Once you're done, let's CODE!!111!!1!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.

  2. 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.

  3. Make a folder for your bot to save files.

  4. 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)

  1. Open Visual Studio Code, and click "Open Folder", then select the folder you've just created for the bot.

  2. Create file names: config.json, index.js.

  3. 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"
}
  1. 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
  1. 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!")
  }

}
  1. 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.

Recommendations

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.

Clone this wiki locally