python - 我需要制作一个 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!")
请通过重写代码或告诉我提示来提供帮助。
解决方案
正如 Dextication 所说,在这里使用 while 会阻止整个机器人。您需要在后台更新计时器。文档提供了如何设置的示例。
然后我建议你使用一个类来创建你的机器人,将更新的计时器保持在它的一个属性中(比如说self.timer
),然后这些命令也被定义为这个类中的方法(或外部 cog)将能够访问self.timer
和添加它在命令的答案中。
推荐阅读
- javascript - 将 keyup 方法绑定到两个 jQuery 对象
- c# - Visual Studio docker工具在调试时发布端口
- wcf - 如何使用 SOAP UI 工具测试 WCF wsHttpBinding 端点以及证书?
- bash - 在 CSV 文件中搜索第一列中的值,如果找到,将第二列的值向下移动一行
- prolog - 立即在控制台上写入
- swift - 应用内购买不更新沙盒下的所有订阅?
- java - 具有多个数据库服务器的分布式事务似乎不可靠
- python-3.x - 如何让输出一直问我直到满足条件?
- javascript - 如何在画布上绘制流畅的线条而不清除它?
- python - 获取另一个模块的 globals()