PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
791 stars 163 forks source link

Nothing firing on pubsub #304

Closed BobWatson closed 2 years ago

BobWatson commented 2 years ago

I can't get pubsub events to fire - I have the following set up - with one OAuth token with all scope options, and the channel ID for that same user:

users_oauth_token = "..." # valid user token
users_channel_id= "..." # valid user id

print(f"PubSub: got {self.channel} ({users_channel_id})")

loop = asyncio.new_event_loop()

client = twitchio.Client(token=users_oauth_token, loop=loop)
client.pubsub = pubsub.PubSubPool(client)

print(f"Listening to {self.channel}")

@client.event()
async def event_pubsub_channel_points(event: pubsub.PubSubChannelPointsMessage):
    print("HELLO")

async def go():
    topics = [
        pubsub.channel_points(users_oauth_token)[users_channel_id]
    ]

    await client.pubsub.subscribe_topics(topics)
    await client.start()

client.loop.run_until_complete(go())

When I redeem channel rewards, nothing happens - I never see "HELLO".

The only slightly different thing I've done that's not in the doco is that I have this running in its own thread (hence loop = ) but client.start() appears to launch fine.

github-actions[bot] commented 2 years ago

Hello! Thanks for the issue. If this is a general help question, for a faster response consider joining the official Discord Server

Else if you have an issue with the library please wait for someone to help you here.

chillymosh commented 2 years ago

Remember, this will only trigger on custom rewards and not the pre existing built in ones from Twitch, this is a Twitch design.

The channel ID should usually be an int

Ideally your bot should be using its own token and not the streamer's. We have better examples in the discord that I'll upload to the repo soon.

Loop= has been deprecated from asyncio and will be completely removed from the lib in the next major update.

If this doesn't solve your issue then please join the discord to discuss further, I'd also suggest to enable logging debug for further details.

BobWatson commented 2 years ago

I now have this working, based on an example I found on discord:

bot_token = "..." # valid bot token
users_oauth_token = "..." # valid user token
users_channel_id = 12345 # valid channel id

print(f"PubSub: got {self.channel} ({users_channel_id})")

loop = asyncio.new_event_loop()

client = twitchio.Client(token=bot_token, loop=loop)
client.pubsub = pubsub.PubSubPool(client)

print(f"Listening to {self.channel}")

@client.event()
async def event_pubsub_channel_points(event: pubsub.PubSubChannelPointsMessage):
    print(event.id)

async def go():
    topics = [
        pubsub.channel_points(users_oauth_token)[users_channel_id]
    ]

    await client.pubsub.subscribe_topics(topics)
    await client.connect()

client.loop.create_task(go())
client.loop.run_forever()

Using loop is required here since this is happening in a thread in my app - this is all wrapped in a function that's being called with Thread(...). So not sure about longevity if that's going away.

I'm not sure what the mechanical difference between those two scenarios is, but have it working for now.

chillymosh commented 2 years ago

The issue with yours was you had run_until_complete, so the loop runs only once and ends as it completes.

It's an issue with the example on the docs page that will be updated soon. I missed it at first glance.