PythonistaGuild / TwitchIO

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

No event_ready if initial_channels is not passed #290

Closed YetAnotherSimon closed 2 years ago

YetAnotherSimon commented 2 years ago

Hello there, I stumbled across the issue that my event ready function is not called if the initial_channels parameter is not passed to the client. Since it is an optional parameter, I don't think this is supposed to happen.

Thanks in advance!

from twitchio import Client
from os import getenv

token = getenv("TWITCH_OAUTH_TOKEN")
secret = getenv("TWITCH_CLIENT_SECRET")

client = Client(token=token, client_secret=secret)  # <- Here event_ready is never called
# client = Client(token=token, client_secret=secret, initial_channels=['gronkhtv'])  # <- This works just fine

@client.event()
async def event_ready() -> None:
    print("Ready!")

client.run()
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.

IAmTomahawkx commented 2 years ago

Well with this example your bot would stop running within 30 seconds, because you need to connect to a channel for twitch to keep your connection alive. However if you don't provide initial channels, no channels are sent at startup, which means twitch will not provide a ready event; thus no event_ready

YetAnotherSimon commented 2 years ago

Oh okay I see, then I will add a default channel. Maybe this is something to note down in the documentation? This seems like an rather important information to me for something marked as optional

IAmTomahawkx commented 2 years ago

Hello, sorry for the slow response. A note in the docs is a good idea. This event is dispatched whenever it is received by twitch. Calling client.join_channels after starting the bot should in theory still dispatch this event, however using initial_channels is still recommended

Commaster commented 2 years ago

Oh, so that's the reason... I was wondering why nothing happened after twitch sends me codes 1, 2, 3, 4, 375, 372, 376. I guess the behavior changed after https://github.com/TwitchIO/TwitchIO/commit/49ba6a53b830458c778a392bddb82b9a3e8ff9e4#diff-c98e8aab9611c0fdcab32d3a2e428de2db41bb097987af0aef88b5cba753c163 so I didn't experience this issue while using 2.1.5, but am now after updating to 2.3.0... Any reason this was changed to enforcing initial_channels? Basically breaking whisper-listening bots...

chillymosh commented 2 years ago

You can add the bots own channel to initial_channels for now, it should only be event_ready that doesn't fire with empty channels, but it could still send and recieve data. I'll look into making empty lists trigger it again soon. The main reason the channel codes were explicitly added was the introduction of wait_for_ready.

Commaster commented 2 years ago

Can we let the 376 RPL_ENDOFMOTD ":End of MOTD command" code trigger is_ready? It seems to be the last reply twitch sends in the initial burst after connecting 🤔

EvieePy commented 2 years ago

The problem is that event_ready is supposed to represent the bots internal cache being fullfilled as well as being authenticated by Twitch. So currently it's slightly flawed, I'm not entirely sure how it will be fixed in 2.0 I might leave that up to @chillymosh but in 3.0 the behaviour will be different.

EvieePy commented 2 years ago

I've added the 3.x label here as this will need to be changed on that version as well.

chillymosh commented 2 years ago

@Commaster please try with the latest push

Commaster commented 2 years ago

@Commaster please try with the latest push

@chillymosh I'm using pip so I just patched my local websocket.py with your changes (Hope this is okay). I'm getting event_ready properly now. Thank you!

chillymosh commented 2 years ago

Fixed in 1559d1a2a56b2a04b140a5e37a88767371761082