'Discord.py how do I send a DM 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 alot 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 1:[1]

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)

Solution 2:[2]

HI there hope this help you

client.command(aliases=['dm'])
async def DM(ctx, user : discord.User, *, msg):
    try:
        await user.send(msg)
        await ctx.send(f':white_check_mark: Your Message has been sent')
    except:
        await ctx.send(':x: Member had their dm close, message not sent')

change you client if you use any other str

Solution 3:[3]

The easiest way to do is:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Aarsh Raghuvanshi
Solution 3 EpicGamer123