![](/img/trans.png)
[英]How do i make a discord bot send a message when a specific user is mentioned?
[英]How can I make my Discord bot recognize it when it is mentioned?
例如,我在考虑我有一个命令,它给面包,它给每个用户面包,但我想这样做,如果有人标记机器人,那么它会说不同的信息,所以我为此做了不同的功能,但不幸的是我不能弄清楚如何让我的机器人识别用户已标记机器人或用户......有人可以帮忙吗?
client.on("message", function(message) {
if (message.content === `give bread to <@${client.id}>`) {
message.channel.send(`I cant give bread to my self...`)
}
return;
});
您可以检查message.mentions.users
并将has()
与机器人的 ID 一起使用,这将是client.user.id
,因为我们想要机器人的用户 object。
client.on("message", function(message) {
if (message.content.startsWith('give bread to')) {
const mentions = message.mentions.users;
if (mentions.has(client.user.id)) {
return message.channel.send(`I cant give bread to my self...`)
} else {
// Your code
}
}
return;
});
首先, client
object 没有id
属性。 我相信您指的是client
的user
object 属性,它返回一个用户 object,您可以从中获取客户端的 ID。
实现所需结果的更优化方法是检查提到的用户(在您的原始函数中)的 ID 是否等于客户端 ID,如果是,则返回不同的 output:
client.on('message', message => {
if (!message.content.startsWith(`give bread to `) return
const mentioned = message.mentions.users.first()
if (!mentioned) return
if (mentioned.id === client.user.id) return message.channel.send(`I cant give bread to my self...`)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.