最大反应问题Discord.js

时间:2020-06-29 20:37:51

标签: node.js discord.js

我的目标是使用添加反应的命令,您可以进行反应来更改文本,因此,当我单击并将将原始文本编辑为1的反应1时,其他的都相同...这很有效,但是当我尝试删除并重新添加反应时,该方法不起作用,我认为是因为:{ max: 1 }这是我的代码:

bot.on('message', msg => {
    if(msg.content === PREFIX + "test") {
        msg.channel.send(`${msg.author.username}, exemple`).then((sentMessage) => {
            
            setTimeout(function() {
                sentMessage.react("⏪");
            }, 500);

            setTimeout(function() {
                sentMessage.react("⬅️");
            }, 1000);

            setTimeout(function() {
                sentMessage.react("➡️");
            }, 1500);

            setTimeout(function() {
                sentMessage.react("⏩");
            }, 2000);

            let reactionFilter = (reaction, user) => {
                reaction.emoji.name === '⏪' && user.id !== "bot id" && user.id == msg.author.id
            }
            sentMessage.awaitReactions(reactionFilter, { max: 1 }).then(() => {
                sentMessage.edit(`1`)
                }
            )

            let reactionFilter2 = (reaction, user) => { 
                reaction.emoji.name === '⬅️' && user.id !== "bot id" && user.id === msg.author.id
            }
            sentMessage.awaitReactions(reactionFilter2, { max: 1 }).then(() => {
                sentMessage.edit(`2`)
                }
            )

            let reactionFilter3 = (reaction, user) => {
                reaction.emoji.name === '➡️' && user.id !== "bot id" && user.id === msg.author.id
            }
            sentMessage.awaitReactions(reactionFilter3, { max: 1 }).then(() => {
                sentMessage.edit(`3`)
                }
            )

            let reactionFilter4 = (reaction, user) => {
                reaction.emoji.name === '⏩' && user.id !== "bot id" && user.id === msg.author.id
            }
            sentMessage.awaitReactions(reactionFilter4, { max: 1 }).then(() => {
                sentMessage.edit(`4`)
                }
            )
        })
    }
})

谢谢!

1 个答案:

答案 0 :(得分:0)

是的,因为有最大限制,您应该改为使用一个反应收集器:

const emojis = ["⏪", "⬅️", "➡️", "⏩"];
//no need to check if the user id isnt the bot 
const filter = (reaction, user) => emojis.includes(reaction.emoji.name) && user.id === msg.author.id;

// adding a time is probably a better option
const collector = sentMessage.createReactionCollector(filter, { time: 5000 });
collector.on("collect", reaction => {
    // array is 0 indexed so just add 1 to the index and you get the number you wanted
    // might need to convert the number into a string aswell
    sentMessage.edit("" + (emojis.indexOf(reaction.emoji.name) + 1));
});

应该注意,如果删除了一个反应,它什么也不做,我认为这是最好的,因为删除文本后,很难决定文本应该是什么