問(wèn)題描述
我正在嘗試用我的機(jī)器人制作右舷代碼,其他一切都運(yùn)行良好.但我正試圖讓機(jī)器人忽略實(shí)際消息作者的反應(yīng).
I'm trying to make a starboard code with my bot, and everything else is working good. But I'm trying to make it to where the bot ignores reactions from the author of the actual message.
這是我當(dāng)前的代碼:
client.on('messageReactionAdd', (reaction_orig, message, user) => {
if (message.author.id === reaction_orig.users.id) return
manageBoard(reaction_orig)
})
它返回以下錯(cuò)誤:
if (message.author.id === reaction_orig.users.id) return;
^
TypeError: Cannot read property 'id' of undefined
推薦答案
問(wèn)題是 messageReactionAdd
有兩個(gè)參數(shù);消息反應(yīng)為第一個(gè),應(yīng)用表情符號(hào)的用戶為第二個(gè).當(dāng)您編寫(xiě) reaction_orig, message, user
時(shí),reaction_orig
是反應(yīng)(這是正確的),但 message
是做出反應(yīng)的用戶,因?yàn)樗堑诙€(gè)參數(shù).user
變量將為 undefined
.
The problem is that messageReactionAdd
takes two parameters; the message reaction as the first one, and the user that applied the emoji as the second one. When you write reaction_orig, message, user
, reaction_orig
is the reaction (which is correct), but message
is the user who reacted as it's the second parameter. The user
variable will be undefined
.
另一個(gè)問(wèn)題是 reaction_orig.users
返回一個(gè) ReactionUserManager 沒(méi)有 id
屬性.幸運(yùn)的是,user
已經(jīng)傳遞給您的回調(diào),因此您可以使用它的 ID.
Another issue is that reaction_orig.users
returns a ReactionUserManager that doesn't have an id
property. Luckily, the user
is already passed down to your callback so you can use its ID.
另外,reaction_orig
有一個(gè) message
屬性,即此反應(yīng)所引用的原始消息,因此您可以從中獲取其作者的 ID.
Also, reaction_orig
has a message
property, the original message that this reaction refers to so you can get its authors' ID from it.
您可以將您的代碼更改為此工作:
You can change your code to this to work:
client.on('messageReactionAdd', (reaction_orig, user) => {
if (reaction_orig.message.author.id === user.id) {
// the reaction is coming from the same user who posted the message
return;
}
manageBoard(reaction_orig);
});
但是,上面的代碼僅適用于緩存消息,即在機(jī)器人連接后發(fā)布的消息.對(duì)舊消息做出反應(yīng)不會(huì)觸發(fā) messageReactionAdd
事件.如果您還想收聽(tīng)對(duì)舊消息的反應(yīng),則需要在實(shí)例化客戶端時(shí)為 MESSAGE
、CHANNEL
和 REACTION
啟用部分結(jié)構(gòu),例如這個(gè):
However, the code above only works on cached messages, ones posted after the bot is connected. Reacting on older messages won't fire the messageReactionAdd
event. If you also want to listen to reactions on old messages you need to enable partial structures for MESSAGE
, CHANNEL
and REACTION
when instantiating your client, like this:
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
您可以檢查消息是否被緩存,例如檢查它的 author
屬性是否不是 null
.如果是null
,你可以獲取消息.現(xiàn)在,您擁有消息作者和做出反應(yīng)的用戶,因此您可以比較他們的 ID:
You can check if the message is cached by e.g. checking if its author
property is not null
. If it's null
, you can fetch the message. Now, you have both the message author and the user who reacted, so you can compare their IDs:
// make sure it's an async function
client.on('messageReactionAdd', async (reaction_orig, user) => {
// fetch the message if it's not cached
const message = !reaction_orig.message.author
? await reaction_orig.message.fetch()
: reaction_orig.message;
if (message.author.id === user.id) {
// the reaction is coming from the same user who posted the message
return;
}
// the reaction is coming from a different user
manageBoard(reaction_orig);
});
這篇關(guān)于反應(yīng)事件 discord.js的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!