我创建了一个Discord机器人,目标是通过表情符号按钮生成反应式嵌入。我的问题是,一旦按下“按钮”,所有由机器人创建的嵌入都会同时被修改。
我的机器人的伪代码下方:
const raidEmbed = new Discord.MessageEmbed() //create the embed
//some code to fill the embed
message.channel.send(raidEmbed).then(embedMessage => {
embedMessage.react('❌')
.then(()=>embedMessage.react('⭕')
//more react to create all the 'buttons'
client.on('messageReactionAdd', (reaction, user) => {
//some code to do stuff when the right 'button' is pressed
//then reset the button with this code:
if (user.id !== client.user.id) {
reaction.users.remove(user.id);
}
const newEmbed = new Discord.MessageEmbed()
//code to create the new embed
embedMessage.edit(newEmbed);
}
})
我不明白为什么我所有的嵌入内容都链接在一起以及如何解决此问题。
答案 0 :(得分:1)
您的嵌入并非全部链接在一起。这里的问题是您正在使用全局事件来检查反应。这是问题所在的代码部分:
client.on('messageReactionAdd', (reaction, user) => {
//some code to do stuff when the right 'button' is pressed
//then reset the button with this code:
if (user.id !== client.user.id) {
reaction.users.remove(user.id);
}
const newEmbed = new Discord.MessageEmbed()
//code to create the new embed
embedMessage.edit(newEmbed);
}
这部分代码的作用是每当向任何消息添加反应时,都会编辑所有嵌入。这意味着,即使对非嵌入邮件添加响应,也会导致所有嵌入被修改。 messageReactionAdd
是一个全局事件,这意味着它适用于所有邮件,而不仅仅是您的嵌入邮件。
最好的解决方案是使用reaction collector而不是反应事件。反应收集器是在特定消息上创建的,因此只会修改您在其上进行反应的嵌入。
以下是您的代码示例,它不一定是有效的示例,但应为您提供有关如何完成此操作的一般思路:
const raidEmbed = new Discord.MessageEmbed() //create the embed
//some code to fill the embed
message.channel.send(raidEmbed).then(embedMessage => {
embedMessage.react('❌')
.then(()=>embedMessage.react('⭕')
//more react to create all the 'buttons'
const filter = (reaction, user) => (r.emoji.name == "❌" || r.emoji.name == "⭕") && user.id === message.author.id;
const collector = embedMessage.createReactionCollector(filter, { time: 15000 });
collector.on('collect', reaction => {
//some code to do stuff when the right 'button' is pressed
//then reset the button with this code:
reaction.users.remove(message.author.id);
const newEmbed = new Discord.MessageEmbed()
//code to create the new embed
embedMessage.edit(newEmbed);
}
})
您还可以使用filter
来缩小应收集的用户反应以及要收集的特定反应的范围。