Search code examples
pythondiscordbotsdm

How do I make my discord bot reading dms in Python?


I have got a discord bot and it works fine, I've just got one problem: If the bot sends a DM to someone due to some command, I want it to receive the answer to that DM. I don't get it working for DMs. I've tried some stuff I've found on the internet but nothing was even close to working. Any help would be much appreciated :) Here's what I've tried (I'm sorry I can't give you that much)

@bot.event
async def on_private_message(ctx):
    if ctx.channel.id == ctx.author.dm_channel.id:
        # here I've tried using ctx.content in several ways but there was no ctx.content...

Solution

  • on_private_message is not a discord event, we just use on_message and check if it is a dm.

    @bot.event()
    async def on_message(message):
        if message.guild is None:
           #this is a dm message
    

    However, I see that your problem is accepting an answer from a user in private message, this can be done with wait_for.

    @bot.command()
    async def something(ctx):
        await ctx.author.send('hello there')
        await ctx.author.send("you have 30 seconds to reply")
        msg = bot.wait_for('message', check = lambda x: x.author == ctx.author and x.channel == ctx.author.dm_channel, timeout=30)
        # do stuff with msg
    

    References: