pythondiscord

How do I send a DM (direct message) using discord.py to anyone I want through a command?


Someone asked me to make a bot for him that sends a DM to anyone he specifies through a command, like *send_dm @Jess#6461 hello.

I've searched a lot and I came across this code:

async def send_dm(ctx, member:discord.Member, *, content):
    await client.send_message(member, content)

But then I got the error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Bot' object has no attribute 'send_message'

I want to type, for example, *send_dm @Jess#6461 hello and the bot sends a DM saying "hello" to that user.


Solution

  • client.send_message() has been replaced by channel.send() in the version 1 of discord.py

    You can use Member.create_dm() to create a channel for sending messages to the user

    async def send_dm(ctx, member: discord.Member, *, content):
        channel = await member.create_dm()
        await channel.send(content)