如何在Discord.py中获取用户的私人消息频道?

时间:2017-04-23 20:28:19

标签: python python-3.x discord discord.py

我需要获取用户的PM频道才能看到我的机器人之前发送给该用户的消息。我已经从标准通道中的命令消息中获取了User对象。

这是我到目前为止所尝试的:

@client.event
async def in_msg(msg):
    user = msg.author
    privateChannel = client.get_channel(user.id) # not working
    if privateChannel is not None:
        await doSomethingWithChannel(privateChannel, user)
    else:
        privateChannel = await client.start_private_message(user)
        await firstMessageToUser(privateChannel, user)

但是,用户的DM频道似乎与其用户ID无关。我现在该怎么办?

2 个答案:

答案 0 :(得分:1)

目前没有任何简单的方法可以做到这一点。在新版本中可能有user.dm_channel的计划,但现在我们必须通过client.private_channels循环并寻找用户:

@client.event
async def in_msg(msg):
    user = msg.author
    for ch in client.private_channels:
        if user in recipients and len(recipients) == 2:
            await doSomethingWithChannel(ch, user)
            return
    # user doesn't have a PM channel yet if we got here
    ch = await client.start_private_message(user)
    await firstMessageToUser(ch, user)

答案 1 :(得分:-1)

您的用户对象本身成为pm的目标。 所以,privateChannel = user