【发布时间】:2019-02-06 02:33:43
【问题描述】:
这段代码是一个设置持续时间为 90 秒的计时器,使用命令 '!t90' 启动。
如何编写选项以使用任意秒数的命令“!t”?
(或 30、45、60、90、120 在一个文件中(因为这些是我的服务器将在 99.9% 的时间内使用的唯一计时器))
谢谢
import discord
from discord.ext import commands
import asyncio
bot = commands.Bot(command_prefix="!")
counter_channel = None
task = None
async def ex(message):
global counter_channel
if counter_channel is not None:
await bot.send_message(
message.channel,
embed=discord.Embed("There is a counter in {}".format(counter_channel.mention), color=discord.Color.red() ))
return
counter_channel = message.channel
await bot.send_message(message.channel, "1:30")
await asyncio.sleep(30)
await bot.send_message(message.channel, "1:00")
await asyncio.sleep(30)
await bot.send_message(message.channel, "0:30")
await asyncio.sleep(20)
await bot.send_message(message.channel, "0:10")
await asyncio.sleep(10)
await bot.send_message(message.channel, "time")
counter_channel = None
@bot.command(pass_context=True)
async def t90(ctx):
global task
task = bot.loop.create_task(ex(ctx.message))
@bot.command(pass_context=True)
async def cancel(ctx):
global task, counter_channel
await bot.send_message(message.channel, "timer reset")
task.cancel()
task = None
counter_channel = None
bot.run('###token###')
【问题讨论】:
-
你应该修复你的缩进,很难说出你的一些协程中有什么。一种选择是将秒数作为参数:
async def t(ctx, seconds: int),然后将该数字传递给您的辅助协程 -
@PatrickHaugh 感谢您的快速回复,我添加了
async def t(ctx, seconds: int)和seconds:int = [30,45,60,90,120]。这允许我使用列出的命令,但如何更改倒计时 - 因为它仍然在每个命令上调用 1:30
标签: python timer discord discord.py