Rovindu Thamuditha
Rovindu Thamuditha

Reputation: 220

How can I dm a specific User by using Discord.py?

I'm making a discord bot with discord.py and I want to a specific user when a user uses a specific command.

from discord import DMChannel

client = discord.Client()
client = commands.Bot(command_prefix=']')

@client.command(name='dmsend', pass_context=True)
async def dmsend(ctx, user_id):    
  user = await client.fetch_user("71123221123")
  await DMChannel.send(user, "Put the message here")

When I give the command ]dmsend nothing happens. And I've tried dmsend also. But nothing happened.

Upvotes: 0

Views: 396

Answers (2)

Dominik
Dominik

Reputation: 3592

A few things I noticed:

You defined client twice, that can only go wrong.

First) Remove client = discord.Client(), you don't need that anymore.

If you want to send a message to a specific user ID, you can't enclose it in quotes. Also, you should be careful with fetch, because then a request is sent to the API, and they are limited.

Second) Change await client.fetch_user("71123221123") to the following:

await client.get_user(71123221123) # No fetch

If you have the user you want the message to go to, you don't need to create another DMChannel.

Third) Make await DMChannel.send() into the following:

await user.send("YourMessageHere")

You may also need to enable the members Intent, here are some good posts on that:

A full code, after turning on Intents, could be:

intents = discord.Intents.all()

client = commands.Bot(command_prefix=']', intents=intents)

@client.command(name='dmsend', pass_context=True)
async def dmsend(ctx):
    user = await client.get_user(71123221123)
    await user.send("This is a test")

client.run("YourTokenHere")

Upvotes: 2

Aditya Tomar
Aditya Tomar

Reputation: 1639

Use await user.send("message")

Upvotes: 0

Related Questions