How I created an alert Twitch live
Tags: node, javascript, api, twitch
Published: 2021-07-05 22:45:16

## 🔧 Tech used

For this little project, I used Twitch API with the library [**node-twitch** on npm](https://www.npmjs.com/package/node-twitch).

I send the alert message on [Discord](https://discord.com) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxmxwszy1139fzlaqans.png)

## ⚙️ How it's work ?

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

```js
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](https://dev.twitch.tv/console/apps/). 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 :
```js
let IsLiveMemory = false
```

I get Twitch profile from the API

```js
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](https://www.twitch.tv/thomasbnt). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0j8ssyw8owx28pyreqh4.png)](https://www.buymeacoffee.com/thomasbnt/e/57017/?via=thomasbnt)


> [**This script is available** on my **Buy Me a Coffee** page.](https://www.buymeacoffee.com/thomasbnt/e/57017/?via=thomasbnt)

Also available in [French language here](https://www.buymeacoffee.com/thomasbnt/e/36106/?via=thomasbnt). ✨


|☕|Check my [Twitter account](https://twitter.com/thomasbnt_). You can see many projects and updates. You can also [support me on Buy Me a Coffee, Stripe or GitHub Sponsors](https://thomasbnt.dev/donate). Thanks for read my post ! 🤩|
|---|---|