【问题标题】:How would I go about allowing a command only during a certain time window?我将如何仅在特定时间窗口内允许命令?
【发布时间】:2018-03-08 02:23:05
【问题描述】:

我试图让命令仅在 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

【问题讨论】:

  • tm_sec 将是[0, 61] 范围内的整数。所以300 &gt;= sec 总是正确的。 300 是什么?
  • 只是一个 300 秒的数字文字。我想我应该使用分钟。我想我可以解决这个问题。如果它也不能按照我想要的方式工作,我稍后会再发一篇文章。另外,非常感谢。你帮了我不少忙。我欠你一个。

标签: python-3.x python-asyncio discord discord.py


【解决方案1】:

您可以使用localtimestruct_time 的形式获取当前时间。这为您提供了当地的小时、分钟等。然后您可以将它们与您的目标条件进行比较。例如,如果您只想在上午 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")

【讨论】:

  • 一旦用户使用该命令,我是否可以将其重置?
  • 您是在寻找人们可以发出命令的固定时间窗口,还是在寻找具有冷却时间的命令?
  • 好吧,我使用的是 tm_sec 而不是 tm_hour,我需要 sec 变量,它本质上是我的循环控制变量。 (我将 if 语句更改为 while 循环,以便它会迭代)进行设置,以便我的循环评估为 false。例如,在用户使用 !pick 命令后,300 >= sec 应该评估为 false。
  • 我无法完全想象你在说什么。你能分享一些代码吗?
  • 当然对不起,
猜你喜欢
  • 1970-01-01
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多