【问题标题】:How to get input from mentioned user with discord.py rewrite?如何通过 discord.py 重写从提到的用户那里获取输入?
【发布时间】:2020-10-10 06:47:18
【问题描述】:

我正在尝试为我最近开始编程的 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)的下一个回复,而不仅仅是我?

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite


    【解决方案1】:

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

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

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

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

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

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      相关资源
      最近更新 更多