久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

反應(yīng)事件 discord.js

Reaction event discord.js(反應(yīng)事件 discord.js)
本文介紹了反應(yīng)事件 discord.js的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(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í)為 MESSAGECHANNELREACTION 啟用部分結(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來(lái)自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時(shí)的歡迎消息)
主站蜘蛛池模板: 色一阁| 欧美精品在线一区二区三区 | 久久午夜国产精品www忘忧草 | 一区日韩 | 午夜爽爽爽男女免费观看 | 人人干视频在线 | 国产成人精品亚洲日本在线观看 | 久久一区精品 | 一区二区三区免费 | 亚洲欧美v | 色婷婷一区| 嫩呦国产一区二区三区av | 国产欧美一区二区三区另类精品 | 精品一级毛片 | 久久6视频 | 亚洲 精品 综合 精品 自拍 | 成人福利在线 | 亚洲精品视频一区 | 日韩av在线播 | 人人99 | 国产高潮好爽受不了了夜夜做 | 毛片免费在线观看 | 精品一区二区三区免费毛片 | 噜久寡妇噜噜久久寡妇 | 成人免费观看视频 | 九九久久精品 | 免费在线观看91 | 午夜免费视频 | 国产av毛片 | 久久久久久天堂 | 久久蜜桃精品 | 国产日韩欧美 | 99久久精品免费看国产免费软件 | 日韩视频观看 | 日韩国产在线观看 | 国产精品欧美大片 | 99热碰 | 99综合网 | 国产美女福利在线观看 | 欧美综合国产精品久久丁香 | 欧美另类日韩 |