0

I'm using discord.py and wanted to call a command from another command. There are many questions like that in stack overflow but the difference in mine is that I don't want the command that is going to be called to be available for the user to call.

For Example:

Let's say that I have an animals category and have two commands in that category such as (jokes, pictures) as an example. Then if the command prefix was !.

The user would type !animals joke or !animals pictures.

This should return the desired results.

I thought I could do that by:

animals.py:

@commands.command
async def animals(self, ctx, com_name):
    await ctx.invoke(self.bot.get_command(com_name))

jokes.py

@commands.command
async def joke(self, ctx):
   await ctx.send('a random joke')

Now if the user types !animals joke it will work but they would then be able to type !joke and that would also work. How could I only let the command be called if the category is also present.

Thanks.

2
  • It almost sounds like you want to create a command group, which is a way of organizing subcommands. Alternatively, you can just create a check that always prevents execution when you call the command directly. Commented Aug 15, 2020 at 22:23
  • @PatrickHaugh could you give me an example of how that is done (command group). Commented Aug 15, 2020 at 22:41

1 Answer 1

1

You can create an animals Group and then have joke be a subcommand:

@commands.group()
async def animals(self, ctx):
    pass

@animals.command()
async def joke(self, ctx):
   await ctx.send('a random joke')

Another option would be adding a check on joke that is always false

fail = commands.check(lambda ctx: False)

@fail
@commands.command()
async def joke(self, ctx):
   await ctx.send('a random joke')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.