我将如何仅在特定时间 window 内允许命令?

How would I go about allowing a command only during a certain time window?

我试图让一个命令只在 discord.py 的特定时间内工作,有什么方法可以创建选择语句或使用 asyncio 函数吗?这是我的一些代码:

 @client.command(pass_context=True)
 async def pick(ctx):
    oneto5 = str(random.randrange(1, 5))
    await client.say("You recived " + oneto5 + " fish")

更新 这是我更新的代码,但它似乎给了我不稳定的输出。它没有正确迭代。

@client.command(pass_context=True)
async def pick(ctx):
sec = localtime().tm_sec
count = 0
print(sec)
while  (300 >= sec):
    if (1 <= lowFishRange <= 20):
        oneto5 = str(5)
        await client.say("You recived " + oneto5 + " fish")
        await asyncio.sleep(43200)
        return
    else:
        oneto5 = str(1)
        await client.say("You recived " + oneto5 + " fish")
        await asyncio.sleep(43200)
        return
else:
    await client.say("You seemed to have missed the mark, try again when a message appears...")
    await asyncio.sleep(43200)
    return

您可以获取当前时间 struct_time with localtime。这会为您提供当地的小时、分钟等。然后您可以将这些与您的目标条件进行比较。例如,如果您只想允许在上午 9 点到下午 5 点之间执行命令,您可以执行

from time import localtime

@client.command(pass_context=True)
async def pick(ctx):
   hour = localtime().tm_hour
   if 9 <= hour < 17:
       oneto5 = str(random.randrange(1, 5))
       await client.say("You recived " + oneto5 + " fish")