0

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.

  • are you sure `client.fetch_user("71123221123")` is doing what you think it's doing? Due to the addition of intents, you might need to update the user cache before you are allowed to do something like this. – Lucas Roy Sep 30 '21 at 02:42

2 Answers2

2

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")
Dominik
  • 3,612
  • 2
  • 11
  • 44
0

Use await user.send("message")

Aditya Tomar
  • 1,601
  • 2
  • 6
  • 25