【问题标题】:Wait for valid DateTime reply in Discord.py在 Discord.py 中等待有效的 DateTime 回复
【发布时间】:2021-09-02 07:12:55
【问题描述】:

我正在尝试创建一个命令等待格式正确的日期和时间字符串。我正在使用带有检查功能的 wait_for 命令,但它还会检查日期是否在将来的某个地方。 如果格式不正确,我希望机器人向用户发送消息。有没有更好的方法来验证字符串,因为目前如果不正确,它会自动抛出错误。

from dateutil.parser import parse
from datetime import datetime 

    @commands.command(name='Due', brief="", description="Add an item that's due")
    @commands.cooldown(1, 2)
    async def add_due(self, ctx, *, message):
        await ctx.send("When is it due? (DD/MM/YYYY HH:MM:SS) \n NB: Time is optional")

        def check(m):
            if m.author == ctx.author:
                x = parse(m.content)
                return isinstance(x, datetime) and x > datetime.now()
            # send error message if not in correct format
        
        msg = await self.bot.wait_for("message", check=check,timeout=30)
        due = parse(msg.content)```

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您应该尝试在 python 中使用 Exception。超级好用。

    Here is one solution for DateTime

    And exception statements

    【讨论】:

      猜你喜欢
      • 2020-11-07
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2019-08-22
      • 2014-12-04
      • 2021-01-14
      • 2018-12-09
      相关资源
      最近更新 更多