Search code examples
discord.pybotsdm

Discord.py DM command (To send and recieve a dm)


I'm pretty new to python and well, I wanted to make a discord bot that sends a dm, and when the other user receives the dm, they answer back, and the owner of the code or an admin of a server will be able to see what it says!

Here is my code so far for the dm:

@client.command()
async def dm(ctx, user: discord.User, *, message):
    await user.send(message)

How do I make it so that i can see what the user replied to the bot?


Solution

  • For this, you can use client.wait_for() with check(), so add this after await user.send(message):

    def check(msg):
            return msg.author == user and isinstance(msg.channel, discord.DMChannel)#check for what the answer must be, here the author has to be the same and it has to be in a DM Channel
        try:
            answer = await client.wait_for("message", check=check, timeout=600.0)#timeout in seconds, optional
            await ctx.send(answer.content)
        except asyncio.TimeoutError:
           await author.send("Your answer time is up.")#do stuff here when the time runs out
    

    all if this is in your dm function.

    References:

    wait_for(event, *, check=None, timeout=None)