【发布时间】:2021-10-23 12:32:21
【问题描述】:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
const fs = require('fs');
client.command = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.command.set(command.name, command);
}
client.once('ready', () => {
console.log('Primio Has Activated :)');
});
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 === 'flip') {
client.command.get('flip').execute(message, args,);
} else if (command === 'help') {
client.command.get('help').execute(message, args,);
}
});
client.login('token');
我不断收到错误提示:
TypeError: Cannot read property 'execute' of undefined
at Client.<anonymous> (C:\Users\Pokey\Desktop\Primio\main.js:29:35)
at Client.emit (node:events:394:28)
at MessageCreateAction.handle (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\Pokey\Desktop\Primio\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:394:28)
at Receiver.receiverOnMessage (C:\Users\Pokey\Desktop\Primio\node_modules\ws\lib\websocket.js:833:20)
【问题讨论】:
-
看起来
client.command.get('flip')返回未定义。
标签: javascript node.js discord.js