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

Viewed 649

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...
2 Answers

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:

Ok, so as per your question it says that, you want to receive the answer of dms, in order to receive answer, you might have asked a question, so I will explain using an example, so suppose you run a command !question, the bot will dm you or whoever runs the command, a question whose answer needs to be checked. For that I would recommend the use of bot.wait_for():-

bot.command()
async def question(ctx):
    channel = await ctx.author.create_dm()
    def check(m):
        return m.author == ctx.author and m.channel == channel
    try:
        await ctx.author.send("") #your question inside ""
        msg = await bot.wait_for('message', timeout=100.0, check=check)
        message_content = msg.content
        print(message_content) #you can do anything with the content of the message
    except:
        await ctx.author.send("You did not answer in given time")

        
Related