Teekeks / pyTwitchAPI

A Python 3.7 compatible implementation of the Twitch API, EventSub, PubSub and Chat
https://pytwitchapi.dev
MIT License
256 stars 38 forks source link

Using pyTwitchAPI through OBS-Studio Scripting? #320

Closed exeloar closed 4 months ago

exeloar commented 4 months ago

I've been trying to use the library functionality through the embedded OBS-Studio Scripting page to perform OBS actions on a channel point eventsub event.

I have the script initialize and start up the async twitch calls (asyncio.run()) when OBS-studio starts up, but this causes crashing & indefinite hang on the async function. I've also tried making a new asyncio event loop and triggering it on that with the same result.

I've started it in a separate thread now (shown below), with no crashing, but this causes the callbacks to never be triggered. Has anyone used the library in conjunction with the OBS scripting platform before and know what might be happening?

  def connectToTwitch():
      asyncio.run(connectToTwitchChannelPointRedemptions())

  async def connectToTwitchChannelPointRedemptions():
      global twitch
      global eventsub

      twitch = await Twitch(APP_ID, APP_SECRET)
      helper = UserAuthenticationStorageHelper(twitch, [AuthScope.CHANNEL_READ_REDEMPTIONS])
      await helper.bind()

      eventsub = EventSubWebsocket(twitch)
      eventsub.start()

      print("Channel Point Redemptions Enabled")
      await eventsub.listen_channel_points_custom_reward_redemption_add(ExeloarTwitchID, handleChannelPointRedemption)

  Thread(target = connectToTwitch).start()
Teekeks commented 4 months ago

after subscribing, you have to keep the asyncio loop (and with it the thread) alife using something like this:

try:
    while True:
        await asyncio.sleep(1)
finally:
    eventsub.stop()
    await twitch.close()
exeloar commented 4 months ago

I fixed it with a combination of this & also putting it on its own event loop. Thanks for the help