Search code examples
pythonnlp-question-answeringnextcord

I wanna make a command in a command in nextcord


Ok so I want to make like a sub-command in nextcord using python

it will be like this:

Me: .question

Bot: How many centimeters is in 1 meter?

Me: 100 cm

Bot: Correct!

Here is my code currently...

from nextcord.ext import commands



class Fun(commands.Cog, name="Fun Cog"):
    
    def __init__(self, bot:commands.Bot):
        self.bot = bot


    @commands.command(aliases = ["q/a"])
    async def question(self, ctx: commands.Context):
        await ctx.send("How many centimeters is in 1 meter?")


def setup(bot: commands.Bot):
    bot.add_cog(Fun(bot))

Any ideas?


Solution

  • You can use wait_for() function to do it.

    import asyncio
    
    @commands.command(aliases = ["q/a"])
        async def question(self, ctx: commands.Context):
            await ctx.send("How many centimeters is in 1 meter?")  # your question
            try:
                message = await self.bot.wait_for('message', check=lambda m: m.author == ctx.author and m.channel == ctx.channel, timeout=25)  # you can specify timeout here
                await ctx.send("Correct!" if message.content == "100 cm" else "Incorrect!")
                # `message.content` is the answer
            except asyncio.TimeoutError:
                pass  # if time limit exceeded
                
    

    You also can create more complicated system using a dictionary of questions and answers to them. I think that it's not so difficult to implement.