Peter G
Peter G

Reputation: 2821

How do I get a user's private message channel in Discord.py?

I need to get a user's PM channel to see the messages my bot has previously sent to that user. I already have their User object from a command message in a standard channel.

This is what I've tried so far:

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

However, it seems like a user's DM channel is not related to their user id. What should I do now?

Upvotes: 2

Views: 10773

Answers (2)

Hyperclaw79
Hyperclaw79

Reputation: 11

your user object itself becomes the destination for pm.

So, privateChannel = user

Upvotes: 0

Peter G
Peter G

Reputation: 2821

There isn't any simple way to do this currently. There might be plans for a user.dm_channel in a newer version, but for now we have to make do with looping through client.private_channels and looking for the user:

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

Upvotes: 4

Related Questions