DeadPool
DeadPool

Reputation: 255

How to convert Discord bot commands to hybrid command

I'm trying to convert my Discord Bot commands to hybrid commands. When I don't use the hybrid_command decorator, the slash commands work.

The error says the callback must be a coroutine. What does it mean? What am I missing in the code?

main.py

class MyBot(commands.Bot):

    def __init__(self):
        intents=discord.Intents.all()
        intents.message_content = True

        super().__init__(
            command_prefix='!',
            intents=discord.Intents.all()
        )

        self.initial_extensions = [
            "cogs.user_commands"
        ]


    async def setup_hook(self):
        for ext in self.initial_extensions:
            await self.load_extension(ext)

        await bot.tree.sync(guild = discord.Object(id=191453821174531128))

user_commands.py

class user_commands(commands.Cog):

    def __init__(self, bot):
        self.bot = bot
        bot.remove_command("help")

    @commands.hybrid_command(name='help', with_app_command=True)
    @app_commands.command()
    async def help(self, interaction: discord.Interaction, command: Optional[str]):
        ......

        await interaction.response.send_message(embed=embed, ephemeral = True)

    @help.autocomplete('command')
    async def help_autocomplete(self,
        interaction: discord.Interaction,
        current: str,
    ) -> List[app_commands.Choice[str]]:
            .....

Error:

enter image description here

Upvotes: 3

Views: 8486

Answers (1)

Aluerie
Aluerie

Reputation: 374

First of all, your error means that decorators @commands.hybrid_command(...) and @app_commands.command() do not go well together. You either define hybrid command, slash command or text-chat command.

Next moment, hybrid commands have commands.Context as their argument so we need to replace interaction parameter with that and adjust your ...... code accordingly -> replace usage of interaction with its analogical attributes/methods from ctx of said commands.Context class,

i.e. replace

  • interaction.user with ctx.author
  • interaction.channel with ctx.channel
  • interaction.response.send_message with ctx.send or ctx.reply
  • etc

So your code in user_commands.py would look like this.

@commands.hybrid_command(name='help', with_app_command=True)
async def help(self, ctx: commands.Context, command: Optional[str]):
    ......

    await ctx.send(embed=embed, ephemeral=True)

@help.autocomplete('command')
async def help_autocomplete(self,
                            interaction: discord.Interaction,
                            current: str,
                            ) -> List[app_commands.Choice[str]]:
    .....

PS. it's not advisable to remove help command. I suggest reading this amazing github gist A basic walkthrough guide on subclassing HelpCommand

Upvotes: 6

Related Questions