简体   繁体   English

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

[英]How to use wait_for command in the 'on message' event discord.py rewrite

I'm trying to make a discord bot have the same functionality as an input() command, but as discord.py rewrite didn't have that command, I searched the API and found wait_for .我试图让 discord bot 具有与input()命令相同的功能,但由于 discord.py rewrite 没有该命令,我搜索了 API 并找到了wait_for But, of course, it brought a whole load of problems with it.但是,当然,它带来了很多问题。 I searched the internet for this, but most of the answers were in a @command.command and not async def on_message(message) and the others weren't really helpful.我在互联网上搜索了这个,但大多数答案都在@command.command而不是async def on_message(message)和其他人并没有真正的帮助。 the furthest I got was:我得到的最远的是:

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

    ```

First of all, you're using the same variable msg for multiple things.首先,您对多个事物使用相同的变量msg Here is a working example I can make with the information you've given.这是我可以使用您提供的信息制作的一个工作示例。

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 returns a message object. mesg 返回一个消息对象。

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM