Search code examples
pythondiscordpycord

Add slash command in commands.Bot subclass


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")

Solution

  • 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)