Mario Arquette
Mario Arquette

Reputation: 25

Create a seperate stop commnad that can be used to break a loop in discord.py

I am currently trying to code a quiz bot in discord, and right now I am having trouble trying to create a command that can be used to stop a session. This is what I've come up with so far:

@client.command()
async def ask(ctx):

    quiz_data = {
        'question_1': (["1", "one"], "one"),
        'question_2': (["2", "two"], "two"),
    }
    question = random.choice(list(quiz_data.keys()))
    answers, hint = quiz_data[question]

    await ctx.send("What is the answer to this question?")
    await asyncio.sleep(1)
    await ctx.send(question)

    def check_sender(msg): 
        return msg.author == ctx.author and msg.channel == ctx.channel

    def check_answer(msg):
        return any(answer in msg.content.lower() for answer in answers)
    try:
        async with timeout(10):
            while True:
                def running(self):
                    return True
                msg = await client.wait_for('message', check=check_sender)
                if check_answer(msg):
                    await ctx.send("good")
                    break
                else:
                    print('someone got it wrong lol')
    except asyncio.TimeoutError:
        await ctx.send(f"What is the answer to this question? hint: {hint}")
        await ctx.send(question)

        msg = await client.wait_for('message', check=check_sender)
        if check_answer(msg):
            await ctx.send("good")
        else:
            await ctx.send("wrong.")


@client.command()
async def stopg(ctx):
    if running() == True:
        await ctx.send("Round is stopping...")
    else: 
        await ctx.send("No round active")

The command stopg does not work at the moment simply because running is only defined in the previous command. This is the attempt I decided to use cause I think this can explain what I want to accomplish the easiest; Create an indicator that can tell whether the loop or command is running or not, which will be applied as an if statement in the stop command. I honestly haven't done as much research about this since this just stormed into me a few days ago.

Sorry if this didn't make much sense, i'll be willing to answer any questions. Or any other way if what I mentioned is overcomplicating things. Thanks so much~.

Upvotes: 0

Views: 52

Answers (1)

pThreadMutex
pThreadMutex

Reputation: 51

Instead of doing a while True you could do something like while someVariable then program the stop function to change the value of the variable someVariable. And then you could also check whether the loop is running or not by checking the variable someVariable itself.

Cheers,

Upvotes: 2

Related Questions