Hey everyone! I’m working on building a Discord bot that can automatically post messages to my server whenever I start streaming on Twitch. I’ve been using discord.py library but I’m stuck on how to properly detect when someone goes live and send notifications to a specific channel.
I tried using voice state events but I’m not sure if this is the right approach. Here’s what I have so far:
@client.event
async def on_user_update(user_before, user_after):
if user_before.activity != user_after.activity:
if user_after.activity and user_after.activity.type == discord.ActivityType.streaming:
print("Streaming activity detected!")
Any suggestions on how to make this work properly? I want it to send a message when streaming starts and maybe another one when it ends.
Your problem is on_user_update - it only catches profile changes, not activity stuff. Switch to on_member_update since that picks up activity changes in your server. I had the same issue and user events were super unreliable for streaming status.
Couple things I learned: definitely add channel filtering to your event handler. Without it, your bot will either spam everywhere or just break silently. Also throw in a cooldown - Discord loves firing multiple activity events when people mess with their stream settings. I just use a timestamp check to block duplicate announcements for 5 minutes.
Once you’ve got the right event handler, the streaming detection works great.
Your on_user_update approach should work, but needs better handling. I hit the same issues building my streaming bot last year. Main problem: activity detection gets inconsistent and you’ll get multiple triggers for one stream session. I fixed this by storing streaming states in a dictionary - tracks who’s already announced. Add error handling too because the activity object sometimes returns None even when someone’s streaming. Check for your specific user ID instead of all users or you’ll spam notifications for every streamer. Also throw in a small delay before notifications - Discord fires multiple activity updates when people go live.
i totally agree. twitch’s api is def the better way to go. discord sometimes misses updates, and polling is just more reliable for stream status. just set up a webhook or check the api every few mins to keep ur notifications accurate and neat.