Ad
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
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:
time.sleep(1)
print("a")
releasetime -=1
if releasetime <= 0:
print("Countdown finished")
countdowndone = True
@bot.command(pass_context=True)
async def time(ctx):
global releasetime
await bot.say("MineSaga will be up in" 'releasetime' "seconds.")
@bot.event
async def on_ready():
print("Bot ready")
await bot.change_presence(game=discord.Game(name=";time", type=1))
@bot.command(pass_context=True)
async def ping(ctx):
await bot.say(":ping_pong: Pong!")
Please help out by either rewriting the code or telling me tips.
Ad
Answer
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.
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad