How To Make a Simple Discord Bot in JavaScript [2022]
Tags: javascript, discord, beginners, node
Published: 2022-02-05 13:07:13

## What Will You Need

- [Visual Studio Code](https://code.visualstudio.com/) (or any other code editor)
- [Node.js](https://nodejs.org/) 16.6.0 or newer
- [Discord Account](https://discord.com/register)

## 1) Make a New Discord Application

Go to the [Discord Developer Portal](https://discord.com/developers/applications) and click on **New Application**
![Discord Developer Portal](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n6lcdpo9j8s23ckmaomu.png)

Name it and hit **Create**
![Create Application](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tc9wi2o9p6kifhdlvdlp.png)

Now let's go to the **Bot** section and **Add a Bot**
![Add a Bot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tz69hmzfylbfsku9uop1.png) 

## 2) Invite The Bot to Your Server

Go to the **URL Generator**, and select **bot** and **Administrator**
![URL Generator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yuitmxh8qsltysm2aiqk.png)
![Scopes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jtmzjyj0n3l4qr3m1ma.png)
![Bot permissions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ent5ejo916ljuqjusfk.png)
 
**Copy** the URL at the bottom of the page and open it in a new tab
![Copy URL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tel52m8qfrap6f4oifpv.png)

You should see something like this:
![Connect bot to Discord](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8p40tvfg8fsqhj4py6t.png)
Select a server you want to add the bot to, and click **Continue** and **Authorize**

The bot has joined the server, hurray :tada:
![Bot has joined the server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9epn5v97tyhfq48f0p9k.png)

## 3) Add functionality to the bot

First, you'll need to go back to [Discord Developer Portal](https://discord.com/developers/applications), go back to the **Bot** section, and **Copy** the bot token (here you can also name the bot and add a profile picture)
![Copy bot token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbh4flptsqgudd2d4afy.png)
The bot token is like a password for the bot, so **DON'T SHARE IT**!

Now create a new folder for the project if you haven't done that yet and open it in VSCode

We're gonna need to install discord.js first, which you can do in the integrated terminal (ctrl+J, select Terminal).

Type these two commands into it, and that's it
```
npm init -y
npm i discord.js
```

Now create a new file (bot.js) and paste this in (don't forget to replace 'your token' with the bot token) :
```js
const { Client } = require('discord.js');

const client = new Client({
  intents: ['GUILDS', 'GUILD_MESSAGES'],
});

client.once('ready', () => console.log('Ready!'));

client.on('messageCreate', (message) => {
  if (message.content.includes('dn')) {
    return message.reply('deez nuts haha gotem');
  }

  if (message.content === '!help') {
    return message.reply("there's no help lmao");
  }
});

client.login('your token');
```

Run this using
```
node bot.js
```

And now you can see the result:
![Deez Nuts showcase](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvlt587gyla3rhlkdgqu.png)

## Tutorial Completed!
here, have a cat as a reward
![cat](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eactfn9pz7ktf23whf57.png)
(image by Tuqa Nabi on Unsplash)