简体   繁体   English

为什么我收到 TypeError:无法读取未定义的属性“执行”

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

Hey so I've been trying to make a Discord bot, in the spirit of Christmas it will have a command, ".suggestgift" which will start a prompt that figures out a good match for the person.嘿,所以我一直在尝试制作 Discord 机器人,本着圣诞节的精神,它将有一个命令“.suggestgift”,它将启动一个提示,为这个人找出一个很好的匹配。 I keep receiving the error, "TypeError: Cannot read property 'execute' of undefined... I don't know why though, the other commands, written the same way all seem to work. Here's my code...我不断收到错误消息,“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')

The files code:文件代码:

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

module.exports returns an object, which is a key value pair, you are missing the key part for execute function module.exports返回一个 object,这是一个键值对,您缺少执行 function 的关键部分

try changing the module.exports to below尝试将module.exports更改为以下

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

And Welcome to StackOverflow:)欢迎来到 StackOverflow :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在定义的数组上收到“ ERROR TypeError:无法读取未定义的属性'length'的信息”? - Why am I receiving “ERROR TypeError: Cannot read property 'length' of undefined” on a defined array? 为什么我收到无法读取未定义的属性“状态”? - Why am I receiving Cannot read property 'state' of undefined? 为什么我会出错? 类型错误:“无法读取未定义的属性‘执行’” - Why am I getting error? TypeError: “Cannot read property 'execute' of undefined” 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 我正在尝试为三个单独的选项卡运行 onEdit 函数,但收到 TypeError:无法读取未定义的属性“值” - I am trying to run the onEdit function for three separate tabs but receiving TypeError: Cannot read property 'value' of undefined 接收类型错误:无法读取未定义的属性“长度” - 为什么? - Receiving TypeError: Cannot read property 'length' of undefined - why? 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined TypeError:无法读取未定义的属性'addTopic'。 我是盲人吗? - TypeError: Cannot read property 'addTopic' of undefined. Am I blind? 为什么我得到“TypeError:无法读取未定义的属性'恢复'当使用sinon.js删除mongoose时 - Why Am I getting “TypeError: Cannot read property 'restore' of undefined” when stubbing mongoose with sinon.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM