Twitch

This Cog enables the bot’s Twitch livestream-checking capabilities.

It uses Twitch’s API to get the information needed.

Hint

To enable this cog, set the ENABLE_TW variable.

Packages

No Twitch package is needed. It uses requests, which should be already installed. If it’s not, the package can be installed with pip or pipenv.

env variables

Name

Description

TW_CLIENT_ID

A Twitch API client ID

TW_CLIENT_SECRET

The corresponding client secret

TW_TOKEN

An API Access Token

TW_FREQUENCY

How often to check for streams (in minutes)

TW_FILE

The streamers to check and users to notify, see How to use this cog

Using Twitch’s API

To use the API, we need the Client ID and Secret. To get them, follow Step 1 of this getting started guide. Fill TW_CLIENT_ID and TW_CLIENT_SECRET with these values.

Then, we need an Access Token. Normally the bot takes care of this: the tokens expire so the bot regularly checks its validity. If the token is invalid, or it can’t be found in .env, the bot automatically gets a new token and saves it in TW_TOKEN.

If for some reason this fails, see Manually getting an access token.

  • Finally, set TW_FREQUENCY. This variable indicates how often the bot will check Twitch, in minutes. It should be a string, casting it to int is done in bot.py.

How to use this cog

In order to indicate which streamers the bot should check, and which users it should notify, it uses the TW_FILE JSON file.

It is a dictionary where the keys are the IDs of the Discord users to notify, and the values are a list of the streamers to check for each user.

A Discord user’s ID can be found by right-clicking the user’s name. You can either use the URL of the streamer’s channel or its user_login, which is the last portion of the channel’s URL.

For example:

{
   "discord_user_ID_1":
      [
         "twitch_channel_1",
         "twitch_channel_2"
      ],
   "discord_user_ID_2":
      [
         "twitch_channel_1",
         "twitch_user_login_3"
      ]
}

Manually getting an access token

Attention

You can use the following methods to get an access token, but keep in mind that they will still expire, meaning that you will have to renew it every 60 days. If the automatic way is not working, I suggest opening an issue in the repo.

To manually get an access token, you can use:

  1. The Twitch CLI, as described in Step 2 of the Twitch API guide.

  2. A simple script, based on this Stack Overflow answer:

    import requests
    
    # Fill these variables with the credentials obtained
    # on the previous step.
    client_id = ''
    client_secret = ''
    
    body = {
       'client_id': client_id,
       'client_secret': client_secret,
       'grant_type': "client_credentials"
    }
    r = requests.post('https://id.twitch.tv/oauth2/token', body)
    
    keys = r.json()
    
    print(keys)
    

    A sample result of the above script:

    {
       "access_token": "132456789abcdefgh",
       "expires_in": 3600,
       "token_type": "bearer"
    }
    

    access_token is the token you need. expires_in indicates how many seconds the token will remain valid since the request.