![](/img/trans.png)
[英]Is there is a way to add role to someone who has mentioned in a message in discord.js?
[英]Is there is a way to DM someone that is mentioned in a message with discord.js?
有沒有辦法向 discord.js 消息中提到的人發送消息?
這是我的代碼。
client.on("message", message => {
if (message.author.bot) {return}
let person = message.content.mentions
person.send("My message")
})
由於某種原因它不起作用
mentions
不是content
的有效屬性。 您正在尋找Message.mentions
,它是MessageMentions
object。
client.on("message", message => {
if (message.author.bot) return false;
// Getting the first mentioned user in the message.
const person = message.mentions.users.first();
// Checking if any user was mentioned.
if (!person) return message.reply("Please mention someone.");
person.send("Hello!").then(() => {
message.reply(`Message sent to ${person.tag}`);
}).catch(error => {
message.reply(`Couldn't send the message to ${person.tag}. | ${error}`);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.