Friday, March 31, 2023

Creating a Discord Bot to Notify When Your Twitch Channel Goes Live

Discord is a popular platform for gamers to communicate and connect with each other. If you are a Twitch streamer, you can use Discord to notify your followers whenever you go live. In this article, we will show you how to create a Discord bot that sends a message to your Discord server when your Twitch channel goes live. 

  1. Create a Discord bot: First, you'll need to create a Discord bot and get its token. Follow the official Discord guide to create a bot: https://discord.com/developers/docs/intro.
    Make sure to copy the token, as you'll need it later. 
  2. Install necessary libraries: You'll need to install the discord.py and twitchapi libraries. You can install them using the following commands:

    pip install discord.py
    pip install twitchapi

    Make sure you have some basic knowledge using python.

  3. Get your Twitch API credentials: To access your Twitch channel's information, you'll need to create a Twitch API application and get its credentials. Follow the official Twitch guide to create an application: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth. Make sure to get your Client ID and Client Secret, as you'll need them later.
  4. Write the code: Here's an example code snippet that you can modify to suit your needs:

 

import os
from twitchapi import Twitch
from discord.ext import commands
from discord import Webhook, RequestsWebhookAdapter

# Set up the Twitch API client
twitch = Twitch(client_id='YOUR_TWITCH_CLIENT_ID', client_secret='YOUR_TWITCH_CLIENT_SECRET')
twitch.authenticate_app([])

# Set up the Discord client and webhook
discord_token = 'YOUR_DISCORD_BOT_TOKEN'
discord_channel_id = 'YOUR_DISCORD_CHANNEL_ID'
discord_webhook_url = 'YOUR_DISCORD_WEBHOOK_URL'
client = commands.Bot(command_prefix='!')

# Define a function to send a message to the Discord channel
async def send_discord_message(message):
    webhook = Webhook.from_url(discord_webhook_url, adapter=RequestsWebhookAdapter())
    await webhook.send(message)

# Define a function to check if your Twitch channel is live
async def check_twitch_live():
    user_login = 'YOUR_TWITCH_CHANNEL_NAME'
    user_data = twitch.get_users(logins=[user_login])
    user_id = user_data['data'][0]['id']
    stream_data = twitch.get_streams(user_id=user_id)
    if stream_data['data']:
        # Your Twitch channel is live, send a message to Discord
        message = f'Hey everyone, {user_login} is now live on Twitch! Check it out: https://twitch.tv/{user_login}'
        await send_discord_message(message)

# Define a command to manually check if your Twitch channel is live
@client.command()
async def checklive(ctx):
    await check_twitch_live()

# Set up the Discord client event to check if your Twitch channel is live on startup
@client.event
async def on_ready():
    print('Bot is ready')
    await check_twitch_live()

# Start the Discord bot
client.run(discord_token)


Make sure to replace the placeholders with your own Twitch client ID and secret, Discord bot token, Discord channel ID, Discord webhook URL, and Twitch channel name.

Run the bot: Save the code to a file with a .py extension, and run it using the command python filename.py. The bot will automatically check if your Twitch channel is live on startup, and will send a message to your Discord channel if it is. You can also manually check if your Twitch channel is live by typing !checklive in your Discord channel.


1 comment:
  1. This works good for people who dont want to rely on 3rd parties.

    ReplyDelete