Writing a Discord bot 03-05-2016, 03:00 AM
#1
Heya,
I'm going to teach you how to make a simple bot for Discord.
Prerequisites:
Pictures, where existent, may look blurry because they're taken from my RDP.
First of all, make an empty folder somewhere on your computer.
This'll be your workspace, now in that folder, create a file called package.json
Inside of that, you'll have to paste the following contents, edit these to your needs.
Now create a new javascript file called bot.js
This will be the file where we're gonna do the coding.
Now, shift right click somewhere in the empty folder, and select Open command window here. CMD will open, now you'll need to install some dependencies.
Enter these commands:
![[Image: ZcTJjAo.png]](http://i.imgur.com/ZcTJjAo.png)
After you've installed those 2 dependencies, you'll notice a new folder, called node_modules has been created. This is where the dependencies are saved.
Now we can start coding!
Create a new file, called auth.json, this is where we're going to save your credentials.
Open it and enter this:
Open bot.js with your favorite IDE.
At this point, you'll need the credentials of your discord account, you can create one specially made for being a bot.
To run the bot, just run node bot.js in CMD in your workspace.
This is obviously a very simple example, but you can do some pretty cool stuff with discord.io.
Anyway, that was it for this simple 'tutorial' (more like snippet, but whatever
Here's a picture of the bot in action.
![[Image: vDIwQtb.png]](http://i.imgur.com/vDIwQtb.png)
![[Image: rLNBJAg.png]](http://i.imgur.com/rLNBJAg.png)
Also, please invite me to the Discord server
I've requested an invite some time ago, but never received one.
Also, shit wrong section
I'm going to teach you how to make a simple bot for Discord.
Prerequisites:
- Node.js installed
- Basic coding knowledge
- Discord account
Pictures, where existent, may look blurry because they're taken from my RDP.
First of all, make an empty folder somewhere on your computer.
This'll be your workspace, now in that folder, create a file called package.json
Inside of that, you'll have to paste the following contents, edit these to your needs.
Code:
{
"name": "Discord Bot",
"version": "1.0.0",
"description": "",
"main": "bot.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "",
"dependencies": {}
}
Now create a new javascript file called bot.js
This will be the file where we're gonna do the coding.
Now, shift right click somewhere in the empty folder, and select Open command window here. CMD will open, now you'll need to install some dependencies.
Enter these commands:
Code:
npm install discord.io --save
npm install winston --save
![[Image: ZcTJjAo.png]](http://i.imgur.com/ZcTJjAo.png)
After you've installed those 2 dependencies, you'll notice a new folder, called node_modules has been created. This is where the dependencies are saved.
Now we can start coding!
Create a new file, called auth.json, this is where we're going to save your credentials.
Open it and enter this:
Code:
{
"email": "YOUR_EMAIL",
"password": "YOUR_PASSWORD"
}
Open bot.js with your favorite IDE.
At this point, you'll need the credentials of your discord account, you can create one specially made for being a bot.
Code:
//First of all, we need to load the dependencies we downloaded!
var logger = require("winston");
var Discordbot = require('discord.io');
//Let's change some settings!
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, {
colorize : true
});
logger.level = 'debug';
//The auth file is important as well!
var auth = require("./auth.json");
//Here we create our bot variable, this is what we're going to use to communicate to discord.
var bot = new Discordbot({
email : auth.email, //<-- This is the email from your auth file.
password : auth.password,//<-- This is the password from your auth file.
autorun : true
});
bot.on("ready", function (rawEvent) {
logger.info("Connected!");
logger.info("Logged in as: ");
logger.info(bot.username + " - (" + bot.id + ")");
});
//In this function we're going to add our commands.
bot.on("message", function (user, userID, channelID, message, rawEvent) {
if (message.substring(0, 1) == "!") {
var arguments = message.substring(1).split(" ");
var command = arguments[0];
arguments = arguments.splice(1);
if (command == "ping") {//If the user posts '!ping' we'll do something!
bot.sendMessage({ //We're going to send a message!
to : channelID,
message : "Pong!"
});
}
}
});
To run the bot, just run node bot.js in CMD in your workspace.
This is obviously a very simple example, but you can do some pretty cool stuff with discord.io.
Anyway, that was it for this simple 'tutorial' (more like snippet, but whatever
Here's a picture of the bot in action.
![[Image: vDIwQtb.png]](http://i.imgur.com/vDIwQtb.png)
![[Image: rLNBJAg.png]](http://i.imgur.com/rLNBJAg.png)
Also, please invite me to the Discord server

I've requested an invite some time ago, but never received one.
Also, shit wrong section