DEV Community

loading...
Cover image for How I created a alert Twitch live

How I created a alert Twitch live

Thomas Bnt
French web developer maintly but touches everything, servers, bots, community management, events, social networks and moore. I learn VueJs at this moment and MySQL. β€” Addict to Cappuccino and Music
・2 min read

πŸ”§ 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 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.

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

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

Discussion (0)