DEV Community

IntegraterGithub
IntegraterGithub

Posted on

Make a basic discord bot in discord.js

Creating the bot

Go to the discord developer portal and click on create new application. Now choose the name and click create. Now click on Bot and click add bot, next click yes, do it.

Inviting the bot  

To invite the bot, click on oauth2, then choose the bot scope now scroll down and give the bot the admin permission. Now copy the link and paste it into your browser and you will be able to invite it to any server you own.

Basic commands

Open up your text editor of choice and execute


npm init -y && npm i discord.js 

Enter fullscreen mode Exit fullscreen mode

Explanation: npm init -y creates our package.json and npm i discord.js installs the discord.js libary in order to interact with the discord api.
Now create a file called index.js and paste in the following code:


var discord = require("discord.js"); // imports the discord.js libary
var client = new discord.Client(); // this is the main way we interact with the discord api   
client.on('ready', () => { // executes when the bot is online
    console.log("Bot online")
})
client.on("message", (msg) => { // executes on a new message
    if(msg.content === "!ping"){
        msg.reply("pong")
    }
})
client.login("your-token") // replace "your-token" with your bot token from https://discord.com/developers/applications

Enter fullscreen mode Exit fullscreen mode

Now replace "your-token" with your bot token from the discord developer portal. Next, run the following command:


node index.js

Enter fullscreen mode Exit fullscreen mode

You should see in the console: Bot online and if you send !ping in your discord server with the bot you'll see the bot reply with pong.

More tutorials coming soon!

Top comments (0)