A beginner's guide to Discord.js

Jacob Squier
5 min readOct 22, 2020

--

https://discord.js.org/#/docs/main/stable/search?q=embed

Discord.js is an API equipped with a massive library to cater to your discord bots needs. This API provides a library of many methods and new code blocks to satisfy you and your bot's curiosity. What I wish to cover in today's article is a beginners' guide on how to install, create, set up your bot, and then send him/her to battle in your very own discord server.

Step 1: Installing the API

The Discord.js API runs on the most recent versions of node, so you don't have to worry about some annoying versioning issues. To get started open your preferred coding environment, open your terminal, and run the installer for the API which is…

npm install discord.js

Step 2: Setting up our environment

Now within our preferred environment create a file named “main.js” this is where the commands will be handled, and this will be the file that runs when we type a bot command in our server. Inside of our main.js file, we need to require the Discord.js API and we do it like so…

const Discord = require(‘discord.js’);

After we require our API, we need to turn the bot on whenever we run our environment. How do we do this, well, simply, we write a simple command as such…

const client = new Discord.Client();

Congrats your environment is set up!!!!

Step 3: Obtaining our bot token

For discord to allow us to add our bot to our server, we must first obtain a token for our bot. What is a token? well, a token is provided by discord it is used to give our bot a unique ID as well as to authenticate our bot. To get a hold of this token, we must go to the Discord developer portal (https://discord.com/login?redirect_to=%2Fdevelopers). If you already have an account with Discord sign in using your Discord login and password if not, then go ahead and register because Discord is fun :). Once logged in navigate here…

Once here go ahead and click “New Application” that is at the top righthand corner. Once clicked a window will pop up prompting you to name your application, make sure to choose wisely because this will actually be your bot's username. When finished it will redirect you here…

While here copy your client id we are going to need that shortly. Then direct yourself to the bots tab…

Once you have made it here click add Bot. It should look like so…

Once here open a new tab. In this tab go here(https://discordapi.com/permissions.html#2146958847). The page should look like so…

Go ahead and past your client ID that you copied EARLIER into the Client ID field. Once the ID is there go ahead and click the link at the bottom of the page. It will redirect you here…

Once click the dropdown box and select the server you would like your newly found friend(your bot) to join. Once that is done It will take you here…

For this tutorial check all the little boxes so that your bot can do everything and, not be restricted on the things we can do with our bot. Once done hurting your fingers click Authorize. Then do the Recaptcha🙄. Then It will get ATHOURIZEDDDD yayyyy!!!

Then we can rush over to our server and see that our bot has Joined!

Now the best part CODINGGGG!!! Navigate your way back to your bots information on the Discord developer portal…

Copy your token were gonna need that for later.

Step 4: The code

Navigate back to your code environment and copy-paste the code below into your preferred environment…

const Discord = require('discord.js');const client = new Discord.Client();const prefix = '!';const fs = require('fs');var version = '1.0.1';client.commands = new Discord.Collection();const commandFiles = fs.readdirSync('./client-commands/').filter(file => file.endsWith('.js'))for(const file of commandFiles){const command = require(`./client-commands/${file}`);client.commands.set(command.name, command);}client.once('ready', () => {    console.log('Sir Peter has risen!')});client.on('message', message => {if(!message.content.startsWith(prefix) || message.author.bot) return;const args = message.content.substr(prefix.length).toLowerCase().split(' ');console.log(args)if(args[0] === 'ping'){message.channel.send('Pong')}})client.login('your token here');

^Make sure to put your token inside the parentheses of the client.login()^

Step 5: Run your bot

In your terminal type node . then go to Discord and type !ping like such…

and your bot will reply with…

Congratulations you made your first bot!!!

--

--

Jacob Squier
Jacob Squier

No responses yet