Video game streaming has taken over a big part of the commercial video game scene with Twitch being its biggest platform. Viewers are able to communicate with other viewers and streamers through the chat, or watch their previous streams or highlights. These highlights are made by streamers creating short clips of their favourite moments either by looking through all of their footage, or marking the time of a potential highlight by using the chat command /mark.
Since a streamer marking a highlight in the chat himself while playing a game is highly improbable and not every streamer has a chat moderator to take that responsibility into his or her hands, there is mostly only the possibility of looking through hours of video footage, to find a few highlights to cut out.
Because of this issue, I had the idea of creating a Twitch Chat Bot, which analyzes and evaluates the chat of a Twitch channel, taking message frequency, length and emotes into account, which is able to detect sudden surges in chat activity and sets a highlight marker accordingly. Also it would be nice to be able to actively track the collected data in a way, which makes it easy and intuitive to understand.
I decided to implement the Bot first, worrying about everything that comes after later. I took a look at different Twitch Bots to help me start things and created a simple connection to my Twitch Channel through the Java Twitch API, integrated into my project by Gradle. Soon I was able to receive messages I had in my chat and relay them to my console. The next issue I had was to determine the way I detected highlights in my chat. I had to find a way to reliably detect surges in chat activity, without counting and marking isolated cases. Research suggested using the exponentially weighted moving average (EWMA), allowing my bot to rule out the occasional nonstarter.
The EWMA allows the detection of exceptional deviation by comparing a value to a history, but adding more weight on recent instead of old values through adding exponentially decreasing weights. This added reliable analysis of my received data, so finally all that was left to do was setting a threshold deviation value and giving my bot access to set a marker command in the chat as soon as this threshold was exceeded.
Database and Visualization
Throughout my research for a appropriate and comfortable interface between the database to implement and the visualization tool, I came by countless options. The original plan was to turn my Bot into a scheduled task as a service in my very own Wildfly application server. This should be integrated into my IntelliJ IDE via Gradle, or tested out through Eclipse via Maven. Additionally I wanted to save my data in a SQLite database which would save data with JPA/Hibernate and which would communicate with my Chartjs visualization tool on my website through a REST API. Chartjs would take the data and show a graph, describing the EWMA value of the chat’s activity in realtime.
Rethinking the Architecture
After some research for the best way to set up the Wildfly Server I came across an article, describing .NET core and its web and database features. It seemed like there was everything I needed to realise my project in one package, so I decided to leave the comfort of my Java-only experience and dive into uncharted territory. Setting up the bot again in a .NET core web application using C# was no issue, since I was able to take over a lot of my Java logic into my new project. NuGet proved itself to be an intuitive tool, to integrate external libraries into my workspace and through the integration of TwitchLib, the .NET library for Twitch related projects, my bot was able to do everything, the Java version was able to do.
Next was the creation and storage of the received data in a database. For this I used the combination of .NET’s provided library for databases of webapplications and Microsoft’s Entity Framework. These allowed me to create a SQLite database model which could be initialized by NuGet and filled by connecting my Bot with the database context.
Through the NuGet Package Manager I also found a convenient interface to connect the ASP.NET MVC framework to chartjs. By writing a ChartController class, I created the means to send a JSON format result from my database to the visualization website, which I configured to show chat data from every second in a historical line graph.
Additionally to just being able to run the WebApplication through my IDE, the .NET Web Application allowed me to create a Docker script, which, when running Docker in Linux mode, could simply be started through the Visual Studio IDE, running my application as well.
Issues and Conclusion
My biggest issue was to find and configure the right tools, which would connect every service I needed. This, of course, was also made difficult by the fact, that I have never done a project like this, so initially I didn’t even know what to look for. It was eventually rectified by research and deciding to use .NET core as my preferred platform, which however only happened after a lot of trial and error episodes, trying to get Wildfly working in my IntelliJ, als well as Eclipse IDE. As a developer who has first and foremost worked with Eclipse, Maven and Java I first tried to try something new with the IntelliJ IDE and Gradle. Of course I needed a little time to get used to this change, even though the difference to my usual workflow was not too big.
By deciding to switch everything up, I challenged myself to take on something completely new. Though I worked with C# once, as part of a Unity project where I was in charge of the enemy AI in a video game, the scope of the project was vitally different. So first I decided which libraries, frameworks and interfaces I would need and use, to afterwards research and understand how I needed to use them in order to set up the base for my project.
In conclusion this was a really interesting and fun project to work on. By combining Twitch as my private interest and the cloud technologies, I gained valuable knowledge and insights into the subject matter. It also eased my fear in trying new things and technologies. Sometimes it’s necessary to jump over your own shadow in order for things to get eventually easier.
If you would like to take a look at the source code, or try the bot yourself, click here.