Detect when an (IRC) thread ends which otherwise is silent

Viewed 17

I'm attempting to use the python Twitch-Python library for a simple Twitch bot to connect to chat and respond to messages. The bot works as expected, but about 1/6 times the bot fails to connect and the program simply ends. As explained on this error page, it seems to be because the thread ends and doesn't return anything at all https://github.com/PetterKraabol/Twitch-Python/issues/45

I have moved the Chat class into my own code, and have the following:

    def __init__(self, channel: str, nickname: str, oauth: str, helix: Optional['twitch.Helix'] = None):
    super().__init__()
    self.helix: Optional['twitch.Helix'] = helix

    self.irc = tc.IRC(nickname, password=oauth)
    self.irc.incoming.subscribe(self._message_handler)
    self.irc.start()
    self.channel = channel.lstrip('#')
    self.joined: bool = False

    print(self.irc.is_alive())

The class is called with two different functions elsewhere, like so:

def handle_message(self, message : twitch.chat.Message) -> None:
    print(message.sender, message.text)



def main(self):
    ircConn = TCH(channel='#CHANNEL NAME", nickname="BOT NAME", oauth="SAMPLEOAUTH")
    ircConn.subscribe(self.handle_message)

The issue is that about 1 in 6 times, the subscribe() doesn't actually do anything and the program ends. I'd like to find a way to detect when it fails, so that I may attempt a retry, but nothing I've found works. The function/class doesn't return anything, adding an on_error or on_completed argument to the subscribe() doesn't call anything, and using sys.excepthook also doesn't catch it failing. Additionally, the IRC thread always prints True for irc.is_alive(), even when it fails afterwards.

How can I catch the thread failing? Thank you.

0 Answers
Related