Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending private messages to user

I'm using the discord.js library and node.js to create a Discord bot that facilitates poker. It is functional except the hands are shown to everyone, and I need to loop through the players and send them a DM with their hand.

bot.on("message", message => {   message.channel.sendMessage("string"); }); 

This is the code that sends a message to the channel when any user sends a message. I need the bot to reply in a private channel; I've seen dmChannel, but I do not understand how to use it. I have the username of the member that I want to send a message to. An example would be appreciated.

Edit: After looking around for a user object, I found that I can get all of the users using the .users property of the client (bot). I will try using the user.sendMessage("string") method soon.

like image 608
adapap Avatar asked Jan 19 '17 15:01

adapap


People also ask

How do you DM someone on Discordjs?

If you want to DM the user who sent the interaction, you can use interaction. user. send() .

How do you check private messages on discord?

To access the DM window, Click the Home icon, which is your portrait on the upper left. This will show you a list of your friends, and the direct messages that you currently have. Clicking on the portrait of a friend will open the Direct Message window.


1 Answers

In order for a bot to send a message, you need <client>.send() , the client is where the bot will send a message to(A channel, everywhere in the server, or a PM). Since you want the bot to PM a certain user, you can use message.author as your client. (you can replace author as mentioned user in a message or something, etc)

Hence, the answer is: message.author.send("Your message here.")

I recommend looking up the Discord.js documentation about a certain object's properties whenever you get stuck, you might find a particular function that may serve as your solution.

like image 171
Kaynnc Avatar answered Sep 19 '22 00:09

Kaynnc