繁体   English   中英

如何通过 discord.py 重写从提到的用户那里获取输入?

[英]How to get input from mentioned user with discord.py rewrite?

我正在尝试为我最近开始编程的 discord.py 机器人添加结婚命令。 我希望提到的用户能够回复机器人。

截至目前的代码,它只回复我。

@client.command()
async def marry(ctx, member: discord.Member):

    await ctx.send(f"{ctx.author.mention} **proposes to** {member.mention} **Do you accept??** "
                   f"\nRespond with [y(es)/n(o)]")

    def check(m):
        return m.author == ctx.author

    try:
        msg = await client.wait_for('message', check=check, timeout=10)

        if msg.content.lower() in ['y', 'yes']:
            await ctx.send(f"Congratulations! {ctx.author.mention} and {member.mention} are now married to each other!")
        elif  msg.content.lower() in ['n', 'no']:
            await ctx.send(f"Unlucky, maybe another time! {ctx.author.mention}")
        else:
            await ctx.send("I did not understand that, aborting!")
    except asyncio.TimeoutError as e:
        print(e)
        await ctx.send("Looks like you waited too long.")

有谁知道如何使机器人能够识别来自上述用户(成员:discord.Member)的下一个回复,而不仅仅是我?

在您的检查中,您可以简单地检查消息的作者是否与传递到命令的 arguments 的成员相同:

def check(m):
    return m.author == member

我还建议为执行命令的同一通道添加另一个检查。

这将防止机器人从提到的用户正在进行的其他对话中获取可能不相关的回复:

def check(m):
    return ... and m.channel == ctx.channel

参考:

暂无
暂无

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

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