首页 > 解决方案 > 无法读取未定义 Discord.js 的“执行”

问题描述

我有一个错误:C:\Users\robcio\Desktop\BotFolder\index.js:33 client.commands.get('verify').execute(message, args, Discord, client); ^

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\Users\robcio\Desktop\BotFolder\index.js:33:38)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\robcio\Desktop\BotFolder\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\robcio\Desktop\BotFolder\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\robcio\Desktop\BotFolder\node_modules\ws\lib\websocket.js:825:20)

我的代码是:

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const prefix = '!';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./command').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./command/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Ready');
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    if(command === 'info'){
        message.channel.send('Author: ITS ME')
        message.channel.send('Version: Alpha')
    }
    if(command === 'verify'){
        client.commands.get('verify').execute(message, args, Discord, client);
    }
})

client.login('NOT FOR YOU!');

验证.js:

module.exports = {
    name: 'Verification',
    description: 'Verification',
    execute(message, args, Discord, client) {
        const channel = 'NOPE';
        const VerificatedRole = message.guild.roles.cache.find(role => role.name === "Verificated");

        const VerificatedEmoji = '✅';

        let embed = new Discord.MesssageEmbed()
        .setColor('#043927')
        .setTitle('Verificate')
        .SetDescription('Normal Verification')

        let messageEmbed = new message.channel.send(embed);
        messageEmbed.react(VerificatedEmoji);

        client.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.messsage.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 === VerificatedEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.add(VerificatedRole);
                } else {
                    return;
                }
            }
        });

        client.on('messageReactionRemove', async (reaction, user) => {
            if (reaction.messsage.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 === VerificatedEmoji) {
                    await reaction.message.guild.members.cache.get(user.id).roles.remove(VerificatedRole);
                } else {
                    return;
                }
            }
        });
    }
}

而且我不知道如何修复这个我想我必须使用 const 执行“执行”但是 IDK 怎么做这很简单我只想用验证和其他东西做一个非常简单的机器人

标签: node.jsdiscord.js

解决方案


在 verify.js 文件中,命令名称是,Verification但在您的主文件中,您正在尝试获取verify( ..get('verify')) 并执行它。由于找不到验证命令名称,因此会引发错误。这是因为您正试图进入command.name命令处理程序。因此,在您的情况下,要么将名称更改为verifyverify.js,要么尝试进入Verification主文件,因为命令名称是Verification.

注意: JS 区分大小写,因此请确保两者相同。


推荐阅读