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
- Your bot might now have the user in its cache. Then use
fetch_user
instead ofget_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!")
- 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
Python: Editing List While Iterating Over It
Reduce Multi-Index/Multi-Level Dataframe to Single Index, Single Level
Splitting a Phone Number into a List of Digits: Python
Keras Valueerror: Input 0 Is Incompatible With Layer Conv2D_1: Expected Ndim=4, Found Ndim=5
Print() Prints Only Every Second Input
Using a String Variable as a Variable Name
How to Save\Load Models in Spark\Pyspark
Python: Opencv - Selecting Region of an Image
Python Creating Dictionary from Excel Data
Spark Equivalent of If Then Else
How to Repeatedly Execute a Function Every X Seconds
How to Constantly Run Python Script in the Background on Windows
How to Find a Word That Starts With a Specific Character
Find the Index of a Value in a 2D Array
Removing Rows and Columns in Python CSV Module