#๐Ÿ”’ Discord bot commands class

15 messages ยท Page 1 of 1 (latest)

sonic hamlet
#

Hi guys, how do you add a class containing commands into a bot inheriting from commands.Bot? For example

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

    @commands.command(name='roll')
    async def roll(self, ctx: commands.Context):
        print("rolling...")
        random_number = str(random.randint(1, 6))
        await ctx.send(random_number)

when I try to add it into the MyBot class

class MyBot(commands.Bot):
    def __init__(self, command_prefix, intents):
        super().__init__(command_prefix, intents=intents)
        self.add_cog(MyBotCog(self))  # <--- This line

      # Rest of implementation ...

and run the bot with the commnd prefix !, I get:
discord.ext.commands.errors.CommandNotFound: Command "roll" is not found
when I run !roll on Discord

Thanks for the help!

slate agateBOT
#

@sonic hamlet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

strong cove
#

!d discord.ext.commands.Bot.add_cog

slate agateBOT
#

await add_cog(cog, /, *, override=False, guild=..., guilds=...)```
This function is a [*coroutine*](https://docs.python.org/3/library/asyncio-task.html#coroutine).

Adds a โ€œcogโ€ to the bot.

A cog is a class that has its own event listeners and commands.

If the cog is a [`app_commands.Group`](https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.app_commands.Group) then it is added to the botโ€™s [`CommandTree`](https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.app_commands.CommandTree) as well.

Note

Exceptions raised inside a [`Cog`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Cog)โ€™s [`cog_load()`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Cog.cog_load) method will be propagated to the caller...
sonic hamlet
#

Thanks for the reply. I tried that, but i get

SyntaxError: 'await' outside async function
#

and also vscode adds a red underline to that line of code

strong cove
#

!d discord.ext.commands.Bot.setup_hook

slate agateBOT
#

await setup_hook()```
This function is a [*coroutine*](https://docs.python.org/3/library/asyncio-task.html#coroutine).

A coroutine to be called to setup the bot, by default this is blank.

To perform asynchronous setup after the bot is logged in but before it has connected to the Websocket, overwrite this coroutine.

This is only called once, in [`login()`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.login), and will be called before any events are dispatched, making it a better solution than doing such setup in the [`on_ready()`](https://discordpy.readthedocs.io/en/latest/api.html#discord.on_ready) event.

Warning

Since this is called *before* the websocket connection is made therefore anything that waits for the websocket will deadlock, this includes things like [`wait_for()`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.wait_for) and [`wait_until_ready()`](https://discordpy.readthedocs.io/en/latest/ext/commands/api.html#discord.ext.commands.Bot.wait_until_ready)...
strong cove
#

you can do it in here ^

sonic hamlet
#

it worked! i created a setup_hook method and the bot is working with commands

#

thanks a lot!

slate agateBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.