【问题标题】:TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefinedTypeError [ERR_INVALID_CALLBACK]:回调必须是函数。收到未定义
【发布时间】:2020-07-28 19:54:15
【问题描述】:

我的验证码有点问题。

client.on('guildMemberAdd', async member => {
    const captcha = await createCaptcha();
    try {
        const msg = await member.send('Vous avez 60 secondes pour resoudre ce captcha !', {
            files: [{
                attachment: `${__dirname}/captchas/${captcha}.png`,
                name: `${captcha}.png`
            }]
        });
        try {
            const filter = m => {
                if (m.author.bot) return;
                if (m.author.id === member.id && m.content === captcha) return true;
                else {
                    m.channel.send('Vous avez mal rentrer le captcha.');
                    return false;
                }
            };
            const response = await msg.channel.awaitMessages(filter, {
                max: 1,
                time: 20000,
                errors: ['time']
            });
            if (response) {
                await msg.channel.send("Vous venez de vous vérifier ! Vous avez alors l'accès complet au serveur");
                await member.roles.add('737228969426026635');
                await fs.unlink(`${__dirname}/captchas/${captcha}.png`)
                    .catch(err => console.log(err));
            }
        } catch (err) {
            console.log(err);
            await msg.channel.send("Vous n'avez pas résolu le captcha.");
            await member.kick();
            await fs.unlink(`${__dirname}/captchas/${captcha}.png`)
                .catch(err => console.log(err));
        }
    } catch (err) {
        console.log(err);
    }
});

我有这个错误:

TypeError [ERR_INVALID_CALLBACK]: Callback must be a function. Received undefined
    at makeCallback (fs.js:152:11)
    at Object.unlink (fs.js:1118:14)
    at Client.<anonymous> (C:\Users\wstep\OneDrive\Bureau\DaliaMC\index.js:88:22)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 'ERR_INVALID_CALLBACK'
}

机器人所说的内容有一个捕获

机器人怎么说 =>

感谢您的帮助!

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    这是因为用于删除您的验证码图像的unlink 方法需要一个回调函数,该回调函数将在您的文件被删除后执行。

    client.on('guildMemberAdd', async member => {
        const captcha = await createCaptcha();
        try {
            const msg = await member.send('Vous avez 60 secondes pour resoudre ce captcha !', {
                files: [{
                    attachment: `${__dirname}/captchas/${captcha}.png`,
                    name: `${captcha}.png`
                }]
            });
            try {
                const filter = m => {
                    if (m.author.bot) return;
                    if (m.author.id === member.id && m.content === captcha) return true;
                    else {
                        m.channel.send('Vous avez mal rentrer le captcha.');
                        return false;
                    }
                };
                const response = await msg.channel.awaitMessages(filter, {
                    max: 1,
                    time: 20000,
                    errors: ['time']
                });
                if (response) {
                    await msg.channel.send("Vous venez de vous vérifier ! Vous avez alors l'accès complet au serveur");
                    await member.roles.add('737228969426026635');
                    fs.unlinkSync(`${__dirname}/captchas/${captcha}.png`);
                }
            } catch (err) {
                console.log(err);
                await msg.channel.send("Vous n'avez pas résolu le captcha.");
                await member.kick();
                fs.unlinkSync(`${__dirname}/captchas/${captcha}.png`);
            }
        } catch (err) {
            console.log(err);
        }
    });
    

    您可以像上面一样使用unlinkSync,因此它不需要回调函数。您也可以使用fs.promises.unlink() 保持等待。

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2017-05-08
      相关资源
      最近更新 更多