簡體   English   中英

為什么我收到 TypeError:無法讀取未定義的屬性“執行”

[英]Why am I receiving TypeError: cannot read property 'execute' of undefined

嘿,所以我一直在嘗試制作 Discord 機器人,本着聖誕節的精神,它將有一個命令“.suggestgift”,它將啟動一個提示,為這個人找出一個很好的匹配。 我不斷收到錯誤消息,“TypeError: Cannot read property 'execute' of undefined ...我不知道為什么,其他命令以相同的方式編寫似乎都可以工作。這是我的代碼...

const Discord = require('discord.js');
const WOKCommands = require ('wokcommands');
const client = new Discord.Client();

const prefix = '.';

const fs = require('fs');

client.commands = new Discord.Collection();
client.commands.giftsuggest = new Discord.Collection()


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

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


client.once('ready', () => {
    console.log('Santa bot is online');
});

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 === 'clear'){
        client.commands.get('clear').execute(message, args, Discord);
    } else if(command === 'kick'){
        client.commands.get('kick').execute(message, args, Discord);
    } else if(command === 'ban'){
        client.commands.get('ban').execute(message, args, Discord);
    }else if(command === 'ping'){
        client.commands.get('ping').execute(message, args, Discord);
    }else if(command == 'giftprompt'){
        client.commands.giftsuggest.get('giftprompt').execute(message, args);
    }

    
});



client.login('my token would be here')

文件代碼:

module.exports = {
    name: 'suggestprompt',
    description: 'testing',
    execute(message, args){
        message.channel.send('This is testing to see that the command works')
    }
}

module.exports返回一個 object,這是一個鍵值對,您缺少執行 function 的關鍵部分

嘗試將module.exports更改為以下

module.exports = {
    name: 'suggestprompt',
    description: 'testing',
    execute: function(message, args){
        message.channel.send('This is testing to see that the command works')
    }
}

歡迎來到 StackOverflow :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM