【问题标题】:Discord.py wait_for message... How to take the message content and the person who wrote the message?discord.py wait_for message... 如何取消息内容和写消息的人?
【发布时间】:2021-06-27 08:54:29
【问题描述】:

我正在寻找编写一个shutdown-room 命令,将特定房间中的所有用户静音。 所以,我必须得到写消息的用户和消息内容。

现在我有这个,但在这种情况下,每条消息都会被监控:

def shut_check(msg):
   return msg.content

@client.command()
@commands.has_permissions(manage_messages=True)
async def shut_room(ctx):

   await ctx.send("Please send me the id of the room that you want to shut down.")

   content = await client.wait_for("message", check=check)

现在,我有发送消息的内容,但我如何验证消息的作者是否为 ctx.author?强>

我有另一个请求,你能向我解释一下pass_context=True in 的目的是什么:

@client.command(pass_context=True)

【问题讨论】:

    标签: python async-await discord discord.py bots


    【解决方案1】:

    逻辑很简单,在命令里面定义check函数。此外,您当前的检查根本没有任何意义,它总是会返回一个类似真实的值

    @client.command()
    async def shut_room(ctx):
        def check(msg):
            return msg.author == ctx.author
    
        await ctx.send("Please send me the id of the room that you want to shut down.")
    
        message = await client.wait_for("message", check=check)
        content = message.content
    

    此外,在等待消息时,它不会返回内容本身,而是返回 discord.Message 实例,我认为您对 check 函数感到困惑。

    解释check 参数:机器人将等待所需的事件,直到check 函数返回一个真实值。

    编辑:

    在命令外定义检查函数

    def check(ctx):
        def inner(msg): # This is the actual check function, the one that holds the logic
            return msg.author == ctx.author
        return inner
    
    # inside the command
    message = await client.wait_for("message", check=check(ctx)) # Note that I'm calling it
    

    我已经用另一个函数包装了检查函数,所以我可以传递 Context 参数。

    【讨论】:

    • 是的,我对检查功能感到困惑,无论如何谢谢兄弟。
    • 如果我不想将函数定义放入命令中怎么办?我的意思是,是否可以将 ctx.author 从命令中删除?
    • 不完全是,但是是的——可以在命令之外定义检查函数
    • 我该如何做到这一点,简而言之?这不是必需的,但会很舒服。
    • 我刚才编辑了答案,看看吧
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2018-09-19
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多