简体   繁体   English

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

[英]Discord.js V12 Message Collector “bug”?

I am making a roster command, and this is my code so far, the error is when it comes to the admin it bugs out like in the picture and it doesn't continue... It also saves all of the answers in a file.我正在制作一个名册命令,这是我到目前为止的代码,错误是当涉及到管理员时,它会像图片中一样出现错误并且它不会继续......它还将所有答案保存在一个文件中. So an admin does $roster setup and it starts saving replies for the names of each file.因此,管理员进行$roster setup并开始保存每个文件名称的回复。 This is the only part I've done for now, so the setup.这是我目前唯一完成的部分,所以设置。 And I am getting that bug there, there's no error in the console.我在那里遇到了那个错误,控制台中没有错误。

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


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





            }
            
        }
    }});

Picture:图片: 漏洞

I believe that the error comes from the original MessageCollector listener not being properly disabled, so the original listener still triggers for each additional message.我相信错误来自原始 MessageCollector 侦听器未正确禁用,因此原始侦听器仍会为每条附加消息触发。

It looks like you tried to stop the original listener with collector.off , but because this is a statement instead of a function call, it doesn't do anything.看起来您试图用collector.off停止原始侦听器,但因为这是一个语句而不是 function 调用,所以它什么也没做。 Furthermore, if it did function, it would end the parent collector and none of the subsequent collector.on callbacks would work.此外,如果它执行 function,它将结束父收集器,并且后续的collector.on回调将不起作用。

Instead, I would replace the collector.on functions with collector.once and remove the collector.off statements.相反,我会用collector.once替换collector.on函数并删除collector.off语句。 Changing it to collector.once automatically ends the listener after it receives the first event, which is what you want in this scenario.将其更改为collector.once会在接收到第一个事件后自动结束侦听器,这就是您在本场景中想要的。 If you wanted to receive more than one event, you'd have to use something else.如果您想接收多个事件,则必须使用其他东西。

For example, the listener would look something like this:例如,监听器看起来像这样:

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 ... */

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM