Discord.js V12消息收集器“错误”?

时间:2020-07-04 16:27:08

标签: javascript discord discord.js

我正在执行一个名册命令,这是我到目前为止的代码,错误是当涉及到管理员时它会像图片中那样错误地发出,并且不会继续...它还保存了所有答案在一个文件中。 因此,管理员执行$roster setup,并开始保存对每个文件名的答复。这是我目前唯一要做的部分,因此是设置。而且我在那里遇到了这个错误,控制台中没有错误。

client.on('message', async message => {
    if (message.content.startsWith(prefix + "roster")) {
    if (!message.member.hasPermission('ADMINISTRATOR')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))
    if (message.author.bot){}
        else {
            !fs.existsSync(`./roster/` + message.guild.id) && fs.mkdirSync(`./roster/` + message.guild.id, { recursive: true })
            const args = message.content.slice(prefix.length + 7).split(/ +/)
            let uReply = args[0];
            if(!uReply) return message.channel.send("Please use the following options: `setup`, `send`, `add` or `remove`!")

            //SETUP
            if(uReply === "setup") {
                const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { time: 10000 });
            
                message.channel.send("Please write the exact role name of leader role (with emojis if it has).")
                collector.on('collect', message => {
                    let leaderRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `Leader`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Ok, the leader role is called \`${leaderRole}\`.`) 
                    collector.off
                

                message.channel.send("Now, whats the role name of co-leader or co-owner role (with emojis if it has)?")
                collector.on('collect', message => {
                    let coleaderRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `Co-Leader`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Ok, the co-leader/co-owner role is called \`${coleaderRole}\`.`) 
                    collector.off


                message.channel.send("Now, whats the role name of admin role (with emojis if it has)?")
                collector.on('collect', message => {
                    let adminRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `Admin`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Ok, the admin role is called \`${adminRole}\`.`) 
                    collector.off
 

                message.channel.send("Awesome, now whats the role name of staff role (with emojis if it has)?")
                collector.on('collect', message => {
                    let staffRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `Staff`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Ok, the staff role is called \`${staffRole}\`.`)
                    collector.off


                message.channel.send("Cool, now whats the role name of tiny-staff role (with emojis if it has)?")
                collector.on('collect', message => {
                    let tinyStaffRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `Tiny-Staff`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Ok, the tiny-staff role is called \`${tinyStaffRole}\`.`)
                    collector.off


                message.channel.send("Just a few more, now whats the role name of higher ranked members role (with emojis if it has)?")
                collector.on('collect', message => {
                    let HigherRankedRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `HigherRanked`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Ok, the higher ranked members role is called \`${HigherRankedRole}\`.`)
                    collector.off


                message.channel.send("Last one, whats the role name of the normal members role (with emojis if it has)?")
                collector.on('collect', message => {
                    let NormalMembersRole = message.content
                    fs.writeFileSync(`./roster/${message.guild.id}/`+ `NormalMembers`+".txt", message.content, (err) => console.error);
                    message.channel.send(`Awesome, the normal members role is called \`${NormalMembersRole}\`.`)
                    message.channel.send("That's it for setup.")
                    collector.off


                })})})})})})})





            }
            
        }
    }});

图片: Bug

1 个答案:

答案 0 :(得分:1)

我认为该错误是由于未正确禁用原始MessageCollector侦听器所致,因此原始侦听器仍会为每条其他消息触发。

您似乎尝试使用collector.off停止原始侦听器,但是由于这是一条语句而不是函数调用,因此它无能为力。此外,如果它确实起作用,它将结束父收集器,并且随后的collector.on回调均不起作用。

相反,我将collector.on函数替换为collector.once并删除了collector.off语句。将其更改为collector.once会在接收到第一个事件后自动终止侦听器,这是您在此情况下想要的。如果您想接收多个事件,则必须使用其他事件。

例如,监听器看起来像这样:

collector.once('collect', message => {
    let leaderRole = message.content
    fs.writeFileSync(`./roster/${message.guild.id}/`+ `Leader`+".txt", message.content, (err) => console.error);
    message.channel.send(`Ok, the leader role is called \`${leaderRole}\`.`)

    /* ... Rest of code ... */

}