pycord icon indicating copy to clipboard operation
pycord copied to clipboard

AttributeError: 'Bot' object has no attribute 'all_commands'

Open rmenai opened this issue 5 years ago • 6 comments

Summary

Reloading or unloading an extension fails with an AttributeError

Reproduction Steps

  1. Create an extension file ping.py containing a cog with a slash command.
  2. Create another extension file extensions.py that reloads the first extension (example below)
  3. Observe the error

Minimal Reproducible Code

main.py

import discord

bot = discord.Bot()

# Load the extensions
bot.load_extension("cogs.ping")
bot.load_extension("cogs.extensions")

bot.run("TOKEN")

cogs/extensions.py

from discord.ext import commands


class Extensions(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.slash_command(guild_ids=[...])
    async def reload(self, ctx):
        self.bot.reload_extension("cogs.ping")
        await ctx.respond("Reloaded ping extension!")


def setup(bot):
    bot.add_cog(Extensions(bot))

cogs/ping.py

from discord.ext import commands


class Ping(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.slash_command(guild_ids=[...])
    async def ping(self, ctx):
        await ctx.respond("Pong!")


def setup(bot):
    bot.add_cog(Ping(bot))

Expected Results

The extension should reload successfully

Actual Results

Ignoring exception in command <discord.commands.SlashCommand name=reload>:
Traceback (most recent call last):
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 84, in wrapped
    ret = await coro(arg)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 526, in _invoke
    await self.callback(self.cog, ctx, **kwargs)
  File "/mnt/Data/Programming/Tests/pycord-issue/cogs/extensions.py", line 11, in reload
    self.bot.reload_extension("cogs.ping")
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/cog.py", line 822, in reload_extension
    self._remove_module_references(lib.__name__)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/cog.py", line 619, in _remove_module_references
    for cmd in self.all_commands.copy().values():
AttributeError: 'Bot' object has no attribute 'all_commands'

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

Traceback (most recent call last):
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/bot.py", line 390, in process_application_commands
    await ctx.command.invoke(ctx)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 133, in invoke
    await injected(ctx)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 90, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'Bot' object has no attribute 'all_commands'

Intents

members, presences, messages

System Information

- Python v3.9.5-final
- py-cord v2.0.0-alpha
    - py-cord pkg_resources: v2.0.0a4334+gb05b0835
- aiohttp v3.8.0
- system info: Linux 5.11.0-38-generic #42~20.04.1-Ubuntu SMP Tue Sep 28 20:41:07 UTC 2021

Checklist

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

Additional Context

This issue is a follow-up to #181. Note that loading an extension works as expected.

rmenai avatar Nov 05 '21 08:11 rmenai

Found out another error message: When I load an extension via a slash command it loads It successfully but when I reload it with the same command above I get a different error:

Ignoring exception in command <discord.commands.SlashCommand name=reload>:
Traceback (most recent call last):
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 84, in wrapped
    ret = await coro(arg)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 526, in _invoke
    await self.callback(self.cog, ctx, **kwargs)
  File "/mnt/Data/Programming/Tests/pycord-issue/cogs/extensions.py", line 10, in reload
    self.bot.reload_extension("cogs.ping")
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/cog.py", line 822, in reload_extension
    self._remove_module_references(lib.__name__)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/cog.py", line 616, in _remove_module_references
    self.remove_cog(cogname)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/cog.py", line 600, in remove_cog
    cog._eject(self)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/cog.py", line 484, in _eject
    bot.remove_application_command(command)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/bot.py", line 139, in remove_application_command
    return self.application_commands.pop(command.id)
KeyError: None

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

Traceback (most recent call last):
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/bot.py", line 390, in process_application_commands
    await ctx.command.invoke(ctx)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 133, in invoke
    await injected(ctx)
  File "/mnt/Data/Programming/Tests/pycord-issue/venv/lib/python3.9/site-packages/discord/commands/commands.py", line 90, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: KeyError: None

Command to load the extension:

@commands.slash_command(guild_ids=[...])
async def load(self, ctx):
    self.bot.load_extension("cogs.ping")
    await ctx.respond("Loaded ping extension!")

rmenai avatar Nov 06 '21 19:11 rmenai

you should use commands.Bot(), not discord.Bot(). You can still use slash commands with it. I used to have the same error. try it and then lmk the results

pratheek78 avatar Nov 08 '21 13:11 pratheek78

you should use commands.Bot(), not discord.Bot(). You can still use slash commands with it. I used to have the same error. try it and then lmk the results

That seems to get rid of the first error, but if I reload a command more than one time I get the second error.

rmenai avatar Nov 09 '21 18:11 rmenai

It is not supported to reload application commands on runtime.

Lulalaby avatar Nov 09 '21 18:11 Lulalaby

So is it a discord limitation or is it just not implemented yet?

rmenai avatar Nov 09 '21 18:11 rmenai

We won't implement this because it's not intended by discord.

Lulalaby avatar Nov 09 '21 18:11 Lulalaby