除了awaitReactions,您还可以使用createReactionCollector,这可能更易于使用,并且它的collector.on() 侦听器比awaitReactions 的then() 和catch() 方法更具可读性。
您无需使用message.reactions.cache.get('?').count 来检查反应数量,因为当您达到最大值时会触发end 事件,您可以在其中发送消息。
此外,在您的filter 中,您无需检查是否为user.id === message.author.id,因为您希望接受其他用户的反应。但是,您可以检查是否!user.bot 以确保您不会计算机器人的反应。如果您不想限制机器人收集反应的时间,也可以删除 time 选项。
另一个错误是您在message 本身而不是sentMessage 上调用了.awaitReactions()。
查看下面的工作代码:
// v12
client.on('message', async (message) => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const MAX_REACTIONS = 2;
if (command === 'react') {
try {
// send a message and wait for it to be sent
const sentMessage = await message.channel.send('React to this!');
// react to the sent message
await sentMessage.react('?');
// set up a filter to only collect reactions with the ? emoji
// and don't count the bot's reaction
const filter = (reaction, user) => reaction.emoji.name === '?' && !user.bot;
// set up the collecrtor with the MAX_REACTIONS
const collector = sentMessage.createReactionCollector(filter, {
max: MAX_REACTIONS,
});
collector.on('collect', (reaction) => {
// in case you want to do something when someone reacts with ?
console.log(`Collected a new ${reaction.emoji.name} reaction`);
});
// fires when the time limit or the max is reached
collector.on('end', (collected, reason) => {
// reactions are no longer collected
// if the ? emoji is clicked the MAX_REACTIONS times
if (reason === 'limit')
return message.channel.send(`We've just reached the maximum of ${MAX_REACTIONS} reactions.`);
});
} catch (error) {
// "handle" errors
console.log(error);
}
}
});
如果您使用的是 discord.js v13,有几个变化:
- 您需要添加
GUILD_MESSAGE_REACTIONS 意图
-
message 事件现在是 messageCreate
- 收集器的
filter 在options 对象内
// v13
const { Client, Intents } = require('discord.js');
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
],
});
const prefix = '!';
client.on('messageCreate', async (message) => {
if (message.author.bot || !message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
const MAX_REACTIONS = 2;
if (command === 'react') {
try {
// send a message and wait for it to be sent
const sentMessage = await message.channel.send('React to this!');
// react to the sent message
await sentMessage.react('?');
// set up a filter to only collect reactions with the ? emoji
// and don't count the bot's reaction
const filter = (reaction, user) => reaction.emoji.name === '?' && !user.bot;
// set up the collecrtor with the MAX_REACTIONS
const collector = sentMessage.createReactionCollector({
filter,
max: MAX_REACTIONS,
});
collector.on('collect', (reaction) => {
// in case you want to do something when someone reacts with ?
console.log(`Collected a new ${reaction.emoji.name} reaction`);
});
// fires when the time limit or the max is reached
collector.on('end', (collected, reason) => {
// reactions are no longer collected
// if the ? emoji is clicked the MAX_REACTIONS times
if (reason === 'limit')
return message.channel.send(`We've just reached the maximum of ${MAX_REACTIONS} reactions.`);
});
} catch (error) {
// "handle" errors
console.log(error);
}
}
});
结果: