我正在 Discord 上制作一个倒计时机器人,但需要您的帮助。我想这样做,以便您可以使用";time"更新剩余的时间量,并且当倒计时达到零时它会自动发送消息。以下是我当前的代码:
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!")
请通过重写代码或告诉我提示来提供帮助。
正如Dextication所说,在这里使用while会阻止整个机器人。您需要在后台更新计时器。这些文档提供了有关如何设置的示例。
然后,我建议您使用类来创建机器人,将更新的计时器保留在其属性之一(假设self.timer
)中,也定义为此类中的方法(或外部齿轮)的命令将能够访问self.timer
并将其添加到命令的答案中。