如何在'on message'事件discord.py rewrite中使用wait_for命令

时间:2020-04-02 17:19:56

标签: python discord.py-rewrite

我正在尝试使不和谐的bot具有与 input()命令,但是由于discord.py rewrite没有该命令,因此我搜索了API并发现了wait_for。但是,当然,它带来了很多问题。我在互联网上搜索了此内容,但是大多数答案都在@command.command中,而不是async def on_message(message)中,其他答案并没有真正的帮助。我最远的是:

def check(m):
    if m.author.name == message.author.name and m.channel.name == message.channel.name:
        return True
    else:
        return False
msg = "404 file not found"
try:
msg = await client.wait_for('message', check=check, timeout=60)
await message.channel.send(msg)
except TimeoutError:
    await message.channel.send("timed out. try again.")
    pass
except Exception as e:
    print(e)
    pass

    ```

1 个答案:

答案 0 :(得分:1)

首先,您将同一变量msg用于多种用途。这是一个我可以使用您提供的信息进行工作的示例。

msg = "404 file not found"
await message.channel.send(msg)

def check(m):
    return m.author == message.author and m.channel == message.channel

try:
    mesg = await client.wait_for("message", check=check, timeout=60)
except TimeoutError: # The only error this can raise is an asyncio.TimeoutError
    return await message.channel.send("Timed out, try again.")
await message.channel.send(mesg.content) # mesg.content is the response, do whatever you want with this

mesg返回一个message对象。

希望这会有所帮助!