首页 > 解决方案 > 最大反应 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`)
                }
            )
        })
    }
})

谢谢 !

标签: node.jsdiscord.js

解决方案


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

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

应该注意,如果删除了反应,这不会做任何事情,我认为这是最好的,因为在删除后很难确定文本应该是什么


推荐阅读