(节点:44564)UnhandledPromiseRejectionWarning:TypeError:client.on 不是函数

Posted

技术标签:

【中文标题】(节点:44564)UnhandledPromiseRejectionWarning:TypeError:client.on 不是函数【英文标题】:(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function 【发布时间】:2021-09-20 04:11:41 【问题描述】:

我目前正在使用许多不同的命令制作一个不和谐的机器人,在实现了一个 ?beg 和一个 ?bal 命令来乞求我的假想货币“比特”之后,它似乎破坏了很多代码。我能够修复所有问题,除了一个错误,它来自于输入?verify。当您键入 ?verify 时,机器人会向您发送 ?verify 的聊天发送一个嵌入,并要求成员对嵌入做出反应,并打勾以赋予“会员”角色。键入 ?verify 并按 Enter 后,会出现嵌入,并且机器人也会对自身做出反应,并带有刻度,尽管在做出反应时,成员不会获得角色。当我查看终端时,出现了这个错误,

(node:44564) UnhandledPromiseRejectionWarning: TypeError: client.on is not a function
    at Object.execute (C:\Users\013933\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) 

我已经在堆栈溢出时对其进行了搜索,但只是人们说我错误地声明了“客户端”,尽管当“正确”声明客户端时,我只是收到另一个错误,说“客户端”已经定义了它是什么。

这是完整的代码,

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

【问题讨论】:

你知道函数的参数执行顺序和定义顺序有多么不同吗? @MrMythical 抱歉,不,我对 js 很陌生!你能解释一下吗? :D 【参考方案1】:

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

async execute(message, client, args, Discord)

并将其更改为

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

问题在于 message.js command.execute(message, args, cmd, client, Discord, profileData);

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

将来可能会弄乱您的代码的另一件事!把这行const MessageEmbed = require('discord.js');放在最上面..

【讨论】:

以上是关于(节点:44564)UnhandledPromiseRejectionWarning:TypeError:client.on 不是函数的主要内容,如果未能解决你的问题,请参考以下文章

在安装seaborn等包时,使用pip install安装不成功

将 NodeJS Docker 容器连接到 MySQL Docker 容器 - docker-compose 3

Delphi TreeView 选择父节点,选择所有子节点

js DOM知识总结

给出一个二叉树的节点,返回该节点的前驱节点

给出一个二叉树的节点,返回该节点的前驱节点