PennyDreadfulMTG / Penny-Dreadful-Tools

A suite of tools for the Penny Dreadful MTGO community
https://pennydreadfulmagic.com
MIT License
41 stars 28 forks source link

Ignoring exception in on_member_update in bot #5222

Closed bakert closed 6 years ago

bakert commented 6 years ago
Aug 25 22:29:48 katelyngigante bash[13212]: Ignoring exception in on_member_update
Aug 25 22:29:48 katelyngigante bash[13212]: Traceback (most recent call last):
Aug 25 22:29:48 katelyngigante bash[13212]:   File "/home/discord/.local/lib/python3.6/site-packages/discord/client.py", line 220, in _run_event
Aug 25 22:29:48 katelyngigante bash[13212]:     await coro(*args, **kwargs)
Aug 25 22:29:48 katelyngigante bash[13212]:   File "/home/discord/Penny-Dreadful-Discord-Bot/discordbot/bot.py", line 86, in on_member_update
Aug 25 22:29:48 katelyngigante bash[13212]:     await after.add_roles(after, roles[0])
Aug 25 22:29:48 katelyngigante bash[13212]:   File "/home/discord/.local/lib/python3.6/site-packages/discord/member.py", line 506, in add_roles
Aug 25 22:29:48 katelyngigante bash[13212]:     await req(guild_id, user_id, role.id, reason=reason)
Aug 25 22:29:48 katelyngigante bash[13212]:   File "/home/discord/.local/lib/python3.6/site-packages/discord/http.py", line 212, in request
Aug 25 22:29:48 katelyngigante bash[13212]:     raise NotFound(r, data)
Aug 25 22:29:48 katelyngigante bash[13212]: discord.errors.NotFound: NOT FOUND (status code: 404): Unknown Role
bakert commented 6 years ago

Still happening as of now. Very vague possibility this is because snowflakes are now ints and are getting compared against a string somewhere (that was the cause of another recent issue after the move to new discordpy lib version).

bakert commented 6 years ago

I'm not quite sure what

await after.add_roles(after, roles[0])

is meant to be doing. Something about if they are currently streaming.

bakert commented 6 years ago

OK I took another look at this.

When a member of the channel (server?) updates we check if they have switched from offline to online. If they have we find the 'Linked Magic Online' role, fetch their personal data from pdm, and add the role if the personal data comes back with an 'id' (aka we were able to fetch them from PDM via Discord ID). That all seems pretty reasonable.

The line that fails is adding the 'Linked Magic Online' Role. We grabbed this from Discord data so it's kind of weird that it fails. The exception raised implies that we asked the bot to add a role that does not exist.

The official docs don't mention a guild property on Member which is odd because that's where we fetch the roles from. https://discordpy.readthedocs.io/en/latest/api.html#member <-- bit confused because new lib uses guild not server.

Checking the docs it seems completely reasonable to retrieve the server Roles from Member.guild.roles and then send one of those Roles back in add_roles so I'm a bit nonplussed.

bakert commented 6 years ago

Added some debugging print statements in 680cec8b to try and figure this out.

bakert commented 6 years ago

The new handling in #5220 should give us enough to understand this also. Will remove the print statements soon.

bakert commented 6 years ago
(<Member id=156282836024819712 name='ambnz' discriminator='3619' bot=False nick=None guild=>, <Member id=156282836024819712 name='ambnz' discriminator='3619' bot=False nick=None guild=>)
{}

Reported on discordbot by discord user--------------------------------------------------------------------------------
NotFound
Stack Trace:

  File "run.py", line 87, in <module>
    run()
  File "run.py", line 14, in run
    bot.init()
  File "/home/discord/Penny-Dreadful-Discord-Bot/discordbot/bot.py", line 194, in init
    client.init()
  File "/home/discord/Penny-Dreadful-Discord-Bot/discordbot/bot.py", line 31, in init
    self.run(configuration.get('token'))
  File "/home/discord/.local/lib/python3.6/site-packages/discord/client.py", line 564, in run
    loop.run_forever()
  File "/usr/lib64/python3.6/asyncio/base_events.py", line 422, in run_forever
    self._run_once()
  File "/usr/lib64/python3.6/asyncio/base_events.py", line 1432, in _run_once
    handle._run()
  File "/usr/lib64/python3.6/asyncio/events.py", line 145, in _run
    self._callback(*self._args)
  File "/home/discord/.local/lib/python3.6/site-packages/discord/client.py", line 220, in _run_event
    await coro(*args, **kwargs)
  File "/home/discord/Penny-Dreadful-Discord-Bot/discordbot/bot.py", line 95, in on_member_update
    await after.add_roles(after, roles[0])
  File "/home/discord/.local/lib/python3.6/site-packages/discord/member.py", line 506, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
  File "/home/discord/.local/lib/python3.6/site-packages/discord/http.py", line 212, in request
    raise NotFound(r, data)
bakert commented 6 years ago

There doesn't seem to be an "after" line for this state change. ambnz gets three checks in quick succession at 21.50 then this error appears at 21.55 then the next mention of ambnz is after 22.00. I'll wait for another case. Theoretically you can't get to await after.add_roles(after, roles[0]) without visiting print('after', after) but an error waiting 5 minutes seems weird too. But maybe it is some kind of timeout with discord. More cases will help and they come up often enough.

bakert commented 6 years ago

15 of these in the last 2.5 hours so should hopefully be possible to work it out now.

Stack traces all identical and member info all approximately like this:

(<Member id=137374441330966528 name='mGz' discriminator='4146' bot=False nick='MyGaZz' guild=>, <Member id=137374441330966528 name='mGz' discriminator='4146' bot=False nick='MyGaZz' guild=>)
bakert commented 6 years ago

In the 1.0 API add_roles takes only one argument, the role to add. Not two (member, role) as previously.

47d4687a.