Sinisterly
Tutorial Writing a Discord bot - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Design (https://sinister.ly/Forum-Design)
+--- Forum: Tutorials (https://sinister.ly/Forum-Tutorials--78)
+--- Thread: Tutorial Writing a Discord bot (/Thread-Tutorial-Writing-a-Discord-bot)

Pages: 1 2


Writing a Discord bot - TheEvilSocks - 03-05-2016

Heya,

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]
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]
[Image: rLNBJAg.png]


Also, please invite me to the Discord server Smile
I've requested an invite some time ago, but never received one.


Also, shit wrong section


RE: Writing a Discord bot - Inori - 03-05-2016

For this case, winston is purely aesthetic, and something more lightweight like chalk could do the job of coloring text just as well (plus, asynchronicity isn't a requirement here). Some additions to this could be using an argument parser like argparse to specify bot activities, or give the user the capability to send messages.

I actually made something really similar a while back, check it out here.

Not that this was bad, but just some tips for writing tutorials in the future:
  • don't spoonfeed readers by giving them the exact code. Instead of giving the command to install npm packages (can be shortened to just npm install socket.io winston), tell them to do that, but not how. Thus they need to teach themselves if they don't already know.
  • Explain your code more thoroughly using comments or breaking it into smaller pieces, so readers know what's going on. Again, this is about helping them learn

Furthermore, adding the following to package.json then running npm install . negates the need for pre-installing dependencies, as well as generates set executables and runs any defined scripts
Code:
"dependencies": { "socket.io": ">=1.4.5", "winston": ">=2.2.0" }

Edit: seeing discord bots pop up again has convinced me to make a functional command parsing bot for discord, so stay tuned


RE: Writing a Discord bot - pvnk - 03-05-2016

Nicely written tutorial there.


RE: Writing a Discord bot - Oni - 03-05-2016

Sent you an invite to the Discord.


RE: Writing a Discord bot - ThePutzer - 02-14-2017

Hey TheEvilSocks,
nice Tutorial you made, but in the section on line "var bot = new Discordbot({" my cmd tells me "Discordbot" is not a constructor. I tried some ideas of fixing it but ended up the same. Can you help me out?
(03-05-2016, 03:00 AM)TheEvilSocks Wrote: //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
});



RE: Writing a Discord bot - Blink - 02-14-2017

Nice, I recommend using Discord.py instead, but whatever floats your boat Wink


RE: Writing a Discord bot - m0dem - 02-14-2017

(02-14-2017, 09:52 PM)Ender Wrote: Nice, I recommend using Discord.py instead, but whatever floats your boat Wink

You should write a tutorial on Discord.py (if there isn't already one)


RE: Writing a Discord bot - Aeolian - 02-14-2017

(02-14-2017, 11:12 PM)m0dem Wrote:
(02-14-2017, 09:52 PM)Ender Wrote: Nice, I recommend using Discord.py instead, but whatever floats your boat Wink

You should write a tutorial on Discord.py (if there isn't already one)

Agreed, currently in the process of creating my own.


RE: Writing a Discord bot - Blink - 02-14-2017

(02-14-2017, 11:12 PM)m0dem Wrote:
(02-14-2017, 09:52 PM)Ender Wrote: Nice, I recommend using Discord.py instead, but whatever floats your boat Wink

You should write a tutorial on Discord.py (if there isn't already one)

Will do. Thanks for the suggestion.


RE: Writing a Discord bot - Mlrkey - 02-23-2017

Interesting, might need this later