How to build a Twitch bot for free with 0 setup. (Part 2)
Time to build a command that’s actually useful.
In part 1, I showed you how to get from no bot to having a bot in 1 minute. The output wasn’t really useful, but we’re gonna fix that now.
Let’s get to work
We’re not gonna have “Hello World” as a command. Lol.
Basically, each command is gonna be this:
<?php// Whatever needs to be doneecho 'The results';
The 2 forward slashes means the line of code is a comment and it gets ignored. At this point you could pick another language in Runnable and achieve the same thing.
Before moving to a more interactive example in part 3, I’ll show you how to build a quote command to get our feet wet.
“Woah.. slow down..” Yeah, if this is the first time you see this, it looks weird. I’ll explain the syntax, but remember, at this point I’m no longer teaching you how to build a bot; I’m teaching you PHP. You could stop reading this tutorial and learn PHP (or any other language) by yourself.
$quotes is a variable. A variable holds a piece of data and we define them using the dollar sign. For example:
$name = 'John';echo $name;
This would output
John. In the screenshot, the variable holds an array. An array is just a list of comma separated values. You use brackets to write an array:
// This array contains 3 items.
$person = ['John Rivs', 23, 'Web developer'];
By the way, at the end of each “sentence”, you must write
To access a value from the array we do
$person . The number is the index and it’s 0 based, which means that 0 represents the first item in the array ,
'John Rivs'. 1 would be the second value,
23. 2 is the third value,
'Web developer' .
$person = ['John Rivs', 23, 'Web developer'];echo 'Hi! My name is ' . $person;
This would output
Hi! My name is John Rivs . The dot you see in the middle of the sentence is something used to concatenate. Notice how it’s not between the quotes, so it’s not regular text. It’s a special character in the language.
You can think of this like PHP substituting
$person with the value behind it, which happens to be a string (text), and then putting it next to another string
echo 'Hi! My name is ' . 'John Rivs';
Back in time
Just to the first screenshot.
array_rand() is a function. A function is a block of code that you can reuse. This one is provided by PHP out of the box. It picks a random value from an array. The parenthesis is used to execute the function. Between them, you can pass things that the function will use, parameters. In this case, we’re gonna pass
array_rand will pick a random item from the array. Unfortunately, it’s not gonna return the actual value (which would be ideal), it’s gonna return an integer (a number) that represents the position of the item in the array.
So let’s imagine the second quote got selected.
1 (remember, it’s 0 based). At this point we’d have
echo $quotes; . This should look familiar; it’s how you access a value from an array. Now we’d have
echo ‘Well, when I walk outside naked, people throw garbage at me.'; .
Let’s save this and run it a few times:
Hey there! I’m John Rivs, web developer. After developing a Twitch bot for a while, I’ve decided to explain what’s the first step. My bot looks very differently today from what I show in this tutorial, but it explains almost exactly how my bot started.
Any questions? Leave a comment or hit me up on Twitter.