Skip to content

1. Getting Started with Discord.js Bots

seanpeters86 edited this page Jun 24, 2016 · 1 revision

So you want to make a bot huh? Well there are lots of different (arguably better) ways of starting your bot, but I'm just going to walkthrough how I decided to do this. Check the README for how to initialize the bot.

var Discord = require("discord.js");
var bot = new Discord.Client();

This is the fundamental part of the bot, and it won't work without these two lines, so be sure to include them at the top of the program.

bot.on("message", function(message) { }

Now this is how I have decided to structure my code. Jarvis reads every message going through the discord server, looking for the specific things inside the brackets. The server I'm using him on is relatively small, so this isn't an issue for me but may cause problems for larger servers depending on how you have the bot hosted.

bot.loginWithToken("XXXXX");

Now that you have the basics set up you can begin to connect your bot with this command. This is using the secure OAuth to connect the bot to the server, and is the preferred method over providing a login/password. To get this code follow the instructions on the discord OAuth page here.

So with this initial setup this is what your code should look like:

var Discord = require("discord.js");
var bot = new Discord.Client();
bot.on("message", function(message) { }
bot.loginWithToken("your token here");
Clone this wiki locally