DEV Community

Vojta
Vojta

Posted on

6

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
Scopes
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
cat
(image by Tuqa Nabi on Unsplash)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (2)

Collapse
 
zuwie profile image
RS_Webdev •

Quick and easy, thank you!

Collapse
 
mehanalavimajd profile image
mehan •

Really helped me thank you!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more