26
votes

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.

8
I do not work with discord.js yet. Can you create an User-Object? If so, you can use UserObject.sendMessage("string");R. Pülsinger
The bot is a client, here is the doc page: discord.js.org/#/docs/main/stable/class/Client. Your code won't work because sendMessage isn't a method of the client. I am going to see if I can create a user object.adapap
Have you tried message.author.sendMessage("XXX")? I use this in the version 10.0.1 of the lib.JulCh
@JulCh I would choose you as the answer. Thanks a ton, I got it working! One more thing if you have the time, how do I check if the (user, not bot) message was sent in a direct message?adapap
@adapap You can check if the message.channel object is an instance of DMChannel (Direct message channel), or a TextChannel for a classic channel :)JulCh

8 Answers

44
votes

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.

4
votes

The above answers work fine too, but I've found you can usually just use message.author.send("blah blah") instead of message.author.sendMessage("blah blah").

-EDIT- : This is because the sendMessage command is outdated as of v12 in Discord Js

.send tends to work better for me in general than .sendMessage, which sometimes runs into problems. Hope that helps a teeny bit!

4
votes

To send a message to a user you first need to obtain a User instance.

Obtaining a User instance

  • use the message.author property of a message the user sent .
  • call client.users.fetch with the user's id

Once you got a user instance you can send the message with .send

Examples

client.on('message', (msg) => {
 if (!msg.author.bot) msg.author.send('ok ' + msg.author.id);
});
client.users.fetch('487904509670337509', false).then((user) => {
 user.send('hello world');
});
3
votes

Just an FYI for v12 it's now

client.users.fetch('487904509670337509', false).then((user) => {
 user.send('heloo');
});

where '487904509670337509' is an id number.

2
votes

If your looking to type up the message and then your bot will send it to the user, here is the code. It also has a role restriction on it :)

case 'dm':
  mentiondm = message.mentions.users.first();
  message.channel.bulkDelete(1);
  if (!message.member.roles.cache.some(role => role.name === "Owner")) return message.channel.send('Beep Boing: This command is way too powerful for you to use!');
  if (mentiondm == null) return message.reply('Beep Boing: No user to send message to!');
  mentionMessage = message.content.slice(3);
  mentiondm.send(mentionMessage);
  console.log('Message Sent!')
  break;
0
votes

If you want to send the message to a predetermined person, such as yourself, you can set it so that the channel it would be messaging to would be their (your) own userID. So for instance, if you're using the discord bot tutorials from Digital Trends, where it says "to: ", you would continue with their (or your) userID. For instance, with how that specific code is set up, you could do "to: userID", and it would message that person. Or, if you want the bot to message you any time someone uses a specific command, you could do "to: '12345678890'", the numbers being a filler for the actual userID. Hope this helps!

0
votes

This is pretty simple here is an example

Add your command code here like:

if (cmd === `!dm`) {
 let dUser =
  message.guild.member(message.mentions.users.first()) ||
  message.guild.members.get(args[0]);
 if (!dUser) return message.channel.send("Can't find user!");
 if (!message.member.hasPermission('ADMINISTRATOR'))
  return message.reply("You can't you that command!");
 let dMessage = args.join(' ').slice(22);
 if (dMessage.length < 1) return message.reply('You must supply a message!');

 dUser.send(`${dUser} A moderator from WP Coding Club sent you: ${dMessage}`);

 message.author.send(
  `${message.author} You have sent your message to ${dUser}`
 );
}
-4
votes

Make the code say if (msg.content === ('trigger') msg.author.send('text')}