(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function

(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function

我目前正在制作一个包含大量不同命令的 discord 机器人,在执行了一个 ?beg 和一个 ?bal 命令来乞求我想象中的货币 'bits' 之后,它似乎经常崩溃的代码。我能够修复除一个错误之外的所有问题,该错误来自键入 ?verify。当您键入 ?verify 时,机器人会向您发送 ?verify 的聊天发送一个嵌入,并要求成员对嵌入做出反应,并打勾以赋予 'Member' 角色。在输入 ?verify 并按下 enter 后,嵌入出现,并且机器人也会对自己做出反应,但在做出反应时,成员并没有得到这个角色。当我查看终端时,出现了这个错误,

(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function
    at Object.execute (C:\Users3933\Desktop\Vixe\commands\verify.js:20:16)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(node:44564) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:44564) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这很奇怪,因为 client.on 是在代码顶部定义的函数。

async execute(message, client, args, Discord) {

我在堆栈溢出时搜索了它,但只是有人说我错误地声明了 'Client',尽管当 'correctly' 声明客户端时我只是得到另一个错误,说 'client' 已经定义它是什么。

这是完整的代码,

module.exports = {
    name: 'verify',
    description: 'Allows members to react to a message to verify themselves.',
    async execute(message, client, args, Discord) {
        const channel = message.channel.id;
        const memberRole = message.guild.roles.cache.find(role => role.name === 'Member');

        const memberEmoji = '✅';
        const { MessageEmbed } = require('discord.js');

        let embed = new MessageEmbed()
            .setColor('#800080')
            .setTitle('Verification')
            .setDescription('React to this embed with the :white_check_mark: to verify yourself and gain access to the server.\n'
                + `Removing your reaction to this embed will un-verify you.`);

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(memberEmoji);

        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;

            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === memberEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(memberRole);
                }
            } else {
                return;
            }
        });

        client.on('messageReactionRemove', async (reaction, user) => {
            if (reaction.message.partial) await reaction.message.fetch();
            if (reaction.partial) await reaction.fetch();
            if (user.bot) return;
            if (!reaction.message.guild) return;

            if (reaction.message.channel.id == channel) {
                if (reaction.emoji.name === memberEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(memberRole);
                }
            } else {
                return;
            }
        });
    }
}

这是处理所有命令的 message.js,

const profileModel = require('../../models/profileSchema');

module.exports = async (Discord, client, message) => {
    const prefix = '?';
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    let profileData;
    try {
        profileData = await profileModel.findOne({ userID: message.author.id });
        if(!profileData) {
            let profile = await profileModel.create({
                userID: message.author.id,
                serverID: message.guild.id,
                bits: 1000,
                bank: 0,
            });
            profile.save();
        }
    } catch (err) {
        console.log(err);
    }

    const args = message.content.slice(prefix.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    const command = client.commands.get(cmd);

    try {
        command.execute(message, args, cmd, client, Discord, profileData);
    } catch (err) {
        message.reply('There was an error executing this command.');
        console.log(err);
    }
};

这是一个简单的修复方法,只需找到这一行:

async execute(message, client, args, Discord)

并将其更改为

async execute(message, args, cmd, client, Discord, profileData)

问题是 message.jscommand.execute(message, args, cmd, client, Discord, profileData);

执行参数,其中“message、args、cmd、client、Discord、profileData” 但在您的验证文件中,参数“消息、客户端、参数、不和谐” 您没有在客户端之前添加“args,cmd”,因此客户端定义错误

另一件事将来可能会弄乱您的代码!将这一行 const { MessageEmbed } = require('discord.js'); 放在顶部..