Programming a JavaScript bot for Twitch

I have been programming different chat bots for twitch for about 1.5 years now and its been an amazing journey for learning to code.
Bots I have made include Smart Chat bot, Stranded Deep Wiki, Cyberdyne Systems Skynet, and 7 days to die bot. Each of these bots have have been an adventure of thinking outside the box to add unique features.

In this article I will show you how to make your own chat bot for twitch.

At this point you might want to make a folder on your computer dedicated to the chat bot stuff, and have documentation, downloads, and all the info in there. This will help a ton when you need to work on it more later.

Start by downloading Node.js and installing the LTS current version for windows. You can now run bots within a terminal window.

Now lets work on getting the bot accounts setup.

Select your profile on the top right then choose settings
Click on Security and Privacy then check the boxes for additional account creation.

Now log out and make a new twitch account for the chat bot to use. It can be a major security issue if you use the streaming account for the bot, so its best to make a dedicated account.

You can skip the last step if you have an additional email you want to use for the bot, but then you have to verify the email and phone so it adds more steps in the end.

Now that you have a new account for the bot using the same email, you can log into it and now we can generate a OAUTH key that you will use for the bot. This website will give you the OAUTH key and you can save this. Do not show or tell anyone this key or they can use your account and post things.

Generate Oauth Token on this website.

If you want to review the basics of the chat bot from twitch then you can check out this page.

Installing node.js was one of the first things we did, so now its time to use it to install tmi.js.

Click Start, type CMD and click enter (In windows 7 you need to click start, run, CMD)

npm install tmi.js

This will install the tmi.js that is needed for the bot to run. This contains all the code that runs behind your bot.

Now we can start to code our bot.

There are many code editors such as Notepad++, Visual Studio, Sublime Text, and many others. I like to use the free Notepad++.

Make a new folder in your bot stuff for the actual bot code to run inside of. Create a document called bot.js (Or whatever you want to call the bot file, as long as it ends in .js) This is the file that will contain all your bot code.

Make a new file called start.bat in the same folder as bot.js and use notepad++ (Or your other editor) and add the following to it.

node bot.js

Then save. You can use this start.bat file to double click and it will open a CMD window, load node and open the bot. All automatic! You can copy start.bat and paste a shortcut in your computer startup folder so it can start the bot when your computer boots.

The windows 10 startup folder is located at:

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

Just paste a shortcut to the bat file in the startup folder and you are set. The bot should load when you boot the computer. Yes, this means your computer has to be on 24/7 if you want the bot to be always on. But this is also great if you only want the bot when you boot your computer to stream.

The other way to open the bot would be doing it manually in the CMD window. But since our bot has no code, lets talk about that later.

Lets start with the sample code that twitch gives on their developer website.

const tmi = require('tmi.js');

// Define configuration options
const opts = {
  identity: {
    username: <BOT_USERNAME>,
    password: <OAUTH_TOKEN>
  channels: [

// Create a client with our options
const client = new tmi.client(opts);

// Register our event handlers (defined below)
client.on('message', onMessageHandler);
client.on('connected', onConnectedHandler);

// Connect to Twitch:

// Called every time a message comes in
function onMessageHandler (target, context, msg, self) {
  if (self) { return; } // Ignore messages from the bot

  // Remove whitespace from chat message
  const commandName = msg.trim();

  // If the command is known, let's execute it
  if (commandName === '!dice') {
    const num = rollDice();
    client.say(target, `You rolled a ${num}`);
    console.log(`* Executed ${commandName} command`);
  } else {
    console.log(`* Unknown command ${commandName}`);

// Function called when the "dice" command is issued
function rollDice () {
  const sides = 6;
  return Math.floor(Math.random() * sides) + 1;

// Called every time the bot connects to Twitch chat
function onConnectedHandler (addr, port) {
  console.log(`* Connected to ${addr}:${port}`);

Paste the above code in your bot.js file.
Then change the <BOT_USERNAME> with the bot twitch name, <OAUTH_TOKEN> with the token you generated earlier,
and under channels replace <CHANNEL_NAME> with your channel.
All 3 of those need to have single quote marks , ‘bot name’, ‘oauth’, ‘channel name’.
You can also add a comma and add the bots channel too if you want the commands to work there also.

Example: (Shows My_Chatbot login, and it will join 3 channels including itself.)

const opts = {
  identity: {
    username: 'My_Chatbot',
    password: 'p34897324nhiodsf782344rnobc8v'
  channels: [

Now save and run the start.bat file we created. You should see a CMD window open and show its connecting to the twitch. Now go to your channel and type !dice to see if it works.

Now that you have a running bot, I will show how to manually load it, if you do not want to use the bat file we created earlier.

Click on start, CMD, and hit enter. Then navigate to the folder that you have the bot in using: cd c:\folder\folder

Then you type: node bot.js and hit enter

Next article shows how you can change the commands in JavaScript to add new features to the chat bot. See how to code your own commands on my next article / Part 2 here.