javascript - 为什么我收到 TypeError:无法读取未定义的属性“执行”
问题描述
嘿,所以我一直在尝试制作一个 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
返回一个对象,它是一个键值对,您缺少执行函数的关键部分
尝试将其更改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 :)
推荐阅读
- python - 从 Python 中的 Entry 中获取变量以创建 Cloud Env
- node.js - 将图像转换为可读流
- python-3.x - 如何使用python获取在浏览器中打开的url
- mysql - Mysql在have子句中传递变量
- arrays - 数组的程序跟踪?
- shell - 在大文件的每一行中提取唯一值的脚本
- reactjs - reactjs在开发中设置base-href
- php - 如何从 magento 2 中删除沉默错误是不鼓励的错误?
- reactjs - ReactJS + Apollo 客户端 CORS 问题
- android - 一旦在 Firebase 中更改了子值,有什么方法可以防止 onCreate 调用?