0

I had multiple attempts.

# One of the attempts
 ch = client.get_user(USERID)
 await ch.send("IDK")

#Another
 client = discord.Client()

 @client.event
 async def on_ready():
  user = "@name"
  user = discord.Member

  await user.send(user, "here")

#Another
 client = discord.Client()

 @client.event
 async def on_ready():
  user = discord.utils.get(client.get_all_members(), id='USERID')

  if user is not None:
    await client.send(user, "A message for you")
  else:
    await client.send(user, "A message for you")

#Another
 @client.event
 async def on_ready():
    ch = client.get_all_members()
    await ch.send("Hello")

# Another
    ch = client.start_private_message(USERID)
    await ch.send("IDK")

As you can see I messed with the variables because I noticed that you can send a message by channel like this

channel = client.get_channel(CHANNELID)
await channel.send("Something)

But that doesn't work with get_user. Thanks in advance also sorry how bad my post/code is.

Seti
  • 3
  • 2

1 Answers1

0

Allow me to inform you on what is wrong with the pieces of code you have provided.

await client.send(user, "A message for you") is older syntax, and has not been used since the v1.0 migration.

client.get_all_members() shouldn't be used in this case, as you are only getting all the members the bot shares a server with rather than a single user.

client.start_private_message(USERID) does not exist as far as I have read, and therefore we can assume that this wouldn't work.

My recommendation would be to use one of the two methods I will detail.

The first method would be to use client.fetch_user(), which sends a request to the Discord API instead of its internal cache (as the second method will). The only problem you will face with this method is if you retrieve too many users in a small amount of time, which will get you ratelimited. Other than that, I recommend this method.

The second method is to get a server through client.get_guild() and getting your user through there via guild.get_member(). This method will require the user to be in its cache, however, so this shouldn't be your go-to method in my opinion.

Do view both of these methods in the code below.

@client.event
async def on_ready():
    # Method 1: Using fetch
    user = await client.fetch_user(USER_ID)
    await user.send("A message!")

    # Method 2: Using 'get'
    guild = client.get_guild(GUILD_ID)
    user = guild.get_member(USER_ID)
    await user.send("A message!")

Other Links:

Bagle
  • 2,326
  • 3
  • 12
  • 36