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.
Requirements¶
Packages¶
No Twitch package is needed. It uses
requests, which should be already installed. If it’s not, the package can be installed withpip:
python3 -m install -U requests
py -m pip install -U requests
env variables¶
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_IDandTW_CLIENT_SECRETwith these values.Then, we need an Access Token. Normally the bot takes care of it, you just need to uncomment
TW_TOKENand leave it blank. When the bot checks the token validity at startup, it will notice the token is empty and will automatically try to get a valid token and save it inTW_TOKEN. If for some reason this fails, see Manually getting an access token.TW_FILErepresents the path to a JSON file that stores the IDs of the Discord users to notify, and the Twitch channels to check for each one. The name of the file istwitch_ids.jsonby default. You can use the example file provided, but please change its name since leaving it as is may result in overwritting when updating the bot with git pull.Finally, set
TW_FREQUENCY. This variable indicates how often the bot will check Twitch, in minutes. It should be a string, casting it tointis done inbot.py.
Format used by TW_FILE¶
The format to use for TW_FILE is as follows:
{ "discord_user_ID_1": [ "twitch_channel_1", "twitch_channel_2" ], "discord_user_ID_2": [ "twitch_channel_1", "twitch_user_login_3" ] }
Fill it with the corresponding information and set TW_FILE in
.env. 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 said URL.
Manually getting an access token¶
Attention
API tokens expire. When this happens, the bot tries to get a new one automatically. If the automatic way failed, you may have to get a new token each 60 days, or the cog won’t work. In this case, I suggest opening an issue in the repo.
If the automatic way of getting an access token fails, there are two manual ways of getting it:
The Twitch CLI is one option. Step 2 of the aforementioned guide explains how to use it.
A simple script (based on this Stack Overflow answer) can be used instead of downloading the CLI:
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_tokenis the token you need.expires_inindicates how many seconds the token will remain valid since the request.