Python 如何获取用户';s Discord.py中的私有消息通道?

Python 如何获取用户';s Discord.py中的私有消息通道?,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我需要获得一个用户的PM频道来查看我的机器人以前发送给该用户的消息。我已经从标准通道中的命令消息中获得了它们的用户对象 这就是我迄今为止所尝试的: @client.event async def in_msg(msg): user = msg.author privateChannel = client.get_channel(user.id) # not working if privateChannel is not None: await doSom

我需要获得一个用户的PM频道来查看我的机器人以前发送给该用户的消息。我已经从标准通道中的命令消息中获得了它们的用户对象

这就是我迄今为止所尝试的:

@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无关。我现在该怎么办?

目前没有任何简单的方法可以做到这一点。在较新的版本中,可能会有一个
用户.dm_频道
的计划,但目前我们必须通过
客户端.private_频道
进行循环,并寻找用户:

@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)

您的用户对象本身将成为pm的目标


因此,
privateChannel=user

您无法从用户对象获取频道历史记录