Skip to content

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

Description

@Paillat-dev

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

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugA bug report that needs triaging

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions