Discord.js在回复表情符号时执行功能



目前,我正在使用Discord.js制作一个机器人。

client.on('message', (message) => {
if (message.content === '$wa') {
message.channel.send({ embed: exampleEmbed }).then((embedMessage) => {
embedMessage.react('❤️');
embedMessage
.awaitReactions(
(filter = (reaction, user) => {
return reaction.emoji.name === '❤️' && user.id === message.author.id;
}),
{ max: 2, time: 60000, errors: ['time'] }
)
.then((collected) => {
const reaction = collected.first();
if (reaction.emoji.name === '❤️') {
message.channel.send(
':sparkling_heart: **Hanno** and **Roronoa Zoro** are now married! :sparkling_heart:'
);
}
});
});
}
});

如果我键入$wa,机器人程序会显示一些嵌入。但问题是,它会自动在嵌入中添加一个心形。我希望如果我也点击心脏(总共有2个心脏(,它会执行底部的if语句。

我试过多种方法,但都没用。这也是我第一次使用Discord.js

您需要考虑机器人自己的反应。我建议将您的filter实现重做为类似的内容。关键是你必须将!user.bot添加到过滤器中,这样机器人自己的反应就会被忽略

const filter = (reaction, user) => {
return reaction.emoji.name === "❤️" && user.id === message.author.id && !user.bot
}
embedMessage.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })

请尝试以下操作:

client.on('message', message => {
if (message.content === '$wa') {
message.channel.send({ embed: exampleEmbed }).then(embedMessage => {
embedMessage.react('❤️');
embedMessage.awaitReactions(filter = (reaction, user) => {
return reaction.emoji.name === '❤️' && user.id === message.author.id;
},
{ max: 1, time: 60000, errors: ['time'] }).then(collected => {
const reaction = collected.first();
if (reaction.emoji.name === '❤️') {
message.channel.send(':sparkling_heart: **Hanno** and **Roronoa Zoro** are now married! :sparkling_heart:');
}
}).catch(() => {
// user didn't react with ❤️ in given time (here: 60 secs)
message.channel.send('no reaction in time');
});
});
}
});

我将max的值更改为1,还添加了一个catch块来捕获UnhandledPromiseRejectionWarning。如果以后不这样做,它可能会退出程序并出现错误。当用户没有及时对embedMessage做出反应时,您当然可以执行任何您喜欢的操作。

最新更新