错误:无法读取未定义的属性“执行”

Posted

技术标签:

【中文标题】错误:无法读取未定义的属性“执行”【英文标题】:error : Cannot read property 'execute' of undefined 【发布时间】:2021-10-08 22:21:22 【问题描述】:

您好,我尝试为我的 discord 机器人创建一个命令处理程序,但是当我尝试我的 !ping 命令时,我总是收到错误消息

这是我的 main.js 文件

const Discord = require('discord.js')

const client = new Discord.Client()

const TOKEN ,PREFIX = require('./config')

const fs = require('fs')

client.commands = 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.on('ready', () =>
    console.log("I'm 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 == 'ping')
     client.commands.get('ping').execute(message,args)
 

);

client.login(TOKEN)


还有我的 ping.js 文件

module.export = 
    name: 'ping',
    description: "ping",
    execute(message, args)
    
    message.channel.send('pong')

    


我遇到的错误

C:\discord\main.js:29
     client.commands.get('ping').execute(message,args)
                                ^

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

【问题讨论】:

【参考方案1】:

使用module.exports,因为模块上不存在export属性(export需要复数)。

【讨论】:

以上是关于错误:无法读取未定义的属性“执行”的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript、Discord.js、Node.js 类型错误:无法读取未定义的属性“执行”

未捕获的类型错误:无法读取未定义的属性“toLowerCase”

错误:无法读取未定义的属性“地图”

数据表:未捕获的类型错误:无法读取未定义的属性“长度”

core.js:30235 未捕获类型错误:无法读取未定义的属性“id”

尝试执行 react-native 应用程序时出现“错误无法读取未定义的属性‘拆分’”