Twitch.tv Chat Bot with Node

Writing a Basic Node Twitch Bot

Writing a Twitch.tv chat bot is a lot easier than people make it out to be. There are a lot of functional APIs in various languages that can be easily manipulated to do a user's bidding.

Here are a few to look at:

In this instance we'll be using Node.Js and as the name implies it does mean we will be using javascript. Grab the latest version of Node and get it installed.

Create a project directory for your bot. Just create a folder on your desktop if you're just messing around and wanted to try something simple. Go into the folder and shift right-click to open up a command prompt.

Sub-Menu

Go ahead and type in: npm init

Follow the steps on the command prompt to setup your project.

Setup Dialog

After finishing your setup you'll need to add tmi.js to your project. Head on over to docs.tmijs.org and grab the install code.

Should basically be this: npm i tmi.js --save

Great, your project should be looking similar to this.

Project  Directory #1

So here's what you need to know about your files.

Your package.json file contains all of your configuration and dependencies for your program.

Inside there is an option for scripts. Should read out similar to:
"scripts": { "test": "something goes here" }

Here we can actually help our program a bit. The main file or entry file for our program is listed under "main":. In this instance our main file is called index.js

Inside of scripts we can change the test to something like start. Then inside of the code to the right we can initialize node to open index.js.

"scripts": { "start": "node index.js" }

Once we've done that we can start our program through the command prompt by typing npm start.

Anyway, that's besides the point of this tutorial. Let's just get on with the rest of it.

We need our index.js file to require tmi.js

Let's create a variable and attach tmi.js to it.

var tmi = require("tmi.js");

Great, we're now hooked up to the api. Let's setup our client protocol.

var tmi = require("tmi.js"); var client = new tmi.client({
identity: { password: "oAuth Code Here" }, channels: ["Channel_To_Connect_To"] });

Okay well, what does all of this do. Well it creates a variable that holds the client information we're passing through tmi.client.

The password is an oAuth code which can be generated at a website like TwitchApps.

The channels is an array of channels that we want to connect to. In this instance it's just a single channel.

Once we've got all of that configured. We can move on to connecting. It's super easy.

client.connect();

And now your bot is connected to the Twitch IRC channels you provided.

Now what?

Well you can check out the documentation to see how to handle chat messages and such.

Docs

Here's a quick example of the bot talking as soon as it joins a channel.

client.on("join", function(channel, username, self) { client.say(channel, "I have joined the channel."); });

Pretty easy, play around with it. Have fun with it.