Create a Twitch bot in Nodejs and Javascript

Learn how to create bots for Twitch and customize your live ones with a unique touch of originality.

Many of us have seen bots that are in the chats of the channels we follow (or on our own channels as well), among the most famous are StreamElements, MooBot, NightBot and StreamLabs, but is it possible to create your own? Th

e answer is yes, and now you’ll learn how

Preparation

First of all we must prepare a few things before creating our bot, for this we will first install the following programs:

1. Node.js
2. Visual studio code.
3. Create a Twitch account for our bot.

Creating the environment

First let’s prepare the project, for this we create a folder anywhere on our computer with the name we want, for example, the name of your bot. We op

en the folder and right click on the background, which will give us an option to open Visual studio code in it.

This will open up the visual studio code program, which is no less than a code editor that will allow us to develop our bot. Next we will give you the button a[Terminal]t the top of the program and then to or de[New terminal]pends on how we have the language.

This will open a small console at the bottom, in this we will write the command “npm init” to start an empty project. The program will ask us several questions which we can simply press Enter on each one and answer the default answer.

Now we only have to install the libraries necessary to connect to the Twitch chat, for this we will write on the same console “npm install tmi.js” after using the above command.

Connect to Twitch chat

Now we’ll make the connection to the chat, but first we’ll create the first file of the bot. We’ll call this one “index.js” and we’ll do it in the same folder where we

are. For that we can right click on the file explorer that we have on the left and give it to [New file]

We will create a file where we can write, in this one we will paste the following:

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

const client = new tmi.Client({
    connection: {
        reconnect: true,
        secure: true,
    },
    identity: {
        username: "bot_name",
        password: "oauth:bot_token",
    },
    channels: ["your_channel"] ,
});

client.connect().then(() => {
  console.log("Bot connected");
});

As you can see we have to place the name of the channel that the bot will connect (We will set it to “your_channel”), the name of our bot’s account (in “bot_name”) and the oauth token of it (in “bot_token”)

I guess they already have the first two requirements, but maybe they’re confused about the Token. In short this password will work, to obtain it we can enter this generator which will ask us for permissions from our Twitch account to generate one. (Make sure you’re in your Bot account on Twitch while doing this step)

Once with the token ready the file would look something like this

In my case I’m creating a bot with my own account to connect to my own channel.

Following this we will start the bot. In the same console we will use the command “node.” and the index.js file will load

If we did all the steps well, we will see the following message in the console:

Creating our first command

As a next step we will make the bot say “Goodbye” every time someone writes “!hello” in the chat. For this we will go to the file “index.js” and below everything we will paste the following:

client.on("message", (channel, tags, message, self) => {
    if (message.toLowerCase() == "!hello")
        client.say(channel, `{$tags.username}, Goodbye!`);
    }
});

If we know a little English, we can figure out what he’s doing. Basically this code will activate every time someone sent a message in the chat and check if the message is “!hello”, if so then it will answer “Goodbye!”.

As you can see the message to be sent we will define it in these quotation marks ‘ and if you ask that it is that is the name of the user who used the message, we will mention it in the chat.

To apply the changes we will turn off the bot, for this we will press CTRL + Z at once on the console and rewrite “node .” and enter.

Now every time we write “!hello” in the chat our bot will answer Goodbye.

Conclusion

It’s a somewhat basic bot but it will give a touch of customization to our streams. Of course, we can create more commands and also add more complex functions to it than just answering, this is just a basic example.

If in doubt do not hesitate to contact me on Twitter (@Sammwy)

Leave a Reply