【问题标题】:How to separately edit multiple discord embeds?如何分别编辑多个不和谐嵌入?
【发布时间】:2021-01-25 13:37:42
【问题描述】:

我创建了一个不和谐机器人,目标是生成带有表情符号按钮的反应式嵌入。我的问题是一旦按下“按钮”,使用机器人创建的所有嵌入都会同时修改。

下面是我的机器人的伪代码:

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);
    }
})

我不明白为什么我的所有嵌入都链接在一起以及如何解决此问题。

【问题讨论】:

    标签: discord.js embed


    【解决方案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 来缩小应收集哪些用户的反应以及您想要收集哪些具体反应的范围。

    【讨论】:

    • 请您解释一下 :) 我修改了您的代码以满足我的需求,它工作得很好! (除了应该是reaction.emoji.name的r.emoji.name):const filter = (reaction) => (reaction.emoji.name == "❌" || reaction.emoji.name == "⭕"); const collector = embedMessage.createReactionCollector(filter); collector.on('collect', (reaction, user) => {
    猜你喜欢
    • 2020-11-12
    • 2021-02-01
    • 2021-05-08
    • 2021-07-06
    • 2021-10-06
    • 2022-10-23
    • 2020-04-20
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多