说明
参数
awaitReactions 将filter 和options 作为参数。
filter是判断user完成的reaction是否需要收集的函数。
例如,您只想收集 ? 或 ? 而忽略其他反应,例如 ?。因此,您的过滤器可能是这样的:
const filter = (reaction, user) => ['?', '?'].includes(reaction.emoji.name);
options 是确定何时停止收集反应的选项。
有3 configurable parameters:
-
max:收集的最大反应总量
-
maxEmojis: 表情收集的最大数量
-
maxUsers:最大反应用户数
在您的情况下,您只想收集一个(有效)表情符号来确定是否应将帖子发布到公共频道。因此,您可能希望将您的 options 设置为:
const options = {
maxEmojis: 1, // stop collecting if any 1 valid emoji is collected
}
返回值
awaitReactions 返回一个 Promise,该 Promise 在收集完成时解决,并返回收集到的反应的 Collection。
对你来说,因为你只关心第一反应,所以你只需要查看集合collected.first()中的第一反应,并确定是否将消息发送到公共频道。因此,处理程序将是:
collected => {
if (collected.first().emoji.name === '?') {
// send the message to the public channel
}
}
结论
结合以上,你可以像这样使用awaitReactions:
execute(message, args) {
let cf = args.join(' ')
message.delete()
const cfAdm = message.guild.channels.cache.get('767082831205367809')
let embed = new discord.MessageEmbed()
.setTitle('**CONFISSÃO**')
.setDescription(cf)
.setColor('#000000')
const filter = (reaction, user) => ['?', '?'].includes(reaction.emoji.name);
const reactOptions = {maxEmojis: 1};
cfAdm.send(embed)
.then(function (message) {
message.react('?')
.then(() => message.react('?'))
/**** start collecting reacts ****/
.then(() => message.awaitReactions(filter, reactOptions))
/**** collection finished! ****/
.then(collected => {
if (collected.first().emoji.name === '?') {
// send the message to the public channel
}
})
});
}
或者你可以使用 async/await 来简化代码:
async execute(message, args) {
let cf = args.join(' ')
message.delete()
const cfAdm = message.guild.channels.cache.get('767082831205367809')
let embed = new discord.MessageEmbed()
.setTitle('**CONFISSÃO**')
.setDescription(cf)
.setColor('#000000')
const filter = (reaction, user) => ['?', '?'].includes(reaction.emoji.name);
const reactOptions = {maxEmojis: 1};
const messageReact = await cfAdm.send(embed);
await messageReact.react('?');
await messageReact.react('?');
/**** collect reacts ****/
const collected = await messageReact.awaitReactions(filter, reactOptions);
/**** determine whether to post message or not ****/
if (collected.first().emoji.name === '?') {
// send `message` to the public channel
}
}
希望这足够清楚,可以理解:)