【问题标题】:how to make command to stop reminder, discord.py?如何发出停止提醒的命令,discord.py?
【发布时间】:2021-09-08 17:04:53
【问题描述】:

我做了这个-

from asyncio import sleep as s

@client.command()
async def reminder(ctx, time: int, *, msg):
    while True:
        await s(time)
        await ctx.send(f'{msg}, {ctx.author.mention}')

效果很好。 但是你能帮我发出停止提醒的命令吗? 因为一旦开始就永远不会停止。 :(

谢谢!

【问题讨论】:

  • 你为什么要使用循环?
  • 你不能用你代码的当前逻辑来真正停止提醒,它甚至还没有接近解决方案,你应该看看 asyncio 任务。

标签: discord discord.py


【解决方案1】:

您只需将return 放在最后一行之后。这是我的做法,我也对代码进行了一些改进。

@bot.command()
async def reminder(ctx, time: int, *, msg):
  await ctx.send(f'Okay, {ctx.author.mention} I will remind you in {time} seconds to complete {msg}')
  await asyncio.sleep(time)
  await ctx.send(f'{msg}, {ctx.author.mention}')
  return

【讨论】:

  • 如果你这样做,你根本不需要while循环
  • 然后你也不需要return
  • 谢谢!但我不想停止命令,直到用户使用像 !reminder stop 这样的命令来停止该提醒。
  • 您的意思是要取消提醒?
【解决方案2】:

如果您希望能够通过!reminder stop 命令停止提醒,我建议使用当前正在运行提醒的用户的全局列表。这不适用于每个用户的多次提醒。 首先定义提醒列表:

reminderList = []

然后在命令中,如果用户启动提醒,则将其添加到提醒列表中,如果运行!reminder stop则将其删除:

@client.command()
async def reminder(ctx, time, *, msg):
    try: #detect if !reminder stop was used
        time = int(time)
    except ValueError:
        if time.lower() == "stop":
            try:
                reminderList.remove(ctx.author.id) #remove them from the list
            except ValueError:
                ctx.send("You don't have a reminder at the moment")
            return
    #add the user to the reminder list:
    reminderList.append(crx.author.id)
    #check if the user is still in the reminder list:
    while ctx.author.id in reminderList:
        await s(time)
        await ctx.send(f'{msg}, {ctx.author.mention}')

【讨论】:

    【解决方案3】:

    不要使用 while true ,因为它会导致问题,如果你不使用它,它在这里也没用,那么它也可以工作,除了重新启动机器人之外,没有什么可以停止 sleep 命令

    【讨论】:

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