Discord.py等待消息。。。如何获取信息内容和信息作者?

2024-06-17 11:56:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在搜索编写一个shutdownroom命令,使特定房间中的所有用户静音。 因此,我必须获得编写消息的用户和消息内容

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

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的目的是什么吗

@client.command(pass_context=True)

Tags: 用户clientsendtrue消息内容defcheck
1条回答
网友
1楼 · 发布于 2024-06-17 11:56:30

这是一个简单的逻辑,在命令中定义check函数。此外,您当前的检查没有任何意义,它将始终返回一个与true相似的值

@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参数:bot将等待所需的事件,直到check函数返回truthy值

编辑:

在命令外部定义check func

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

我已经用另一个函数包装了check函数,以便可以传递Context参数

相关问题 更多 >