How I created an alert Twitch live

πŸ”§ Tech used

For this little project, I used Twitch API with the library node-twitch on npm.

I send the alert message on Discord with a sublime rich text (aka embed), here is the message :

βš™οΈ How it's work ?

At the first time, in my main file named app.js i've got the basics requires.

const Discord = require('discord.js')
const client = new Discord.Client()
const TwitchAPI = require('node-twitch').default
const config = require('./config')

const twitch = new TwitchAPI({
    client_id: config.twitch.AppClientID,
    client_secret: config.twitch.AppSecretToken
})

For AppClientID and AppSecretToken, go to the Twitch Dev Console. Create a new App and catch all.

For the second part, it's the main system.

When X start a stream, the script check if the message is already sended or not with a memory variable. I named IsLiveMemory.

The default state :

let IsLiveMemory = false

I get Twitch profile from the API

const run = async function Run() {
    await twitch.getStreams({ channel: "thomasbnt" }).then(async data => {
        const r = data.data[0]
        let ThisGuildOnly = client.guilds.cache.get("GuildID")
        const ChannelAnnounceLive = ThisGuildOnly.channels.cache.find(x => x.id === "ChannelAnnounceLiveID")

        if (r !== undefined) {
            if (r.type === "live") {
                if (IsLiveMemory === false || IsLiveMemory === undefined) {
                    IsLiveMemory = true
                } else if (IsLiveMemory === true) {
                } else {}
            } else {
                if (IsLiveMemory === true) {
                    IsLiveMemory = false
                } else {}
            }
        } else {
            if (IsLiveMemory === true) {
                IsLiveMemory = false
            } else {
            }
        }
    })
}
setInterval(
    run, 15000)

πŸ’ͺ🏼 Let's me explain the logic

If the live Stream is online ("type": "live"), check if the Memory is false to avoid duplicating messages, if these two conditions are true, then send the message to general channel.

If the live Stream is online and the Memory is true, don't send message to avoid a duplicate message.

I'm proud to this script, good usage for my Discord server and my livestream on Twitch. I send a message and change my server icon to purple color. πŸ˜„βœ¨

✨ You can obtain this script on my Buy Me a Coffee page !

Let your community know you're going live on Twitch! This script is designed for that, it alerts in a chosen room when you go live and when you finish it. It also changes your server icon to whatever you want.

β˜• Check my Twitter account. You can see many projects and updates. You can also support me on BuyMeaCoffee.

28