javascript - 如何收集嵌入消息的总反应?| 不和谐.js v13
问题描述
我正在尝试在机器人对消息做出反应的地方创建一个嵌入,并且我已经成功地做到了。但是,我现在正在尝试收集该消息的两个表情符号的总反应。我在网上看到了多种方法,但似乎没有一种方法能正常工作。有些抛出错误,有些则不给出任何响应,就好像代码一开始就不存在一样。
这是我的代码:
const discord = require('discord.js');
const db = require('quick.db');
module.exports = {
name: 'command',
run: async (message, args) => {
const suggestionEmbed = new discord.MessageEmbed()
.setColor('#000000')
.setTitle('[!] Hello')
.setTimestamp()
message.channel.send({ embeds: [suggestionEmbed] }).then(sEmbed => {
sEmbed.react('⬆️');
sEmbed.react('⬇️');
});
}
}
注意:我认为module.exports
可能是问题所在,所以我将所有尝试的整个代码都移到了主index.js
文件中,但仍然没有运气。没有输出,没有错误。
这是我的尝试:
尝试1:
message.channel.send({ embeds: [suggestionEmbed] })
.then(sEmbed => {
sEmbed.react("⬆️");
sEmbed.react("⬇️");
});
const filter = reaction => reaction.emoji.name === '⬆️';
message.awaitReactions(filter, { time: 10000 })
.then(collected => collected.map(s => message.reply(`we have ${s.count}`)));
尝试2:
message.channel.send({ embeds: [suggestionEmbed] })
.then(sEmbed => {
sEmbed.react("⬆️");
sEmbed.react("⬇️");
});
console.log(suggestionEmbed.reactions.cache.get("⬆️").count);
尝试 2 的输出:
console.log(suggestionEmbed.reactions.cache.get("⬆️").count);
TypeError: Cannot read property 'cache' of undefined
如何收集已发送的特定消息 ID 的总数?所以我可以检查它是否有 5 个赞成票、10 个反对票等......
编辑:
尝试 3 也没有输出或错误:
const emojis = ["⬆️", "⬇️"]; //the emojis to react
message.channel.send({ embeds: [suggestionEmbed] })
.then(async function (message) {
for (let emoji of emojis) { await message.react(emoji) }
const filter = (reaction, user) => {
return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '⬆️') {
message.reply('you reacted with up.');
} else {
message.reply('you reacted with down.');
}
})
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
});
尝试 4 没有输出或错误:
const time = 1000 //amount of time to collect for in milliseconds
const emojis = ["⬆️", "⬇️"]; //the emojis to react
message.channel.send({ embeds: [suggestionEmbed] })
.then(async function (message) {
for (let emoji of emojis) { await message.react(emoji) }
const filter = (reaction, user) => {
return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, { time: time });
collector.on('collect', (reaction, reactionCollector) => {
console.log(reaction.count) //or do whatever you want with the reactions
});
});
尝试 5 没有错误或输出:
const time = 1000 //amount of time to collect for in milliseconds
const emojis = ["⬆️", "⬇️"]; //the emojis to react
message.channel.send({ embeds: [suggestionEmbed] })
.then(async function (message) {
for (let emoji of emojis) { await message.react(emoji) }
const filter = (reaction, user) => {
return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};
const collector = message.createReactionCollector(filter, { time: time });
collector.on('collect', (reaction, user) => {
console.log(reaction.count) //or do whatever you want with the reactions
});
});
尝试 6 没有错误但输出奇怪:
message.channel.send({ embeds: [suggestionEmbed] }).then(sEmbed => {
sEmbed.react('⬆️');
sEmbed.react('⬇️');
//db.set(`${suggestionID}.ChannelID`, sEmbed.ID);
});
const filter = (reaction, user) => {
return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};
message.awaitReactions({ filter, max: 5, time: 60000, errors: ['time'] })
.then(collected => console.log(collected.size))
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 5 reacted.`);
});
这个似乎在0
我运行命令后立即输出,一分钟后它没有collected.size
在.catch
语句中输出。这段代码是我直接从 discord.js 文档中提取的,链接在本await reactions
节中。
尝试 7 输出错误且没有错误:
const embedSend = await message.channel.send({ embeds: [suggestionEmbed] }).then(async sEmbed => {
await sEmbed.react('⬆️');
await sEmbed.react('⬇️');
//db.set(`${suggestionID}.ChannelID`, sEmbed.ID);
const filter = (reaction, user) => {
return reaction.emoji.name === '⬆️' && user.id === message.author.id;
};
const reactions = await sEmbed.awaitReactions({ filter, max: 5, time: 10000, errors: ['time'] })
.then(collected => console.log(collected.size))
.catch(collected => {
console.log(`After a minute, only ${collected.size} out of 5 reacted.`);
});
});
这给了我一个输出After a minute, only 0 out of 5 reacted
。但是,我不明白为什么它是0?它不计算机器人的反应,当我在这 10 秒内做出反应时,总反应是,2
但输出仍然是0
,尽管它应该是 1。知道为什么吗?
解决方案
我找到了解决方案并混合了问题,我的绝大多数尝试都是正确的。唯一的问题是,使用 discord.js v13 我忘了包含Intents.FLAGS.GUILD_MESSAGE_REACTIONS
在我的意图中:) 只花了我 23 多个小时才弄清楚。
推荐阅读
- dns - 将 godady 域连接到我的 hostgator 的 dns 设置
- html - 如何使用带有侧边栏的“折叠”Bootstrap 4 类选择器
- boost-spirit-qi - Boost Spirit Qi语法添加到船长内的列表
- javascript - 无法将纹理设置为框(html5 / js / babylon.js)
- php - Laravel:单击按钮后显示数据库中的数据-但保持在同一页面上
- c# - 是否可以在服务器上“调试”应用程序?
- glpk - Julia Language - JuMP 包,使用 GLPK 的意外模型可行性结果
- java - 如何编写 [recursive?] 函数来迭代 JMenu 层次结构中的(子)MenuElements?
- java - Java递归取幂方法使递归更高效
- prolog - SOOOOO NEWB ... Prolog语法错误:操作员预期