V
V
Valery Albertovich Zhmyshenko2021-02-25 00:01:23
Python
Valery Albertovich Zhmyshenko, 2021-02-25 00:01:23

How to send a private message knowing the ID in discord.py?

I have a user ID and tag, how do I send a private message?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Nevzorov, 2021-02-25
@DIvan2000

In the context of commands, it is recommended to use converters for such purposes :

@bot.command()
async def cmd(ctx, user: discord.User, *, message: str):
     await user.send(message)

Out of command context - bot.get_user/bot.fetch_user
user = bot.get_user(308342985637609361)
# ИЛИ
user = await bot.fetch_user(308342985637609361)

await user.send("<https://bad-me.me/4Xm2Zjx>")

The difference between get_and fetch_is that get methods get objects from the bot's cache (the user may not be in the cache if there are no shared servers, or there are not enough intents), and fetch methods get objects by request from the API, which with a large number of ve requests can negatively affect the latency of requests (ratelimit)

A
Adik Izat, 2021-02-25
@JaxAdam

from another forum.

@client.command(pass_context=True)
async def dm(ctx):
    user=await client.get_user_info("User's ID here")
    await client.send_message(user, "Your message goes here")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question