0

I'm trying to make a trivia bot and it has a command that makes it countdown from 30-1 in seconds. When it hits 1 I don't know how to make say something in the discord chat.

I've already tried looking through the API.

questionTimer = 30

#while questionTimer > 1:
  #questionTimer = questionTimer - 1
  #time.sleep(1)
  #print (questionTimer)

I'm hoping that it can say Times up in the chat when questionTimer = 1

Oof
  • 39
  • 1
  • 2

2 Answers2

2

If you want it to print out every number you can do something like

@client.command()
async def quiz():
  seconds = 30
  while seconds > 0:
    await client.say(seconds)
    await asyncio.sleep(1)
    seconds-=1

  await client.say("Done")

But if you just want to make it wait 30 seconds and then display a message you can do something like

@client.command()
async def quiz():
  await asyncio.sleep(30)
  await client.say("Done")
Tristo
  • 2,328
  • 4
  • 17
  • 28
0

Depending if you are using the rewrite or the old async version of discord.py I would recommend the following :

Discord.py async (0.16.x) :

@client.event
async def on_message(self, message):
    if message.content.startswith('!quiz'):
        quizMsg = 'Question of the quiz'
        msg = await client.say(f"{quizMsg}\n\n{secs}s left !")
        secs = 30
        while secs > 0:
            await asyncio.sleep(1)
            await client.edit_message(msg, f"{quizMsg}\n\n{secs}s left !")
            secs--
        await client.say("Time is up ! The answer was...")

Discord.py rewrite (1.0.x) :

@commands.command(name="quiz", aliases=["q"])
async def quiz():
    quizMsg = 'Question of the quiz'
    msg = await ctx.send(f"{quizMsg}\n\n{secs}s left !")
    secs = 30
    while secs > 0:
        await asyncio.sleep(1)
        await msg.edit(content = f"{quizMsg}\n\n{secs}s left !")
        secs--
    await ctx.send("Time is up ! The answer was...")

Mind the difference between the two methods

Keelah
  • 192
  • 3
  • 15
  • You can't do `secs--` in python, you can read more about it [here](https://stackoverflow.com/a/3654936/9917504) – Tristo Jan 21 '19 at 19:33