0

So today I was tried to learn about python discord.py .

At some point I tried the following thing: A person tells a bot a command ($DM 'discord user' 'content') and through all the research I had done, I could only find client.author.send("message") where client = discord.Client().

Is there any way to do something like:

user = 'example#0000'
client.user.send("message")

?

Nikunj Kakadiya
  • 2,689
  • 2
  • 20
  • 35
Karo
  • 27
  • 7

1 Answers1

0

There's a few ways of doing this

From user ID

@client.command()
async def DM(ctx, id: int, *, content):
    user = client.get_user(id)
    await user.send(content)

# Invoking
# $DM 123123123123123 something here

From user name#discriminator

@client.command()
async def DM(ctx, username, *, content):
    name, discriminator = username.split("#")
    user = discord.utils.get(ctx.guild.members, name=name, discriminator=discriminator)
    await user.send(content)

# Invoking
# $DM example#1111 something here

With user mentions, ID's, names, nicknames (best option imo)

@client.command()
async def DM(ctx, member: discord.Member, *, content):
    await member.send(content)

# Invoking
# $DM @example#1111 something here
# $DM 1231231231231 something here
# $DM example#1111 something here

Also make sure you enabled intents.members, for more info look at one of my previous answers

PS: You need to use commands.Bot in order for all of this to work, not discord.Client

Łukasz Kwieciński
  • 14,992
  • 4
  • 21
  • 39