【问题标题】:Reminder Command issue提醒命令问题
【发布时间】:2021-11-06 14:39:33
【问题描述】:

我的提醒命令有问题。当提醒命令运行时,它可以正常工作:它会在您提供的秒数内提醒您。但是当它运行提醒时,它不会运行任何其他命令,直到它有 DM 提醒你。

这里是代码。我不太擅长解释;你可以问问题。

@client.command()
async def remindersec(ctx,seconds=None):
  try:
    embed = discord.Embed(title = f"I will remind you in {seconds} seconds",
    description = f"You'll be DMed as a reminder.",
    color = 0xf461ff)

    now = datetime.now()
    current_time = now.strftime("%H:%M")

    embed.set_footer(text=f"Requested by  {ctx.author} at {current_time}")

    await ctx.send(embed=embed)
    timer = int(seconds)
    while timer >= 0:
      import time
      time.sleep(1)
      timer -= 1
    await ctx.author.send("Reminder!")
  except ValueError:
    embed = discord.Embed(title = "Error!",
    description = f"Please type a valid number.",
    color = 0xf461ff)

    now = datetime.now()
    current_time = now.strftime("%H:%M")

    embed.set_footer(text=f"Requested by  {ctx.author} at {current_time}")

    await ctx.send(embed=embed)
  except TypeError:
    embed = discord.Embed(title = "Error!",
    description = f"Please type a valid number.",
    color = 0xf461ff)

    now = datetime.now()
    current_time = now.strftime("%H:%M")

    embed.set_footer(text=f"Requested by  {ctx.author} at {current_time}")

    await ctx.send(embed=embed)

【问题讨论】:

  • time.sleep 冻结整个过程,所以我不建议你使用time.sleep
  • 另外,你为什么要在函数中导入time 模块?你应该把它放在你的文件顶部

标签: python discord discord.py command bots


【解决方案1】:

不要使用时间模块。使用异步。所以它会像:-

import asyncio

timer = int(seconds)
while timer >= 0:
  await asyncio.sleep(1)
  timer -= 1
  await ctx.author.send("Reminder!")
except ValueError:
  embed = discord.Embed(title = "Error!",
  description = f"Please type a valid number.",
  color = 0xf461ff)

【讨论】:

  • asyncio.sleep 是协程,需要等待,否则什么都做不了
  • 你应该可以使用asyncio.sleep(timer)而不是while循环。如果您仍然想使用它,ctx.author.send 可能不应该在循环中,虽然大声笑
猜你喜欢
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-11
  • 2021-09-06
  • 2021-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多