Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute error when checking for permissions with has_permissions #2113

Closed
3 tasks done
Mihitoko opened this issue Jun 11, 2023 · 2 comments · Fixed by #2253
Closed
3 tasks done

Attribute error when checking for permissions with has_permissions #2113

Mihitoko opened this issue Jun 11, 2023 · 2 comments · Fixed by #2253
Labels
bug Something isn't working on hold priority: medium Medium Priority

Comments

@Mihitoko
Copy link
Contributor

Mihitoko commented Jun 11, 2023

Summary

When inviting bot only with applications.commands scope has_permissions will fail due to an attribute error.

Reproduction Steps

Register a command with @has_permissions(administrator=True) for example.
Invite the bot only with applications.commands scope.
Try to execute the command.

See how you get an attribute error in the console.
AttributeError: 'PartialMessageable' object has no attribute 'permissions_for'

Minimal Reproducible Code

import discord
from discord.ext.commands import has_permissions

bot = discord.Bot()


@bot.event
async def on_ready():
    print(f"We have logged in as {bot.user}")


@has_permissions(administrator=True)
@bot.slash_command(guild_ids=[guild_id])
async def hello(ctx):
    await ctx.respond("Hello!")


bot.run("TOKEN")

Expected Results

We should get a more telling error message that the channel was not cached and for that reason we cant check permissions.

Actual Results

We get an error
AttributeError: 'PartialMessageable' object has no attribute 'permissions_for'

Since we did not get an GUILD_CREATE event for the guild because it was not invited with the bot scope

Traceback:
Ignoring exception in on_interaction
Traceback (most recent call last):
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\client.py", line 378, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\bot.py", line 1167, in on_interaction
await self.process_application_commands(interaction)
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\bot.py", line 848, in process_application_commands
await self.invoke_application_command(ctx)
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\bot.py", line 1114, in invoke_application_command
await ctx.command.invoke(ctx)
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\commands\core.py", line 372, in invoke
await self.prepare(ctx)
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\commands\core.py", line 292, in prepare
if not await self.can_run(ctx):
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\commands\core.py", line 400, in can_run
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\utils.py", line 699, in async_all
for elem in gen:
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\commands\core.py", line 400, in
return await async_all(predicate(ctx) for predicate in predicates) # type: ignore
File "C:\Users\Mihito\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\core.py", line 2129, in predicate
permissions = ctx.channel.permissions_for(ctx.author) # type: ignore
AttributeError: 'PartialMessageable' object has no attribute 'permissions_for'

Intents

default

System Information

  • Python v3.9.13-final
  • py-cord v2.4.1-final
  • aiohttp v3.8.4
  • system info: Windows 10 10.0.19045

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

@Mihitoko Mihitoko added the unconfirmed bug A bug report that needs triaging label Jun 11, 2023
@JustaSqu1d JustaSqu1d added bug Something isn't working priority: medium Medium Priority and removed unconfirmed bug A bug report that needs triaging labels Jun 11, 2023
@Lulalaby
Copy link
Member

Lulalaby commented Jun 11, 2023

Inviting bots only with app command scope is meant for http interaction bots normally.

@Mihitoko
Copy link
Contributor Author

Inviting bots only with app command scope is meant for http interaction bots normally.
While this is true in my opinion there should still be a type check.

The type hints explicitly say that this can be a PartialMessagable.
Of course its a very niche problem but imo still worth to fix.

Middledot added a commit that referenced this issue Oct 29, 2023
Dorukyum added a commit that referenced this issue Nov 29, 2023
* fix(commands): permissions error with appcmd scope

Refer to #2113

* chore(commands): don't put partial guild in cache

* Apply suggestions from code review

Co-authored-by: Dorukyum <[email protected]>
Signed-off-by: Middledot <[email protected]>

* Update CHANGELOG.md

Signed-off-by: Dorukyum <[email protected]>

---------

Signed-off-by: Middledot <[email protected]>
Signed-off-by: Dorukyum <[email protected]>
Co-authored-by: Dorukyum <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working on hold priority: medium Medium Priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants