python - 如果用户在 channel 中发送命令,则等待 DM 中的消息问题

标签 python discord.py

我尝试了一些代码,但对于等待消息不起作用,所以我想知道如果用户使用discord.py在 channel 中发送命令,如何在DM中等待消息。

已更新

这是根据接受的答案作为示例的最后一个代码!

async def command_name(ctx):
        await ctx.send("Send a message in DM.")

        def check(m):
            return m.author == ctx.author and m.guild is None

        msg = await client.wait_for('message', check=check)
        # Do something after they sent a DM``` 

最佳答案

使用内置wait_for为了这。您可以编写自定义检查来查看它是否是 DM channel 。该检查是一个函数,如果您的函数应该停止等待,则返回 True;如果不应该停止,则返回 False。您只需稍微修改文档中的示例即可获得所需的结果:

@client.command()
async def command_name(ctx):
        await ctx.send("Send a message in DM.")

        def check(m):
            return m.author == ctx.author and m.guild is None

        msg = await client.wait_for('message', check=check)
        # Do something after they sent a DM

关于python - 如果用户在 channel 中发送命令,则等待 DM 中的消息问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64593691/

相关文章:

python - 循环范围结束后自动销毁变量

python - Discord.py-rewrite wait_for() 如何使用?

python - 如何给一个命令多个名称?

python - 从通过 ctypes 获取的 C-ptr 创建 numpy 数组

python - 如何按升序对条形图中的条形进行排序

python - 如何使用 python 中的枚举将值放入列表中?

python - 如何在消息下方创建一个可点击的按钮?

python - Discord.py - 使用特定消息回复 DM

python - defer() 方法返回未知交互错误

python - U8、U16、U32 在 python 中的表示