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

时间:2020-06-19 23:47:23

标签: python discord discord.py 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)的下一个答复,而不仅仅是我一个人?

1 个答案:

答案 0 :(得分:1)

在检查中,您只需检查消息的作者是否与传递到命令参数中的成员相同:

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

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

这将防止漫游器从提到的用户可能不相关的其他对话中获取答复:

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

参考:

相关问题