#Adding slash commands in a commands.Bot class

1 messages · Page 1 of 1 (latest)

rancid elk
#

I'm trying add add a slash command to my client class. I did get the command
running in one of my cogs, but I'm not getting it working in the following use case:

class ClanBotjasClient(commands.Bot):
    ...
    @commands.slash_command(name="test",
                       description="test command",
                       guild_ids=settings.DISCORD_GUILD_IDS )
    async def test(self, ctx: discord.ApplicationContext):
        await ctx.respond("test message")
       

Does anyone know if this is actually supposed to work? Or am I missing something?

cunning tinsel
#

If you don’t use message command

#

You can use discord.Bot

#

@commands.slash_command
Is in discord.commands

#

Not discord.ext.commands

#

Hope it can help you

rancid elk
#

Unfortunately I still have one message command I can't get rid of

desert compass
rancid elk
#

Because this is the cog manager

#

I want to create commands that manage cogs

rancid elk
desert compass
#

You create at the moment a bot class and not a cog?

rancid elk
#

Yes, I'm trying to create this command within the commands.Bot class

#

I also know the following works:

class ClanBotjasClient(commands.Bot):
    ...
client = ClasBotJasClient(...)
@client.slash_command(name="test",
                      description="test command",
                      guild_ids=settings.DISCORD_GUILD_IDS )
 async def test(self, ctx: discord.ApplicationContext):
     await ctx.respond("test message")

desert compass
#

Is that your main file?

rancid elk
#

Yes

desert compass
#

You can add to the client = ()..
debug_guilds=[id]

#

So you dont have to copy and paste the guild id stuff in all commands

rancid elk
#

Thanks for the suggestion, I'll take it into account for the future.
Back to the original problem. Do you know if what I'm suggesting in my initial message is possible?

desert compass
#

You try to get it in a cog right?

rancid elk
#

No

wispy juncoBOT
#

Here's the slash cog example.

rancid elk
#

I'll give you the full code example

desert compass
#

Is it a big file?

rancid elk
#
class ClanBotjasClient(commands.Bot):
    
    def __init__(self):
        super().__init__(command_prefix=commands.when_mentioned_or("!"), intents=settings.INTENTS)
        for cog in settings.DISCORD_COGS:
           self.load_extension(f'cogs.{cog.name}')
           self.add_application_command

    @commands.slash_command(description="load a cog",
                          guild_ids=settings.DISCORD_GUILD_IDS,
                          default_permission=False)
    @commands.has_role(settings.DISCORD_COMMAND_PERMISSION_ROLE)
    @option(name="cog", 
            description="Select Cog", 
            required=True,
            choices=settings.DISCORD_COGS
    )
    async def load(self, ctx: discord.ApplicationContext, cog: str):
        """
        Load a cog
        :param ctx: Original slash command context
        :param cog: Name of the cog to load
        :return:
        """
        cog, className = cog.lower(), cog
        try:
            self.load_extension(f"cogs.{cog}")
            bot = self.get_cog(className)
            await bot.on_ready()
            await ctx.respond(f"Cog: \"{cog}\" loaded.")
        except discord.errors.ExtensionAlreadyLoaded:
            await ctx.respond(f"Cog: \"{cog}\" already loaded.")
#

This is one of the functions

#

It's a function I use for loading cogs

#

I don't think this function would belong in a Cog itself

#

that would make it kind of self referential

fierce ridge