Skip to content

Instantly share code, notes, and snippets.

@rainbownotfound
Created January 23, 2018 00:26
Show Gist options
  • Save rainbownotfound/98963546b6ca6ee23fa4c6689050c462 to your computer and use it in GitHub Desktop.
Save rainbownotfound/98963546b6ca6ee23fa4c6689050c462 to your computer and use it in GitHub Desktop.
Building Your First Discord.js Bot
const Discord = require('discord.js'); // Require discord.js for your bot to communicate with the Discord API
const config = require('./config.json'); // Our config file
const client = new Discord.Client();
client.on('ready', () => { // When bot is online and ready
console.log(`I'm ready!`);
});
client.on("message", message => { // When the bot spots a message
// --------------===== Variables =====-------------- //
if(message.author.bot) return; // Doesn't respond to bots
if(message.content.indexOf(config.prefix) !== 0) return; // Only responding to existing commands that start with "!"
const args = message.content.slice(config.prefix.length).trim().split(/ +/g); // Configure arguments for the commands
const command = args.shift().toLowerCase(); // Detect existing commands
// --------------===== Commands =====-------------- //
if(command === "ping") { // "!ping"
message.channel.send('Pong!'); // Send a message saying "Pong!"
}
});
client.login(config.token); // Log in with the bot token from our config file.
{
"token": "BOT-TOKEN-HERE",
"prefix": "!",
"adminID": "YOUR-ID-HERE"
}
{
"name": "Tutorial Bot",
"version": "1.0.0",
"description": "My First Bot Made With Discord.js!",
"main": "app.js",
"dependencies": {
"discord.js": "^11.3.0"
},
"author": "Your Nickname Here"
}

Building Your First Discord.js Bot

So, you want to create your own Discord bot? But you don't know where to start or how to even create a user? Don't worry, this tutorial will cover the process of building and creating your very own Discord bot!

In order to make a Discord.js bot, you need the following:

  • A Code Editor -> I use Visual Studio Code, you can download it here.
  • Node.js -> You can download it here. Make sure to download the recommended LTS Version
  • A solid understanding of JavaScript.
  • A basic understanding of JSON.
  • A Discord account(obviously)

If the tutorial is too complicated for you, I will leave the source code in seperate files for you to inspect. DO NOT COPY AND PASTE THE CODES IF YOU WANT TO LEARN


Step 1 -> package.json

This file will be processing your bot's dependencies, the package name and version, the license and more. Create a folder where you want your bot to be. I recommend creating it in a location with an easy path, because you will need the folder's location later. Create package.json. In this file, you want to specify the name of your bot, the current version and more. For this tutorial, we will name it "Tutorial Bot". Pretty innovative, right?

{
    "name": "Tutorial Bot",
    "version": "1.0.0",
    "description": "My First Bot Made With Discord.js!",
    "main": "app.js",
    "dependencies": {},
    "author": "Your Nickname Here"
}

This is all you'll need for now. As you probably already noticed, we left the "dependencies" part empty. This will automatically be replaced with all the dependencies you will need and install.


Step 2 -> config.json

This file will hold our bot token, which we can get here. We will take care of the token later in this tutorial. It will also hold the prefix for the bot, and the adminID, for developer commands.

Create a file named config.json. Enter the following code:

{
"token": "BOT-TOKEN-HERE",
"prefix": "!",
"adminID": "YOUR-ID-HERE"
}

You can get your Discord ID by entering the User Settings and enabling Developer Mode: Appearance>Advanced>Developer Mode and turn it on. Right-click your profile in a server and you will see the option "Copy ID". Click it and you will have your ID which you can use for special things.


Step 3 -> app.js

Now, the time has come to actually develop your bot. There are many methods to develop a bot, we will use the most basic method. Create app.js. Enter the following code:

const Discord = require('discord.js'); // Require discord.js for your bot to communicate with the Discord API
const config = require('./config.json'); // Our config file
const client = new Discord.Client();

client.on('ready', () => { // When bot is online and ready
console.log(`I'm ready!`);
});

client.on("message", message => { // When the bot spots a message
// --------------===== Variables =====-------------- //
if(message.author.bot) return; // Doesn't respond to bots

if(message.content.indexOf(config.prefix) !== 0) return; // Only responding to existing commands that start with "!"

const args = message.content.slice(config.prefix.length).trim().split(/ +/g); // Configure arguments for the commands
const command = args.shift().toLowerCase(); // Detect existing commands

// --------------===== Commands =====-------------- //
if(command === "ping") { // "!ping"
    message.channel.send('Pong!'); // Send a message saying "Pong!"
}
});

client.login(config.token); // Log in with the bot token from our config file.

Step 4 -> Creating account and getting bot token

So, finally, we need to create a user account for the bot and get it's token.

Step 4.1 Go to this website. Log in with your own account if needed. Step 4.2 Click on "New app". You will be taken to a page where you can enter the name of your app, and an optional description. New App Step 4.3 Once created, you will be taken to the settings of your app. Underneath the section where you can change the profile picture, there would be a neat button saying "Create a Bot User". Create Bot User Step 4.4 You now have the bot ready to go! Invite it to your server through the OAuth2 URL Generator located right above the Profile Picture section. Now you have it in your server! But we still need the token. Click in your bot section on "Click to reveal Token". Reveal Token

Step 5 -> Installing dependencies and testing

Open a command prompt using Windows + R and entering cmd

Navigate to the location of your app.js file using cd [Path] Now, install discord.js using npm install discord.js Once installed, use the command node app.js and watch the bot go online!

Now, go to the server where you invited your bot in, and send "!ping" in chat and see the bot react with "Pong!"


I surely hope this helped you out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment