Reputation: 665
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.
Upvotes: 40
Views: 246627
Reputation: 1182
To send a message to a user you first need to obtain a User
instance.
message.author
property of a message the user sent .client.users.fetch
with the user's idOnce you got a user instance you can send the message with .send
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');
});
Upvotes: 11
Reputation: 84
Just an FYI for v12 it's now
client.users.fetch('487904509670337509', false).then((user) => {
user.send('heloo');
});
where '487904509670337509'
is an id number.
Upvotes: 4
Reputation: 85
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;
Upvotes: 3
Reputation: 1
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}`
);
}
Upvotes: 0
Reputation: 141
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!
Upvotes: 6
Reputation: 1
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!
Upvotes: 0
Reputation: 4046
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.
Upvotes: 56
Reputation: 1
Make the code say if (msg.content === ('trigger') msg.author.send('text')}
Upvotes: -6