因此,我尝试使用discord.py创建一个提醒函数。这是我的代码:
@client.command(name = "reminder", brief = "I'll send a message to the future.",
description = "State what I have to remind you, as well as when and in which channel and I'll do it! "
"Write d for days, h for hours, m for minutes and s for seconds after the number and then "
"what you want to be remided of. For example: >reminder 1h 30m eat chocolate",
aliases = ["remind", "remindme", "remindto"])
async def reminder(ctx, time, *, reminder):
user = ctx.message.author
seconds = 0
if reminder is None:
ctx.send("Please, tell me what you want me to remind you about!")
if time.lower().endswith("d"):
seconds += float(time[:-1]) * 60 * 60 * 24
counter = f"{seconds // 60 // 60 // 24} days"
if time.lower().endswith("h"):
seconds += float(time[:-1]) * 60 * 60
counter = f"{seconds // 60 // 60} hours"
if time.lower().endswith("m"):
seconds += float(time[:-1]) * 60
counter = f"{seconds // 60} minutes"
if time.lower().endswith("s"):
seconds += float(time[:-1])
counter = f"{seconds} seconds"
if seconds == 0:
await ctx.send("You can't tell me that!")
else:
await ctx.send(f"Alright, I will remind you about {reminder} in {counter}.")
await asyncio.sleep(seconds)
await ctx.send(f"Hi, <@{user.id}>, you asked me to remind you about {reminder} {counter} ago.")
return
我的问题是,当有人为“时间”写了不止一个论点时,我不知道如何让它起作用。例如,如果我调用函数>reminder 1h 30min eat chocolate
,它会在1小时内提醒我“吃巧克力30分钟”,而不是在1小时30分钟内提醒我。我不知道是否有办法解决这个问题(除了写1.5h)。任何输入都是有用的。非常感谢
这就是我最终解决问题的原因:
因此,机器人将分别向用户询问提醒和时间,而不是干扰函数的参数
用户输入的消息被“某处”分割成参数,然后调用您发布的异步方法。用户输入的“拆分”在您显示的代码中是而不是完成的
如果要将
'1d 3hours 52sec Check StackOverflow for answers.'
作为用户输入处理,则需要在使用拆分参数调用async def reminder(ctx, time, *, reminder)
之前更改此消息的方式split
您需要更改它,以便
time
作为'1d 3hours 52sec'
提供,而reminder
作为'Check StackOverflow for answers.'
提供:如果无法更改此拆分行为,请禁止在用户使用的消息的时间部分内使用空格:
似乎您的用户文本当前在空格处被拆分,第一个是为
time
提供的,其余的是作为reminder
消息提供的如果禁止在时间组件中使用空格,则可以更改方法以正确解析输入的时间,如
'1d3hours52sec Check StackOverflow for answers.'
这可能是处理上述消息的方法的非异步实现:
一些助手方法和导入:
方法上下文对象的模拟:
以及更改后的方法(非异步和出于minimal reproducible example目的而模拟):
这是可以调用的,如下所示:
并产生以下输出:
相关问题 更多 >
编程相关推荐