Discord Bot with Twitch and YouTube integrations for creator notifications!
Get messages when a Twitch user starts streaming, or when a YouTube channel posts a new video or starts a live.
Uses modern features like Slash Commands and Message Components
- Common
/about
: Bot status with uptime, memory usage and more
- Creators
/info <service> <term>
: Information about a Twitch user or YouTube channel/follow <service> <term>
: Follow a creator and get notified when it starts streaming or uploads a video/unfollow <service> <term>
: Stops following a creator/list
: Lists all creators being followed on the server
- Music
/play <term>
: Play a song from YouTube/pause
: Pauses current song/resume
: Resumes current song/skip
: Skips current song/stop
: Stops playing and clears the queue/queue
: Lists current songs in queue
Copy the sample environment sample file into a secrets file:
cat config/dev.secret-sample.exs > config/dev.secret.exs
Run mix setup
to install all dependencies and run the database migrations
For receiving and registering to webhooks locally, you need a public url that can reach your machine. You can use start a proxy with ngrok http $PORT, then copy the url generated into the :host
config.
Grab your bot's token from Discord Applications on Your application > Bot > Click to reveal token
Create an account on Google Cloud Platform, and create a new API Key for your bot. You can limit the key to only have access to YouTube Data API v3
Example webhook payload
<?xml version='1.0' encoding='UTF-8'?>
<feed
xmlns:yt="http://www.youtube.com/xml/schemas/2015"
xmlns="http://www.w3.org/2005/Atom">
<link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCHDxYLv8iovIbhrfl16CNyg"/>
<title>YouTube video feed</title>
<updated>2024-05-17T01:37:49.636216015+00:00</updated>
<entry>
<id>yt:video:TBXAlFqn93E</id>
<yt:videoId>TBXAlFqn93E</yt:videoId>
<yt:channelId>UCHDxYLv8iovIbhrfl16CNyg</yt:channelId>
<title>I wish this wasn’t an Ubisoft game</title>
<link rel="alternate" href="https://www.youtube.com/watch?v=TBXAlFqn93E"/>
<author>
<name>GameLinked</name>
<uri>https://www.youtube.com/channel/UCHDxYLv8iovIbhrfl16CNyg</uri>
</author>
<published>2024-05-17T01:37:38+00:00</published>
<updated>2024-05-17T01:37:49.636216015+00:00</updated>
</entry>
</feed>
To be able to search and receive webhooks from streams, you need to register an application on the Twitch Applications. After that, you'll be able to get the client_id
and client_secret
tokens.
Example webhook payload
{
"challenge":"6gepqopldUA-K7p_CJDK3gmndD6mxyxjWSS7wCTFxZ8",
"subscription":{
"id":"3ce498e8-44d4-446f-886c-66bc256af5d9",
"status":"webhook_callback_verification_pending",
"type":"stream.online",
"version":"1",
"condition":{
"broadcaster_user_id":"35958947"
},
"transport":{
"method":"webhook",
"callback":"https://botchini.lucapasquale.dev/api/twitch/webhooks/callback"
},
"created_at":"2024-05-17T01:46:52.47828845Z",
"cost":1
}
}
After all necessary values are on config/dev.secret.exs
, run the bot with: mix phx.server
. It will reload automatically when you update a file.
The bot needs the following env_vars:
PORT
: Port to run on. Defaults to 4000PHX_HOST
: The endpoint your bot is running on, withouthttps://
DATABASE_URL
: The url to connect to your postgres dbDISCORD_TOKEN
: Token for your Discord BotYOUTUBE_API_KEY
: YouTube API keyYOUTUBE_WEBHOOK_SECRET
: Secret string to be used for validating YouTube webhooksTWITCH_CLIENT_ID
: Client ID for your Twitch applicationTWITCH_CLIENT_SECRET
: Client Secret for your Twitch applicationTWITCH_WEBHOOK_SECRET
: Secret string to be used for validating Twitch webhooks