Pycord-Development / pycord

Pycord is a modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python
https://docs.pycord.dev
MIT License
2.7k stars 458 forks source link

Accessing server-specific attributes of discord.Member with user apps causes an exception #2515

Open ToothyDev opened 1 month ago

ToothyDev commented 1 month ago

Summary

If you access server-specific (aka Member related stuff) with a user-installed app, python throws an exception trying to get the member object. I also found that member.display_avatar in particular throws an error only if a server-specific avatar has been set.

Reproduction Steps

Make a user app Install it on your account Make a user command (context menu > user) Try to print member.roles, member.top_role, member.display_avatar (if set) etc., anything Member-specific See the console for the exception

Minimal Reproducible Code

@discord.user_command()
async def bugrepro(self, ctx: discord.ApplicationContext, member: discord.Member):
    print(member.roles)

Expected Results

Pycord handles the case of a user-installed app automatically, and simply returns None for e.g. member.roles, or the global avatar in the case of member.display_avatar

Actual Results

Pycord seems to not handle this at all and throws an exception

Intents

all / not applicable, happens regardless of intents

System Information

Checklist

Additional Context

Traceback for printing member.roles (project path replaced with ...)

Ignoring exception in command bugrepro:
Traceback (most recent call last):
  File "...venv\Lib\site-packages\discord\commands\core.py", line 138, in wrapped
    ret = await coro(arg)
          ^^^^^^^^^^^^^^^
  File "...venv\Lib\site-packages\discord\commands\core.py", line 1796, in _invoke
    await self.callback(self.cog, ctx, target)
  File "...cogs\user_commands.py", line 30, in bugrepro
    print(member.roles)
          ^^^^^^^^^^^^
  File "...venv\Lib\site-packages\discord\member.py", line 562, in roles
    role = g.get_role(role_id)
           ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get_role'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "...venv\Lib\site-packages\discord\bot.py", line 1137, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "...venv\Lib\site-packages\discord\commands\core.py", line 435, in invoke
    await injected(ctx)
  File "...venv\Lib\site-packages\discord\commands\core.py", line 146, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'NoneType' object has no attribute 'get_role'
Dorukyum commented 1 month ago

Seems to happen due to g being None, could be solved by adding a simple check.