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 thr
Make the code say if (msg.content === ('trigger') msg.author.send('text')}
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.
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;
A simple way would be to do something like:
const channel = client.channels.cache.get("CHANNEL_ID");
channel.send("Hello");
const channel = client.channels.cache.get((channel) => channel.name == "CHANNEL_NAME");
channel.send("Hello");
const channel = client.channels.cache.get((channel) => channel.id == "CHANNEL_ID");
channel.send("Hello");
To send a message to a user you first need a User
instance representing the user you want to send the message to.
User
instance from a message the user sent by
doing message.autor
User
instance from a user id with client.fetchUser
Once 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.fetchUser('487904509670337509', false).then((user) => {
user.send('heloo');
});
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!