如何修复“TypeError:无法读取未定义的属性‘执行’”?

时间:2021-06-28 07:55:36

标签: javascript node.js discord.js

我正在尝试让我的 kick 命令 运行,但是如果我想测试它,我会遇到这个错误

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

这是我的index.js

require('dotenv').config();
const Discord = require("discord.js");
const fetch = require("node-fetch");
const client = new Discord.Client();
let randomNumber;
prefix = "!";
client.commands = new Discord.Collection();
var glume = []

function getJockes() {
    random(15);
    joke = glume[randomNumber];
    console.log(randomNumber);
    console.log(joke);
}

function random(number) {
    randomNumber = Math.floor(Math.random() * number + 1);
}

function getQuote() {
    return fetch("https://zenquotes.io/api/random")
        .then(res => {
            return res.json()
        })
        .then(data => {
            return data[0]["q"] + " -" + data[0]["a"]
        })
}

client.on("ready", () => {
    console.log(`Logged in as ${client.user.tag}!`)
})

client.on("message", async msg => {

    if (msg.author.bot) return;
    if (!msg.content.startsWith(prefix)) return;


    //comenzi basic
    if (msg.content.startsWith(prefix + "inspire")) {
        getQuote().then(quote => msg.channel.send(quote))
        console.log('inspire');
    }
    if (msg.content.startsWith(prefix + "ping")) {
        msg.channel.send("pong");
        console.log('Ping-Pong');
    }
    if (msg.content.startsWith(prefix + "zar")) {
        random(6);
        console.log(randomNumber);
        msg.channel.send(randomNumber);
    }
    if (msg.content.startsWith(prefix + "cappajura")) {
        random(2);
        console.log(randomNumber);
        if (randomNumber === 1) {
            msg.channel.send("Cap");
        }
        else {
            msg.channel.send("Pajura");
        }
    }
    if (msg.content.startsWith(prefix + 'glume')) {
        getJockes();
        msg.channel.send(joke);
    }
    if (msg.content.startsWith(prefix + "kick")) {
        client.commands.get('kick').execute(msg, args);
    }
})
client.login(process.env.DISCORDJS_BOT_TOKEN)

错误出现在这一行:

<块引用>

client.commands.get('kick').execute(msg, args);

这是kick.js

module.exports = {
    name: 'kick',
    description: 'kick people',
    execute(msg, args) {
        const channel = '858963360909099068';
        const impostor = msg.mentions.members.first();
        const member = msg.author.id;
        let botResponse;
        let embed = new Discord.MessageEmbed()
            .setColor('#f9ae0e')
            .setTitle('Kick')
            .setDescription(botResponse)
            .addFields(
                { Impostor: impostor },
                { KickAuthor: member }
            );
        let messageEmbed = await msg.channel.send(embed);

        client.on("message", msg => {
            if (msg.member.roles.cache.some(role => role.name === 'ADMIN')) {
                if (!impostor) {
                    botResponse = 'You need to tag the person that you want to be kicked';
                    msg.channel.send(embed);
                }
                else {
                    botResponse = impostor.displayName + " was succesfully kicked";
                    msg.channel.send(embed);
                }
            }
            else {
                botResponse = 'You are not able to kick people';
                msg.channel.send(embed);
            }
        })
    }
}

0 个答案:

没有答案