DEV Community

Cover image for How I created an alert Twitch live
Thomas Bnt
Thomas Bnt Subscriber

Posted on • Edited on

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 :

The output to the message on Discord when I'm in live on Twitch

⚙️ 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
})
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

💪🏼 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 !

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.

Get the script for Twitch alerts on Discord

This script is available on my Buy Me a Coffee page.

Also available in French language here. ✨

Check my Twitter account. You can see many projects and updates. You can also support me on Buy Me a Coffee, Stripe or GitHub Sponsors. Thanks for read my post ! 🤩

Top comments (0)