如何在 discord.js 中获取消息中某个表情符号反应的计数

Posted

技术标签:

【中文标题】如何在 discord.js 中获取消息中某个表情符号反应的计数【英文标题】:How do I get the count of a certain emoji reaction on a message in discord.js 【发布时间】:2021-07-07 04:57:21 【问题描述】:

我正在创建一个投票命令,并且我正在尝试获取单个表情符号的反应计数。当我今天早些时候查看消息时,它返回了消息数据,并显示了 reactions 集合。然而,当我查看反应集合时,它返回了undefined。经过多次调试和尝试,它只返回频道中所有消息的列表。

const embedMessage = await channel.messages.fetch(row.get('messageID')); //row.get(messageID) is just looking at a database for the stored messageID, I checked and the database is storing that correctly.
const embedMessageReacts = embedMessage.reactions; //This returns 'undefined'

//I also have the original 'embed' constant cached from the original embed message sending higher in the code but it no longer shows up as anything.
//The 'channel' constant works as normal.
//Also, the collection cannot be empty as the bot itself reacts to the message I am getting. Meaning, the message already has at least 2 reactions on it (the minimum for the command to work causes there to be at least 2 reactions)
//Lastly, it is not listening for reactions (although there is a listener for when the message containing these reactions is sent), so I have to look it up manually.
//Instead, these run when a certain time has elapsed.
//Hopefully there is a solution because this issue has taken up 2 days of time.

【问题讨论】:

【参考方案1】:

反应不再被捕捉到。 您必须在使用前抓住它们。 像这样:

if(!embedMessage) return(false); 
const embedMessageReacts = embedMessage.reactions.cache;

你可以轻松数出表情符号

let Heart = embedMessage.reactions.cache.get('♥️').count;

【讨论】:

谢谢,我会在今天晚些时候看看这是否有效,并会通知您。

以上是关于如何在 discord.js 中获取消息中某个表情符号反应的计数的主要内容,如果未能解决你的问题,请参考以下文章

discord.js 如何从 fetchMessage 事件中获取“反应用户”列表?

收到表情符号反应后的 Discord.js 消息

Discord JS - 如何对同一个嵌入做出多次反应?

无法从反应中读取标准表情符号名称 [discord.js]

当用户对消息做出反应时,机器人会在 discord.js 中发送另一条消息

从反应 discord.js 中获取消息 ID