Skip to content

Instantly share code, notes, and snippets.

@lewisakura
Last active August 12, 2022 06:40
Show Gist options
  • Save lewisakura/db5513259e00e2087ad23ab2e12bec22 to your computer and use it in GitHub Desktop.
Save lewisakura/db5513259e00e2087ad23ab2e12bec22 to your computer and use it in GitHub Desktop.
How to make a Discord bot using Eris.

Your first Discord bot

So, you want to get in to writing Discord bots? I'm glad you're here!

Before we begin...

This tutorial will go over how to use node.js, but you still need to have basic knowledge on JavaScript. I'm not a Python or Java bot developer, so if you were looking for tutorials on that you'll need to find another one. Sorry!

Prerequisites

Creating an application

Discord uses 'applications' to create bots. In order to do this, what you will need to do is the following:

  1. Go to https://discordapp.com/developers/applications/me (this is where you can create your application)
  2. Click on 'New App'
  3. Fill out all the details, like name, description, profile picture, etc. (you do not need to do Redirect URI!)
  4. Click on 'Create App'
  5. If you have not been loaded into your application's settings, click on your application
  6. Click on 'Create a Bot User'
  7. Click on 'Yes, do it!'
  8. Congratulations, you have made a bot user account!
  9. Click on 'click to reveal' where it says 'Token:' and copy this down for later.

Done! You have created an application, added a bot account to it, and got the token for it! We can now get started.

Coding your first bot

So, I will teach you how to code a Discord bot. Since I use Eris to write my bots, I will teach you how to use Eris. If you want a discord.js tutorial, just let me know by contacting me over at LewisTehMinerz#6312.

Anyhow, lets get started.

The first thing you'll need to do is install node.js (click the link to go to their website). After that, you can get started.

package.json

This tutorial covers everything, so lets get started with creating a package.json.

Create a new folder. In that folder, open up a command window, and type in npm init. It will ask you a few questions. Just keep going. If you don't want to answer any of them, just press enter and it will skip it.

You should now notice a file called package.json in your directory. This contains your metadata for your bot, so keep it safe and make sure you don't break it.

Installing Eris

Now, we can install Eris.

Simply run the command npm install eris in the folder with your package.json. It will download and install Eris for you, and save it in your package.json so that you can move to a server and run npm install to install your bot's dependencies.

Creating your first script

Before we get into coding, we need to create a file.

Name this file index.js. This will be your main bot script. Open it up in a text editor (not Notepad!) and get prepared to do some coding.

You will now notice that this file is blank. Let us change that. Write the following snippet in your file:

const Eris = require('eris');

This code means 'I want to load module eris and put it in the variable Eris that cannot be changed' (const = constant). This is how you import modules in node.js.

Now that we've got that code in, we can write:

const bot = new Eris('bot token'); // put your bot token in here that you got from earlier

This code means 'Create a new copy of Eris with your bot token, and store it in the constant variable bot'. This allows you to get started on your adventure to coding a bot.

We can write this snippet:

bot.on('messageCreate', function(message) {

});

which means 'Whenever there is a message, run the code in this function with the parameter message'.

We can now use this to our advantage and start creating actions.

Let us create a very basic ping-pong command.

Write the following code in the function:

if (message.content == '!ping') {
  message.channel.createMessage('Pong!');
}

This means 'If the message that was sent was !ping, reply with Pong!'

One final thing:

bot.connect();

Put this line at the end of the file. This tells the bot to connect to Discord.

Congratulations, you have made your very first Discord bot!

Try it out! Run the script and it should run fine. If there is an error, double check everything. If there aren't any errors, invite your bot! Go back to the application section and copy the Client ID. Use Discord's OAuth2 URL Generator and paste in your Client ID, and select bot as a scope. Copy the link that is generated, open it up in your browser, and select your server. Once you have invited it, type in !ping and the bot should reply Pong!.

If everything goes well, you have now officially created your first Discord bot. You can now expand off of this simple script file and create larger bots. If you create a really good bot, you can get loads of people using your bot!

Thanks

Thanks for reading my tutorial! If you need any more help, feel free to ask over in the DBL community's #development channel. I might pop in there sometimes, so make sure you're paying attention!

Created by LewisTehMinerz#6312

@Rubayz
Copy link

Rubayz commented Jun 10, 2021

Can you show us how to add command handler on eris and work on different folders?

@lewisakura
Copy link
Author

@Rubayz this is a very old and outdated post. I'd recommend reading a different tutorial.

@DET171
Copy link

DET171 commented Jul 20, 2021

https://dev.to/canaris/make-a-discord-bot-with-eris-phi
Here's a guide I made...
would be helpful if you could give me some suggestions?

@BDT-4248
Copy link

https://dev.to/canaris/make-a-discord-bot-with-eris-phi Here's a guide I made... would be helpful if you could give me some suggestions?

I personally don't use yuuko at all so it'd be helpful if you add the actual eris part :/
Cause i was a bit disappointed when i didn't see the word "eris" in any code examples provided

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