2017-04-23 124 views
0

我需要获取用户的下午通道才能看到我的机器人以前发送给该用户的消息。我已经从标准通道的命令消息中获得了其用户对象。如何在Discord.py中获取用户的私人消息频道?

这是我到目前为止已经试过:

@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。我现在应该怎么做?

回答

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

用户对象本身成为下午的目的地。 所以,privateChannel =用户