Discord.py - 包含多个时间的提醒命令
Discord.py - remind command with multiple time included
我正在尝试制作一个带有提醒命令的 discord 机器人。我希望提醒命令能够像 ;remind 12.5m
或 ;remind 1h 12m 30s
那样做。但现在提醒命令只能像;remind 45m
、;remind 2h
、;remind 9m
这样使用。基本上,该命令仅在仅包含一次时才有效。我不确定如何使命令在包含多个时间的情况下工作,有人可以帮助我吗?
@client.command(case_insensitive=True, aliases=["reminder", "rm"])
async def remind(ctx, time, *, reminder="something"):
user = ctx.author.mention
channel = client.get_channel(12345678912345)
seconds = 0
log = discord.Embed(color=0xe9a9a9, timestamp=datetime.utcnow())
embed = discord.Embed(color=0xe9a9a9)
if time.lower().endswith("d"):
seconds += int(time[:-1]) * 60 * 60 * 24
counter = f"{seconds // 60 // 60 // 24} days"
if time.lower().endswith("h"):
seconds += int(time[:-1]) * 60 * 60
counter = f"{seconds // 60 // 60} hours"
if time.lower().endswith("m"):
seconds += int(time[:-1]) * 60
counter = f"{seconds // 60} minutes"
if time.lower().endswith("s"):
seconds += int(time[:-1])
counter = f"{seconds} seconds"
if seconds == 0 or seconds > 7776000:
await ctx.send("Please specify a valid time.")
if reminder is None:
await ctx.send("Please tell me what to remind you.")
else:
log.set_author(name=f"{ctx.author.display_name}#{ctx.author.discriminator} - Remind", icon_url=ctx.author.avatar_url)
log.set_footer(text=f"{counter} | {reminder}")
embed.add_field(
name="**Reminder**",
value=f"{user}, you asked me to remind you to `{reminder}` `{counter}` ago."
)
await ctx.send(f"Alright, I will remind you about `{reminder}` in `{counter}`.")
await asyncio.sleep(seconds)
await ctx.author.send(embed=embed)
await channel.send(embed=log)
return
您似乎在使用 *
作为提醒,这意味着在第一个提醒之后提供的所有时间都将存储在您的 reminder
变量中。您可以在字符串的开头搜索提醒变量和有效时间。
当你这样做时
remind(ctx, time, *, reminder='something')
*
位表示当您调用该函数时该点之后的任何参数 have to be a named ones。
相反,如果将 *
放在 time
前面,那么您将触发完全不同的行为,并且时间将成为给定的所有不同时间的列表。像这样。
async def remind(ctx, *times, reminder="something"):
for time in times:
# move the code here
# Getting rid of the return statement at the end
然而它会运行每个计时器顺序,等待倒数第二个直到第一个完成。如果您想一次启动所有计时器,因为我们已经在使用 asyncio,我们可以使用 asyncio.gather.
轻松完成此操作
async def remind(ctx, *times, reminder="something"):
asyncio.gather(*[_remind(ctx, time, reminder) for time in times])
async def _remind(ctx, time, reminder):
# All of the code that was in your original remind runction
# would go here instead
顺便说一下,星星(async.gather 中的那颗)是 acting in a different way。
我正在尝试制作一个带有提醒命令的 discord 机器人。我希望提醒命令能够像 ;remind 12.5m
或 ;remind 1h 12m 30s
那样做。但现在提醒命令只能像;remind 45m
、;remind 2h
、;remind 9m
这样使用。基本上,该命令仅在仅包含一次时才有效。我不确定如何使命令在包含多个时间的情况下工作,有人可以帮助我吗?
@client.command(case_insensitive=True, aliases=["reminder", "rm"])
async def remind(ctx, time, *, reminder="something"):
user = ctx.author.mention
channel = client.get_channel(12345678912345)
seconds = 0
log = discord.Embed(color=0xe9a9a9, timestamp=datetime.utcnow())
embed = discord.Embed(color=0xe9a9a9)
if time.lower().endswith("d"):
seconds += int(time[:-1]) * 60 * 60 * 24
counter = f"{seconds // 60 // 60 // 24} days"
if time.lower().endswith("h"):
seconds += int(time[:-1]) * 60 * 60
counter = f"{seconds // 60 // 60} hours"
if time.lower().endswith("m"):
seconds += int(time[:-1]) * 60
counter = f"{seconds // 60} minutes"
if time.lower().endswith("s"):
seconds += int(time[:-1])
counter = f"{seconds} seconds"
if seconds == 0 or seconds > 7776000:
await ctx.send("Please specify a valid time.")
if reminder is None:
await ctx.send("Please tell me what to remind you.")
else:
log.set_author(name=f"{ctx.author.display_name}#{ctx.author.discriminator} - Remind", icon_url=ctx.author.avatar_url)
log.set_footer(text=f"{counter} | {reminder}")
embed.add_field(
name="**Reminder**",
value=f"{user}, you asked me to remind you to `{reminder}` `{counter}` ago."
)
await ctx.send(f"Alright, I will remind you about `{reminder}` in `{counter}`.")
await asyncio.sleep(seconds)
await ctx.author.send(embed=embed)
await channel.send(embed=log)
return
您似乎在使用 *
作为提醒,这意味着在第一个提醒之后提供的所有时间都将存储在您的 reminder
变量中。您可以在字符串的开头搜索提醒变量和有效时间。
当你这样做时
remind(ctx, time, *, reminder='something')
*
位表示当您调用该函数时该点之后的任何参数 have to be a named ones。
相反,如果将 *
放在 time
前面,那么您将触发完全不同的行为,并且时间将成为给定的所有不同时间的列表。像这样。
async def remind(ctx, *times, reminder="something"):
for time in times:
# move the code here
# Getting rid of the return statement at the end
然而它会运行每个计时器顺序,等待倒数第二个直到第一个完成。如果您想一次启动所有计时器,因为我们已经在使用 asyncio,我们可以使用 asyncio.gather.
轻松完成此操作async def remind(ctx, *times, reminder="something"):
asyncio.gather(*[_remind(ctx, time, reminder) for time in times])
async def _remind(ctx, time, reminder):
# All of the code that was in your original remind runction
# would go here instead
顺便说一下,星星(async.gather 中的那颗)是 acting in a different way。