How to make a twitch bot (twytch)

Ben Matare
5 min readApr 12, 2018

--

What is Twitch?

Twitch was launched in 2011 as a video-game live streaming focused system. The primary focus of twitch was to bring together people to watch other play video games. Both in the casual light, as well as the hardcore and professional.

Over the years, Twitch’s popularity would grow exponentially and in October of 2013 the site boasted 45 million unique viewers — and in February of 2014 it was considered the 4th largest source of Internet traffic in the USA. Soon afterwards in August — Twitch was acquired by Amazon and to this day has continued to grow and innovate and integrate with it’s parent company.

Twitch’s channel layout allows you to search and browse through games and channels to find the content you’re looking for!

The UI for finding a channel to watch!

Inside a particular channel you are sent to the channel page, as shown below. You have the actual broadcast, and then adjacent (on the right hand side) you have the chatroom. A treasure trove of spam and human interaction.

The UI of the channel with the chatroom on the right!

Let’s build a Twitch chat bot!

The reason I wanted to build a chat bot, was part exercise-part trolling. As any long time twitch viewer will tell you the culture on twitch is a beast of it’s own with a wonderful affect of copypasta being spammed in chat from time to time.

copypasta (kɒpiːpeɪstə) noun. 1. A block of lengthy text repeatedly copy-pasted in an online forum or chat room. 2.(On twitch.tv) Lengthy text that is mindlessly copy-pasted repeatedly in Twitch Chat, often to make fun of something through satire and repetition.

What do we need to get started?

Node.js — https://nodejs.org/en/

Tmi.js — https://www.tmijs.org/

Twitch OAUTH token — http://twitchapps.com/tmi/

Go ahead and download the most recent version of Node.js, as well as reading over the documentation over at Tmi.js (Twitch Messaging Interface) afterwards, create a directory and run npm init and follow the prompts and enter some information that is applicable to your Twitch bot.

npm init
The resulting package.json file

Afterwards, open your file directory in your IDE, and create a new app.js file, then go back into your package.json file and edit the main key and change it to app.js

Changed the “main” & “author” fields

Now go ahead and run the Install — Node instructions from https://docs.tmijs.org/

run npm i tmi.js — save

Once this process is done, you can begin writing the initial code to connect your bot to the twitch account and twitch channel(s) you’d like your bot to live!

You will create the same code as featured below, and can review the tmi.js documentation on what you need and/or what you don’t need to include!

app.js file with updated data, and we pass it into the client variable, and invoke the connect method to actually connect!

Once that is all setup — you’re good to run node app.js and test the connection!

run node app.js to test your connection!

*As always — keep your eye out for errors! Here I forgot the :’s after connection but I got a wonderful syntax error so I know where and how to fix!

Watch out for errors!

You’re connected!

As you can see, we successfully connected to both channels and we’re getting fed back actual chat messages!

Just to verify that the connections are real, and all is dandy — I went ahead and switched to my main account and logged into both of those channels and verified via the Viewer List that my klundike_jr account is live in those channels!

We’re alive!
Again we’re alive on both channels we declared in our app.js file!

Now that we have verified that our bot is alive and well on both channels, we can begin adding some functionality to it! Because currently, it just sits in the chatroom padding the viewer list.

TMI.js has wonderfully documented a bunch of events our bot can respond to (essentially they are event listeners) to which we can then call functions!

Final code!

So I decided to pick random quotes from TwitchQoutes.com and store them into an array, then have it go through the array and ‘randomly’ pick a message to then ping back out to the twitch channel!

The final product!

That is how you create a very simple Twitch bot! I do not condone the usage of Twitch bots to spam channels, or to boost viewer numbers! I do however hope you use this as a guide to explore the cool and interesting things you can do with code! It’s funny to think about, but prior to this coding journey I thought the people who made twitch bots were super geniuses, but it’s actually not that intense or hard to do!

Sources:

  1. https://www.youtube.com/watch?v=K6N9dSMb7sM Highly recommend you watch this, as it’s the code I used in creating this app!
  2. https://www.tmijs.org/ Awesome documentation and another reason why the tech community is awesome
  3. https://en.wikipedia.org/wiki/Twitch.tv
  4. https://www.twitch.tv The best place to watch video games online!
  5. https://www.twitchquotes.com/ Great place for quality memes and copypasta!

--

--