Add slash command in commands.Bot subclass

Question:

I’m trying to add slash command but it’s not appearing on my discord server.
I’ve tried using commands.slash_command() decorator as well but still not showing up.

I’m using py-cord 2.2.0 and python 3.9

Here’s the code:

import os, re, discord, asyncio
from discord.ext import commands, pages

class TalkToSushi(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.members = True
        intents.presences = True
        intents.message_content = True
        
        debug_guilds = ["998467063972626513"]
        super().__init__(command_prefix=";", 
                         intents=intents, 
                         debug_guilds=debug_guilds,
                         help_command=None)
        self.add_commands()

    def run(self, token):
        print("[*] Starting bot")
        super().run(token, reconnect=True)

    async def on_message(self, msg):
        if not msg.author.bot:
            await self.process_commands(msg)

    async def process_commands(self, msg):
        ctx = await self.get_context(msg, cls=commands.Context)

        if ctx.command is not None:
            await self.invoke(ctx)
        else: await self.process_tts(ctx)

    def add_commands(self):
        @self.command()
        async def help(ctx):
            paginator = pages.Paginator(
                pages=self.__embeds.help, 
                show_menu=True,
                menu_placeholder="Select help"
                )
            await paginator.send(ctx)

        @self.slash_command()
        async def test(ctx):
            await ctx.send("hi")
Asked By: Perdana Arta

||

Answers:

Commands should be placed outside the subclass.
Also for a custom help command, you need to remove the default help command of the bot first.

for example:

class TalkToSushi(commands.Bot):
    ...

bot = TalkToSushi(...)
bot.help_command = None  # remove the default help command

@bot.command()
async def help(ctx):
    paginator = pages.Paginator(
        pages=self.__embeds.help, 
        show_menu=True,
        menu_placeholder="Select help"
    )
    await paginator.send(ctx)
Answered By: Middledot

Im stupid, debug guilds should be list of int, not str.

Also, don’t make on_message() function if subclassing commands.Bot. it will break things

import os, re, discord, asyncio
from discord.ext import commands, pages

class TalkToSushi(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.members = True
        intents.presences = True
        intents.message_content = True
        
        debug_guilds = [998467063972626513]
        super().__init__(command_prefix=";", 
                         intents=intents, 
                         debug_guilds=debug_guilds,
                         help_command=None)

        self._commands()

    def run(self, token):
        print("[*] Starting bot")
        super().run(token, reconnect=True)

    def _commands(self):
        @self.command()
        async def help(ctx):
            paginator = pages.Paginator(
                pages=self.__embeds.help, 
                show_menu=True,
                menu_placeholder="Select help"
                )
            await paginator.send(ctx)

bot = TalkToSushi()
bot.run(token)
Answered By: Perdana Arta
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.