How To Make a Simple Discord Bot in JavaScript [2022]
Vojta
Posted on February 5, 2022
What Will You Need
- Visual Studio Code (or any other code editor)
- Node.js 16.6.0 or newer
- Discord Account
1) Make a New Discord Application
Go to the Discord Developer Portal and click on New Application
Now let's go to the Bot section and Add a Bot
2) Invite The Bot to Your Server
Go to the URL Generator, and select bot and Administrator
Copy the URL at the bottom of the page and open it in a new tab
You should see something like this:
Select a server you want to add the bot to, and click Continue and Authorize
The bot has joined the server, hurray 🎉
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)
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) :
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:
Tutorial Completed!
here, have a cat as a reward
(image by Tuqa Nabi on Unsplash)
Posted on February 5, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.