1

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...
bad_coder
  • 201
  • 1
  • 9

2 Answers2

2

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:

Ceres
  • 2,498
  • 1
  • 10
  • 28
  • Thanks for your quick answer, I had also used `on_message` but didn't know this would be a difference. However, when I use this first code segment, the bot will ONLY reply to DMs, as I'm using these `@bot.command()` things to define commands. How can I fix that? – bad_coder Mar 18 '21 at 16:37
  • The first part is to make the bot respond only to dms, I have explained in the second part, the command which can be used anywhere and will dm the user and wait for a response, not sure what your doubt is – Ceres Mar 18 '21 at 17:02
  • Ok, I think I didn't explain it correctly. So I **don't** want the bot respond **only** to DMs. It has some other commands to be used in servers. But I wanted to add a command which only I can use, and with that command, the bot should message all people on the server and ask for their birthday (for a birthday bot). (This works fine.) But now the problem is, the bot cannot read their reply. And when I copy your first code segment (it actually worked without the second one, because now I used `message.guild`), the bot will **only** reply to DMs and server commands won't work. – bad_coder Mar 18 '21 at 18:33
  • I guess there has to be some `else`-statement? – bad_coder Mar 18 '21 at 18:33
  • yep, but you said that you are using `bot.command`, the first segment doesn't use a bot_command, it simply replies to dm messages, using an else statement, you can make it respond to guild messages, also read [this](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – Ceres Mar 19 '21 at 04:12
  • Thanks you two, I finally got it working :) – bad_coder Mar 20 '21 at 13:06
1

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