How to Create a Twitch Command Script for Streamlabs Chatbot — Part 4: Debugging
In Part 3 we finished the logic in our Mulder command and it worked beautifully. However, when it comes to programming, there seems to be this unwritten rule: Programs never work first try. It’s always an iterative process, whether it’s changes in how you expect something to work or weird bugs in your code. In this part we’ll focus on the latter. Knowing some debugging skills will really help when making your own commands, or just when programming in general. Let’s get to it!
Logging is basically all I need to show you. Logging what your script is doing and when it’s doing it is the fastest way to find out where a bug could be hiding. Luckily, SC has a log method of its own:
Parent.Log(string command, string message) and we are going to use that.
First off, that log method looks kind of bulky and, as we’re going to use it more than once, let’s create a utility method to wrap it in. The added benefit is that we don’t need to specify the command every time we want to log something. It stays the same anyway and doing it this way keeps us DRY:
I noticed that we’re using the literal command name in two different places and this violates the DRY principle, so let’s fix that. Add a global variable to the ones we already have and replace all instances of
“!mulder” with that variable:
Now, let’s add some log statements to the methods we’re using:
Good things are happening here. Reload the script in SC and then move to the log window by clicking the middle icon in the upper right:
Clear the window with the reload symbol in the upper right and move to the Console section of SC. Enter !mulder again and move back to the log window. It should show you the logs as it flows through the different methods:
There are quite a few things that could go wrong in a program and having logs to pinpoint the location will save a lot of time when debugging these problems. If something were to go wrong in send_message, you would notice that by the missing logs. It makes it easy to find a bug, fix it, and continue. Imagine not having any clue why something is failing, you would be randomly guessing problems and get frustrated. Trust me, I’ve been there.
As you continue to create more advanced commands, you’ll recognize where and how to place logs to verify correct values are being passed around or to check if the method you need is actually being called. You can do a lot with logs.
In this part we learned about logging as a tool for debugging your script. This was an optional part, but in my opinion a very useful one. In the next and final Part 5 we will take a look at how to add a cooldown and a UI to Streamlabs Chatbot for your script. It’s optional, but nice to have, so if that sounds interesting, join me towards Part 5!