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.75k stars 462 forks source link

`Member.guild_permissions` raises an error with user installed message commands when the bot is not present in the guild's server #2649

Closed Paillat-dev closed 1 week ago

Paillat-dev commented 1 week ago

Summary

When using a user-installed message command in a guild the bot is not present in, accessing ctx.author.guild_permissions raises an error

Reproduction Steps

Run the below code, user-install the bot and run the message command on a message in a guild the bot is not present in.

Minimal Reproducible Code

import os

from dotenv import load_dotenv

import discord
import logging

logging.basicConfig(level=logging.DEBUG)

load_dotenv()

TOKEN = os.getenv("TOKEN")

bot = discord.Bot()

@bot.message_command(
    contexts={
        discord.InteractionContextType.guild,
        discord.InteractionContextType.bot_dm,
        discord.InteractionContextType.private_channel,
    },
    integration_types={discord.IntegrationType.guild_install, discord.IntegrationType.user_install},
)
async def test_command(ctx: discord.ApplicationContext, message: discord.Message):
    await ctx.defer(ephemeral=True)
    await ctx.respond(f"{ctx.author.guild_permissions.administrator}")

@bot.listen()
async def on_ready():
    logging.info(f"Logged in as {bot.user.name}")

bot.run(TOKEN)

Expected Results

ctx.author.guild_permissions.administrator being false or ctx.author.guild_permissions being missing.

Actual Results

An error, traceback:

Ignoring exception in command test_command:
Traceback (most recent call last):
  File "C:\Users\Jérémie\Documents\GitHub\pycord\discord\commands\core.py", line 138, in wrapped
    ret = await coro(arg)
          ^^^^^^^^^^^^^^^
  File "C:\Users\Jérémie\Documents\GitHub\pycord\discord\commands\core.py", line 1904, in _invoke
    await self.callback(ctx, target)
  File "C:\Users\Jérémie\Documents\GitHub\pycord\thing.py", line 27, in test_command
    await ctx.respond(f"{ctx.author.guild_permissions.administrator}")
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Jérémie\Documents\GitHub\pycord\discord\member.py", line 706, in guild_permissions
    base.value |= r.permissions.value
                  ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'permissions'

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

Traceback (most recent call last):
  File "C:\Users\Jérémie\Documents\GitHub\pycord\discord\bot.py", line 1149, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "C:\Users\Jérémie\Documents\GitHub\pycord\discord\commands\core.py", line 435, in invoke
    await injected(ctx)
  File "C:\Users\Jérémie\Documents\GitHub\pycord\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 'permissions'

Intents

n/a

System Information

master

Checklist

Additional Context

No response