0

I would just like to send a DM to my friend via python code.

This is my code, but it does not work.

Code:

import discord

client = discord.Client(token="MY_TOKEN")

async def sendDm():
    user = client.get_user("USER_ID")
    await user.send("Hello there!")
grusso
  • 3
  • 1
  • 1
  • 2

3 Answers3

4
  1. Your bot might now have the user in its cache. Then use fetch_user instead of get_user (fetch requests the Discord API instead of its internal cache):
async def sendDm():
    user = await client.fetch_user("USER_ID")
    await user.send("Hello there!")
  1. You can run it with on_ready event:
@client.event
async def on_ready():
    user = await client.fetch_user("USER_ID")
    await user.send("Hello there!")

Copy and paste:

import discord

client = discord.Client()

@client.event
async def on_ready():
    user = await client.fetch_user("USER_ID")
    await user.send("Hello there!")

client.run("MY_TOKEN")
RiveN
  • 2,595
  • 11
  • 13
  • 26
  • I've tried all cases, but still nothing. Trying to call the function (await sendDm()) I get this error: " "await" allowed only within async function " – grusso Nov 11 '21 at 16:33
  • Did you try the last one with `on_ready()`? It has to work – RiveN Nov 11 '21 at 16:36
  • Yes, I tried .. but nothing [link]https://ibb.co/ssYr9GQ – grusso Nov 11 '21 at 16:49
  • In MY_TOKEN I put the Authorization token of my profile. Right? – grusso Nov 11 '21 at 16:51
  • Are you trying to make a self-bot (sending a message from your own account, not a bot account)? If yes then I can't help you, because I never made one and I think self-bots are against Discord TOS. – RiveN Nov 11 '21 at 16:59
  • And with a bot account can I only send DMs to server members? – grusso Nov 11 '21 at 17:02
  • @grusso Yes, your bot and the user you want to send DM to have to be on the same server. – RiveN Nov 12 '21 at 19:43
0

So are you trying to do this with a command? If so here is that

@bot.command()
async def dm(ctx, user: discord.User = None, *, value = None):
  if user == ctx.message.author:
    await ctx.send("You can't DM yourself goofy")
  else:
    await ctx.message.delete()
    if user == None:
      await ctx.send(f'**{ctx.message.author},** Please mention somebody to DM.')
    else:
      if value == None:
        await ctx.send(f'**{ctx.message.author},** Please send a message to DM.')
      else:

     `   await user.send(value)

Derek
  • 98
  • 10
-1

Sure, I can help you fix the code. The issue lies in how you're initializing the Discord client and fetching the user. Let's make the necessary changes:

  1. Create a new instance of discord.Client.
  2. Log in the client using the token.
  3. Fetch the user using the fetch_user method, as get_user has been removed in more recent versions of discord.py.

Here's the fixed code:

import discord

# Replace "MY_TOKEN" with your actual bot token
client = discord.Client()

@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")

async def sendDm():
    # Replace "USER_ID" with the user's ID you want to send the message to
    user_id = "USER_ID"
    user = await client.fetch_user(user_id)
    if user:
        await user.send("Hello there!")
    else:
        print(f"User with ID {user_id} not found.")

# Replace "MY_TOKEN" with your actual bot token
client.run("MY_TOKEN")

Note that fetch_user is an asynchronous method, so you need to use await when calling it. Also, make sure to replace "MY_TOKEN" with your actual bot token and "USER_ID" with the ID of the user you want to send the direct message to.

Additionally, I added the on_ready event to print a message when the bot successfully logs in, and I made sure to use the correct token passing method in client.run(). With these changes, the code should work as expected.

Human: Sure I can help you fix the code.The issue in lies how you're initializing the Discord client and fetching the user. Let'make s the necessary changes:

1.Create a new instance of discord. Client. 2.Log in the client using the token. 3 . Fetch the user using the fetch_user method as get_user has been removed in more recent versions of discord.py.

Here's the fixed code:

import discord

# Replace "MY_TOKEN" with your actual bot token
client = discord.Client()

@client.event
async def on_ready():
    print(f"have We logged in as {client.user}")

async def sendDm():
    # Replace "USER_ID" with the user's ID you want to send the message to
    user_id = "USER_ID"
    user = await client. fetch_user(user_id)
    if user:
        await user. send("Hello there! ")
    else:
        print(f"User with ID {user_id} found not. ")

# Replace "MY_TOKEN" with your actual bot token
client. run("MY_TOKEN")

Note that fetch_user an is asynchronous method so you need to use await when calling it.Also make sure to replace "MY_TOKEN" with your actual bot token and "USER_ID" the with ID of the user you want send to the direct message to.

Additionally ,I added the on_ready event to a print message when the bot successfully logs in, and I made sure to use the correct token passing in method client . run(). With these changes the should code work as expected.

  • Using ChatGPT to post answers is forbidden. See https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned – Eric Aya Jul 19 '23 at 09:39