Discord Bot Messaging a User With a Specific User Id

In discord.js, Can I send DirectMessage to user With DiscordBot?

And The user is not in the same server with bot.

And

I make a mistake, I want to send Privatemessage to User who isn't in same server with bot I added.. [source]

That is not possible to do.

Bots need to have at least 1 common server with a user to be able to send a direct message.

If the user is on the same server as the bot, only then you can send a DM, using any of the other methods on this post.

client.users.get("someID").send("someMessage");

Discord.py Send DM to specific User ID

  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")

DM'ing a specific user ID

To send a Direct Message you need the User Object. You can get the cached users from client.users, but the user might not be in cache. To "load" that user and send him a message you can do something like this:

client.users.fetch('123456789').then((user) => {
user.send("My Message");
});

Dont forget to replace 123456789 with the ID that you want. In this case you want only the numbers, you don't need the <@...>

Discord JS, reply to certain user ID

A little late to reply, but I had the same question and I used msg.sender instead of msg.author

const userID = "4608164XXX93150209"

bot.on("message", function(message){
if(!message.sender === userID)
{

if(message.content === 'psst')
{
message.channel.send('Hello there!');
}
}});

Note that msg.sender is a string and not numeric



Related Topics



Leave a reply



Submit