Discord.py DM 命令(发送和接收 dm)

Discord.py DM command (To send and recieve a dm)

我是 python 的新手,好吧,我想制作一个发送 dm 的 discord 机器人,当其他用户收到 dm 时,他们会回复,代码的所有者或者服务器的管理员将能够看到它说的内容!

这是我的代码,因此 far 用于 dm:

@client.command()
async def dm(ctx, user: discord.User, *, message):
    await user.send(message)

如何才能看到用户对机器人的回复?

为此,您可以使用 client.wait_for()check(),因此在 await user.send(message) 之后添加:

def check(msg):
        return msg.author == user and isinstance(msg.channel, discord.DMChannel)#check for what the answer must be, here the author has to be the same and it has to be in a DM Channel
    try:
        answer = await client.wait_for("message", check=check, timeout=600.0)#timeout in seconds, optional
        await ctx.send(answer.content)
    except asyncio.TimeoutError:
       await author.send("Your answer time is up.")#do stuff here when the time runs out

全部,如果这是 你的 dm 函数中。

参考文献:

wait_for(event, *, check=None, timeout=None)