I Need To Make A Discord Bot, Where I Can Make It So The ";time" Command Says The Amount Of Time Left Until The Timer Hits Zero

- 1 answer

I am making a countdown bot on Discord but need your help. I want to make it so you can update the amount of time left with ";time", and also so it automatically sends a message when the countdown hits zero. The following is my current code:

    from discord.ext import commands
import discord
import time
import asyncio

Client = commands.Bot(commands.when_mentioned_or('...'))
bot = commands.Bot(command_prefix=";", status=discord.Status.idle, activity=discord.Game(name="Counting"))

releasetime = 10
countdowndone = False

while releasetime >0:
    releasetime -=1

if releasetime <= 0:
    print("Countdown finished")
    countdowndone = True

async def time(ctx):
    global releasetime
    await bot.say("MineSaga will be up in" 'releasetime' "seconds.")

async def on_ready():
    print("Bot ready")
    await bot.change_presence(game=discord.Game(name=";time", type=1))

async def ping(ctx):
   await bot.say(":ping_pong: Pong!")

Please help out by either rewriting the code or telling me tips.



As Dextication said, using while here will block the whole bot. You need to update your timer in the background. The docs provide an example on how to setup one.

I'd then suggest you use a class to create your bot, keeping the updated timer in one of its attribute (let's say self.timer) the commands also defined as methods in this class (or an external cog) will then be able to access self.timer and add it in the answer to the command.