问题
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.
回答1:
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.
回答2:
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").
.send tends to work better for me in general than .sendMessage, which sometimes runs into problems. Hope that helps a teeny bit!
回答3:
Make the code say if (msg.content === ('trigger') msg.author.send('text')}
回答4:
target.send('string') Should work
回答5:
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!
回答6:
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}`)
}
回答7:
To send a message to a user you first need a User instance representing the user you want to send the message to.
Obtaining a User instance
- You can obtain a
Userinstance from a message the user sent by doingmessage.autor - You can obtain a
Userinstance from a user id withclient.fetchUser
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.fetchUser("487904509670337509",false).then(user => {
user.send("heloo",)
})
来源:https://stackoverflow.com/questions/41745070/sending-private-messages-to-user