DEV Community


Posted on


How To Make a Simple Discord Bot in JavaScript [2022]

What Will You Need

1) Make a New Discord Application

Go to the Discord Developer Portal and click on New Application
Discord Developer Portal

Name it and hit Create
Create Application

Now let's go to the Bot section and Add a Bot
Add a Bot

2) Invite The Bot to Your Server

Go to the URL Generator, and select bot and Administrator
URL Generator
Bot permissions

Copy the URL at the bottom of the page and open it in a new tab
Copy URL

You should see something like this:
Connect bot to Discord
Select a server you want to add the bot to, and click Continue and Authorize

The bot has joined the server, hurray 🎉
Bot has joined the server

3) Add functionality to the bot

First, you'll need to go back to Discord Developer Portal, 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
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
Enter fullscreen mode Exit fullscreen mode

Now create a new file (bot.js) and paste this in (don't forget to replace 'your token' with the bot token) :

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');
Enter fullscreen mode Exit fullscreen mode

Run this using

node bot.js
Enter fullscreen mode Exit fullscreen mode

And now you can see the result:
Deez Nuts showcase

Tutorial Completed!

here, have a cat as a reward
(image by Tuqa Nabi on Unsplash)

Top comments (2)

zuwie profile image

Quick and easy, thank you!

mehanalavimajd profile image

Really helped me thank you!

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await