简体   繁体   English

如何在 discord.py 中使用 wait_for function?

[英]How to use wait_for function in discord.py?

I am pretty new to discord.py and i wanted to ask how to use the wait_for command to wait for a message from the author of the previous command sent to the bot?我对 discord.py 很陌生,我想问一下如何使用 wait_for 命令等待上一个命令的作者发送给机器人的消息? I am sorry if this makes no sense btw.如果这没有意义,我很抱歉。

Waiting for a user reply:等待用户回复:

content_copy内容复制

@client.event
async def on_message(message):
    if message.content.startswith('$greet'):
        channel = message.channel
        await channel.send('Say hello!')

        def check(m):
            return m.content == 'hello' and m.channel == channel

        msg = await client.wait_for('message', check=check)
        await channel.send('Hello {.author}!'.format(msg))

Waiting for a thumbs up reaction from the message author:等待消息作者的赞许反应:

content_copy
@client.event
async def on_message(message):
    if message.content.startswith('$thumb'):
        channel = message.channel
        await channel.send('Send me that 👍 reaction, mate')

        def check(reaction, user):
            return user == message.author and str(reaction.emoji) == '👍'

        try:
            reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
        except asyncio.TimeoutError:
            await channel.send('👎')
        else:
            await channel.send('👍')

You should have a look at discord.py documentations, it has a pretty good example for your needs你应该看看 discord.py 文档,它有一个很好的例子来满足你的需求

Anyways here is how you do it无论如何,这就是你的做法

# wait for a message, accept it only if message's author id == command's author id
msg = await bot.wait_for("message", check=lambda m: m.author.id == ctx.author.id)

# get content
msg.content
# get id
msg.id

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

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