【发布时间】:2021-11-15 06:47:25
【问题描述】:
我正在尝试在机器人对消息做出反应的地方创建一个嵌入,并且我已经成功地做到了。但是,我现在正在尝试收集该消息的两个表情符号的总反应。我在网上看到了多种方法,但似乎没有一种方法能正常工作。有些抛出错误,有些则不给出任何响应,就好像代码一开始就不存在一样。
这是我的代码:
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,一分钟后它没有在.catch语句中输出collected.size。我直接从 discord.js 文档中提取的这段代码链接到 await reactions 部分中的 here。
尝试 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。知道为什么吗?
【问题讨论】:
标签: javascript discord.js