2

I have been searching for the past couple of hours for a way to send a direct message using their discord tag (e.g mj#0001) i have looked everywhere and no solution works for me

i tried client.send_message() but it says

unresolved attribute reference "send_message" for class "Bot"

here's my code:

@client.command()
async def DM(ctx, user_to_dm): # user_to_dm is the discord tag like mj#0081
    await client.send_message(user_to_dm, "insert message here")

i've also tried to create a private channel and add the member i want but i've had no luck. semi-beginner in python , discord library has been very hard for me to use. any help would be appreciated.

  • https://stackoverflow.com/questions/54418496/discord-py-how-do-i-send-private-message-to-someone-using-the-persons-id This could help – SebNik Apr 15 '20 at 12:40
  • didn't work for me :( as i said client.send_message doesnt seem to do anything for me. """ unresolved attribute reference "send_message" for class "Bot"""" – Majd Thaher Apr 15 '20 at 12:48

2 Answers2

0

(I use discord.Client so you might have to modify this slightly)

I have this function that sends a person a message when they join.

@client.event
async def on_member_join(user):
    await user.create_dm()
    await user.dm_channel.send(f'Hi **{user.name}**, welcome to the server! Be sure to read the rules to stay out of trouble. Have a great time!')

So, before you send a direct message to the person, you have to await user.create_dm() first to open the DM channel, where user is a discord.Member object.

So something like this.

@client.command
async def DM(ctx, user):
    user_identifier = int(user[2:-1])
    message_user = client.get_user(user_identifier)
    # now we have a discord.User class under message_user
    await message_user.create_dm()
    await message_user.dm_channel.send(user, "insert message here")

Also when you decorate functions don't call the function, just leave it there without any parentheses.

Eric Jin
  • 3,836
  • 4
  • 19
  • 45
  • 1
    this is great - but what im trying to do is pass user in as a string. basically, what im trying to do is get the bot to take a "user" argument from someone, and then message that person specific information. i just want my bot to message "mj#0081" for example, not having to use the discord.Member class, i hope i made sense lol – Majd Thaher Apr 15 '20 at 15:23
  • If you put the `user:discord.Member` it should take in a mention (make sure you mention) and it should turn it into a discord.Member – Eric Jin Apr 15 '20 at 16:47
  • i think what youre doing would work but, could you help me with the difference bwteen client = discord.Client and client = commands.Bot(command_prefix="insert prefix here") – Majd Thaher Apr 15 '20 at 17:11
  • i find this really hard so it would be great if you were able to help – Majd Thaher Apr 15 '20 at 17:11
  • If you mention someone in discord with @ the text your code will read is `<@123456789>` so you can just use `mention[2:-1]` to extract the user identifier and use it to construct a `discord.Member` object – Eric Jin Apr 17 '20 at 13:40
  • 1
    Using the `client.get_info(user_identifier)` you can construct a `discord.User` class which does almost the same things as a `discord.Member` class (excluding stuff like role assignment, ban, kick, etc). You can construct the user identifier from a mention by cutting off the first two and last characters. Might raise an error if the user you are referring to is nonexistent. – Eric Jin Apr 18 '20 at 22:26
  • this looks like the solution i need. i will try to implement this into my code although it will take time. thanks for the support! – Majd Thaher Apr 19 '20 at 14:47
0

You could just do.

@client.command()
async def DM(ctx, user_to_dm: discord.Member): # user_to_dm is the discord tag like mj#0081
    await user_to_dm.send("insert message here")

That's it, the only problem this only works for Rewrite branch and also only works for commands extension.

But I can see you using the commands extension, so this will work fine for you.

Sairam
  • 375
  • 1
  • 5
  • 28