0

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?

Aaseer
  • 117
  • 10
  • If I can guess at all what you are trying to ask here, you probably want to add a state variable to the bot to let it know that it's waiting for an answer, not any general input. – tripleee Nov 16 '21 at 13:03

1 Answers1

1

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.

Ratery
  • 2,863
  • 1
  • 5
  • 26
  • Hmm I will try that. Also, I was planning on using a dictionary but I couldn't figure how to make the answer part so thanks! Btw in here, do i HAVE to write self.bot or will it be self.client cuz i used client to make my bot. – Aaseer Nov 28 '21 at 11:46
  • Then you should write `self.bot`. – Ratery Nov 28 '21 at 11:51