Discord bot:如何知道用户何时在消息中被 ping,而不是因为它是回复 [英] Discord bot : How to know when a user is pinged in the message and not because it's a reply

查看:8
本文介绍了Discord bot:如何知道用户何时在消息中被 ping,而不是因为它是回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让机器人在特定用户在消息中被 ping 时发送消息.
所以我尝试了这个:

I am trying to get the bot to send a message when a specific user is pinged in a message.
So I tried with this :

if (message.mentions.has(bot.users.cache.get(userID)))

它工作正常,如果你 ping 用户,机器人会发送一条消息.例外...

And it's working fine, if you ping the user, the bot sends a message. Excepted...

当您只是回复用户而不在消息中 ping 用户时,机器人也会做出反应,我不希望这样.
我能看到的唯一方法是:

The bot also react when you simply reply to the user without pinging the user in the message, and I don't want that.
The only way I can see is :

if (message.content.includes(userID))

推荐答案

你应该直接检查他们是否在消息中被ping.

You should check directly if they were pinged in the message.

const userRegex = new RegExp(`<@!?${userID}>`)
if (message.content.match(userRegex)) {
  //user has been mentioned and it was not because of reply
}

这篇关于Discord bot:如何知道用户何时在消息中被 ping,而不是因为它是回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆