metacore
metacore

Reputation: 55

Why slash command doesnt appear with Cog?

I have this code in my cog:

class Admin(commands.Cog):
def __init__(self, bot: discord.Bot):
    self.bot = bot

@slash_command(name="create-embed", description="Test")
async def create_embed(self, ctx: discord.ApplicationContext):
    block.has_role(ctx, env.METACORE_MODERATOR_ROLE_ID)

    embed_title = 'Test title'
    view = v.CreateEmbedView()
    view.bot = self.bot
    view.author_id = ctx.author.id
    result_embed = discord.Embed(title=embed_title)
    embed_message = await ctx.respond(embed=result_embed, view=view, ephemeral=True)
    view.embed = result_embed
    view.embed_message = embed_message

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

But the slash command create-embed doesn't appears. And in my main.py I have:

@bot.event
async def on_ready():
    admin.setup(bot)

Upvotes: 0

Views: 438

Answers (1)

Middledot
Middledot

Reputation: 112

I'm guessing from the admin.setup that you're importing the cog file.

First off is that application commands (slash commands) are registered before on_ready is called in pycord. So if you want to add cogs like this, you'd need to do it as such:

@bot.event
async def on_connect():
    admin.setup()
    await bot.sync_commands()

HOWEVER, cogs aren't supposed to be added like this. Instead, you should not import the cog file and use load_extension to load your cog. Example:

@bot.event
async def on_connect():
    bot.load_extension("admin")
    await bot.sync_commands()

This way is cleaner and allows you to reload your cogs at runtime. Also a better place to ask questions like this is through the support server.

Upvotes: 1

Related Questions