pycord icon indicating copy to clipboard operation
pycord copied to clipboard

'SlashCommand' object has no attribute 'parent'

Open yagomichalak opened this issue 5 years ago • 7 comments

Summary

Whenever you put a Slash command in a cog, getting all commands from a cog will raise the error above.

Reproduction Steps

  • Create a slash command in a cog;
  • Try to get all commands from that same cog;
  • Observe.

Minimal Reproducible Code

cog.get_commands()

Expected Results

Get a full list of cog commands

Actual Results

ERROR: Command raised an exception: AttributeError: 'SlashCommand' object has no attribute 'parent' | Class: <class 'discord.ext.commands.errors.CommandInvokeError'> | Cause: 'SlashCommand' object has no attribute 'parent'

Intents

All intents

System Information

  • Python v3.9.6-final
  • pycord v2.0.0-alpha
    • pycord pkg_resources: v2.0.0a3872+g7e536bc8
  • aiohttp v3.7.4
  • system info: Windows 10 10.0.19042

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

No response

yagomichalak avatar Sep 08 '21 14:09 yagomichalak

This same error also appears when trying to add a user_command in a cog. It appeared for me on a cog reload

LilJess13 avatar Sep 09 '21 07:09 LilJess13

This error appears to always happen when using reload_extension. Not only does it give this error, it also disables all prefixed commands in cogs. Slash commands continue to work and the prefixed commands work after the bot is restarted.

LilJess13 avatar Sep 09 '21 16:09 LilJess13

In my case, all commands seem to work just fine, but the simple fact of having a slash/user/message command in a cog, makes this error to appear whenever one uses the cog.get_commands()

yagomichalak avatar Sep 09 '21 18:09 yagomichalak

This error appears to always happen when using reload_extension. Not only does it give this error, it also disables all prefixed commands in cogs. Slash commands continue to work and the prefixed commands work after the bot is restarted.

this is likely because both get_command and reload_extension access all_commands which appears to be the issue

BobDotCom avatar Sep 09 '21 20:09 BobDotCom

could you add more to your minimal reproducable code? This isn't descriptive enough for me to test

BobDotCom avatar Sep 10 '21 15:09 BobDotCom

could you add more to your minimal reproducable code? This isn't descriptive enough for me to test

This took me a while to figure out how to reproduce, but i got it.

Main file:

@bot.command(name = 'reload')
async def _reload(ctx):
    bot.reload_extension('cogs.CogTest')
    await ctx.send("reloaded cog")

for filename in os.listdir('./cogs'):
  if filename.endswith('.py'):
    bot.load_extension(f'cogs.{filename[:-3]}')

Cog:

from discord.ext import commands
from discord.app import commands as app
from discord import Option

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

    @app.slash_command(description = "I describe", guild_ids = [250507134876254208])
    async def reply(self, ctx):
        await ctx.respond(f"Hi")

    @commands.command()
    async def test(self, ctx):
        await ctx.send("test")

The order that the commands are in inside of the cog is important. The error only occurs when a prefixed command is after a slash command.

LilJess13 avatar Sep 10 '21 16:09 LilJess13

I have implemented a fix for this on the restructure branch that should work, let me know if there's something else wrong with it in your use case.

middledot avatar Sep 18 '21 18:09 middledot