久久久久久久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í)的歡迎消息)
主站蜘蛛池模板: 亚洲精品免费观看 | 日韩网站在线 | 91美女在线观看 | 婷婷久久五月 | 亚洲综合天堂网 | 在线黄色影院 | 亚洲精品久 | 亚洲精品电影网在线观看 | 日韩视频福利 | 欧美亚洲综合久久 | 日韩欧美一级精品久久 | 超碰人人人 | 国产精品一区二区久久久久 | 成人精品鲁一区一区二区 | 91欧美| 91久久久久久久久 | 午夜影院 | 国产精品视频网站 | 九九综合 | 大象视频一区二区 | 成人av在线网站 | 特级生活片 | 精品美女久久久 | 亚洲精品中文字幕av | 中文字幕在线观看 | 青青草亚洲| 亚洲成人黄色 | 91九色视频 | 在线国产精品一区 | 91精品国产91久久综合桃花 | 免费观看av | 欧美精品在线视频 | 久久精品国产久精国产 | 黄片毛片在线观看 | 91高清视频在线观看 | 国产高清一区二区三区 | 亚洲美女一区 | 久草免费电影 | 国产a级毛毛片 | 久久精品国产v日韩v亚洲 | 亚洲精品福利在线 |