dopeghoti / DiscordCurrentlyCastingBot

Python-based bot which uses the Discord and Twitch APIs to monitor a list of approved channels. When one goes live, a message will be posted. Any links to channels not on the air or approved will be removed.
5 stars 0 forks source link

Bot gets Very Sad when Discord API endpoint throws HTTP/5xx error #8

Open dopeghoti opened 3 years ago

dopeghoti commented 3 years ago

Stack trace from stderr follows:

Unhandled exception in internal background task 'find_messages'.
Traceback (most recent call last):
  File "/home/ghoti/.local/lib/python3.7/site-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "./bot.py", line 240, in find_messages
    @bot.command( name='list', help='Report a list of everyone in the Database' )
  File "/home/ghoti/.local/lib/python3.7/site-packages/discord/iterators.py", line 308, in flatten
    data = await self._retrieve_messages(self.retrieve)
  File "/home/ghoti/.local/lib/python3.7/site-packages/discord/iterators.py", line 348, in _retrieve_messages_before_strategy
    data = await self.logs_from(self.channel.id, retrieve, before=before)
  File "/home/ghoti/.local/lib/python3.7/site-packages/discord/http.py", line 265, in request
    raise DiscordServerError(r, data)
discord.errors.DiscordServerError: 502 Bad Gateway (error code: 0): <!DOCTYPE html>

Looks like it may be an upstream issue in discord.py but I might just had to do some exception handling.

dopeghoti commented 2 years ago

I may have a fix for this; it is in a separate branch while I test before I pull it into the mainline.

CrystalSpore commented 2 years ago

As an FYI, the LGBTea Cafe team is now also using the mentioned Dev branch. We'll let you know if we come across any issues 👍

dopeghoti commented 2 years ago

Appreciate it. I have found that while it does help it does not fix the issue because that one location isn't the only one where Discord can throw an Exception at me, so I'll be updating that dev branch soon with more extensive exception handling to hopefully put a pin in this issue once and for all. Also I really love the name of your Team!

CrystalSpore commented 2 years ago

After almost a month of uptime the bot finally died again, so definitely the current version on the other branch is a great improvement.

I'm including the crash error below, in hops that that will provide some insight into the issue:

This is running on an Ubuntu 20.04.4 LTS bare metal server, using an AMD Ryzen 3 2200G.

Unhandled exception in internal background task 'find_messages'.
Traceback (most recent call last):
  File "./bot.py", line 241, in find_messages
    msgs = await MonitorChannel.history().flatten()
  File "/usr/local/lib/python3.8/dist-packages/discord/iterators.py", line 308, in flatten
    data = await self._retrieve_messages(self.retrieve)
  File "/usr/local/lib/python3.8/dist-packages/discord/iterators.py", line 348, in _retrieve_messages_before_strategy
    data = await self.logs_from(self.channel.id, retrieve, before=before)
  File "/usr/local/lib/python3.8/dist-packages/discord/http.py", line 192, in request
    async with self.__session.request(method, url, **kwargs) as r:
  File "/usr/local/lib/python3.8/dist-packages/aiohttp/client.py", line 1138, in __aenter__
    self._resp = await self._coro
  File "/usr/local/lib/python3.8/dist-packages/aiohttp/client.py", line 559, in _request
    await resp.start(conn)
  File "/usr/local/lib/python3.8/dist-packages/aiohttp/client_reqrep.py", line 913, in start
    self._continue = None
  File "/usr/local/lib/python3.8/dist-packages/aiohttp/helpers.py", line 721, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/discord/ext/tasks/__init__.py", line 101, in _loop
    await self.coro(*args, **kwargs)
  File "./bot.py", line 242, in find_messages
    except DiscordServerError:
NameError: name 'DiscordServerError' is not defined