如果没有发生禁止因素,有没有办法在经过一定时间后执行操作? discord.py重写

Is there a way to perform an action after a certain amount of time passed given that a prohibiting factor doesn't happen? discord.py rewrite

这个问题有点令人费解,所以这里有一个更深入的解释。我正在写一个不和谐的机器人,我正在尝试制作一个接受是或否答案的函数,并在 20 秒后删除消息, 在给出响应后.代码:

class MyBot(commands.Bot):
    
    def __init__(self, command_prefix, self_bot):
        commands.Bot.__init__(self, command_prefix=command_prefix, self_bot=self_bot)
        self.add_commands()
        self.playinput = ""
        self.inputvalid = True
        self.playing = False
        self.replace = False

    
    def add_commands(self):
      @bot.command(name="play",help="Plays the first Youtube result from the input you give. Usage:   -play [search here]   Example:   -play Never Gonna Give You Up")
      async def play(cxt,*args):
        self.playinput = ""
        self.inputvalid = True
        if len(args) != 0:
          for i in args:
            self.playinput += i
        else:
          message = await cxt.send("Invalid input.")
          asyncio.sleep(5)
          await bot.delete_message(message)
          inpvalid = False
    
        if inpvalid == True:
          if self.playing == True:
            cxt.send("Video already playing. Replace? y/n")
            
            #
            def check(msg):
              return msg.author == cxt.author and msg.channel == cxt.channel and \
              msg.content.lower() in ["y", "n"]
            replacemessage = await bot.wait_for("message", check=check)




        
    
bot = MyBot(command_prefix="-", self_bot=False)
bot.run(TOKEN)

def check(msg): 我知道如何删除消息,我只需要一种在检查消息时数到 20 的方法。

请查看您正在处理的项目的完整文档和示例,

https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=wait_for#discord.ext.commands.Bot.wait_for

注意:如果您不知道自己在做什么,请不要订阅 class 机器人,因为它可能会影响 class 的属性和功能

@bot.command(name="play", help="Plays the first YouTube result from the input you give. Usage:   -play [search here]   Example:   -play Never Gonna Give You Up")
async def play(cxt, *, args):
    #args is now a string of the query that follows ".play" in the command

    def check(msg):
        return msg.author == cxt.author and msg.channel == cxt.channel and ("y" in msg.content.lower() or "n" in message.content.lower())

    if self.playing == True:
        cxt.send("Video already playing. Replace? y/n")

    replacemessage = await bot.wait_for("message", check=check, timeout=20)