【问题标题】:Discord.py - remind command with multiple time includedDiscord.py - 包含多个时间的提醒命令
【发布时间】:2022-01-21 07:18:32
【问题描述】:

我正在尝试使用提醒命令制作一个不和谐的机器人。我希望提醒命令能够像;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

【问题讨论】:

    标签: python discord discord.py bots


    【解决方案1】:

    当你这样做时

    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

    【讨论】:

    • 成功了,非常感谢您的帮助!
    【解决方案2】:

    看起来您正在使用* 作为提醒,这意味着在第一个之后提供的所有时间都将存储在您的reminder 变量中。您可以在字符串的开头搜索提醒变量和有效时间。

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 1970-01-01
      • 2020-12-18
      • 2021-09-06
      • 2021-03-06
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多