【发布时间】: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