首页 > 解决方案 > 我需要制作一个 Discord 机器人,我可以在其中制作它,以便“;time”命令表示计时器达到零之前的剩余时间

问题描述

我正在 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!")

请通过重写代码或告诉我提示来提供帮助。

标签: pythonbotsdiscorddiscord.py

解决方案


正如 Dextication 所说,在这里使用 while 会阻止整个机器人。您需要在后台更新计时器。文档提供了如何设置的示例。

然后我建议你使用一个类来创建你的机器人,将更新的计时器保持在它的一个属性中(比如说self.timer),然后这些命令也被定义为这个类中的方法(或外部 cog)将能够访问self.timer和添加它在命令的答案中。


推荐阅读