問題描述
我正在嘗試讓我的 discord.js 機器人在被 ping 通時發送一條消息.我不確定如何做到這一點,所以我參考了這段代碼:
I'm trying to make my discord.js bot send a message when it is pinged. I was unsure how to do this so I referred to this code:
client.on('message', message => {
if (message.content === '<@745648345216712825>') {
message.channel.send('Message Here');
}
});
但是,這不起作用.
另外,當有人提到特定用戶時,我的機器人是否可能會做出響應,例如,如果用戶在機器人響應的消息中的任何位置提及我?如果是的話,你能告訴我怎么做嗎?
Also, is it possible that my bot responds when a person mentions a specific user for example if I am mentioned by the user anywhere in a message the bot responds? If yes, can you show me how to do it?
推薦答案
Message
有一個名為 mentions
,包含消息中提到的所有頻道、成員、角色和用戶.您可以使用方法 .has(data, [options])
of MessageMentions
查看是否提到了您的機器人.
Message
has a property called mentions
, which contains all the channels, members, roles, and users mentioned in the message. You can use the method .has(data, [options])
of MessageMentions
to see if your bot was mentioned.
client.on("messageCreate", (message) => {
if (message.author.bot) return false;
if (message.content.includes("@here") || message.content.includes("@everyone") || message.type == "REPLY") return false;
if (message.mentions.has(client.user.id)) {
message.channel.send("Hello there!");
}
});
在 Discord.JS v13 中,message
事件已重命名為 messageCreate
.使用 message
仍然有效,但在您切換之前,您會收到棄用警告.
The message
event has been renamed to messageCreate
in Discord.JS v13. Using message
will still work, but you'll receive a deprecation warning until you switch over.
這篇關于Discord.js 機器人在提及時做出響應的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!