node.js - discord.js“类型错误:无法读取未定义的属性‘执行’”
问题描述
我正在尝试使用 discord.js 制作一个机器人并遇到这个错误我不知道如何解决,我一直在寻找几个小时但找不到答案,当我运行机器人时,它登录成功,但是当您运行命令时,powershell 控制台会抛出错误
TypeError:无法读取未定义的属性“执行”
这是我的主要代码
const Discord = require('discord.js');
const config = require('./config.json');
const client = new Discord.Client();
const { prefix, token } = require('./config.json');
const ms = require('ms');
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('This command has an issue.')
}
// other commands...
});
client.login(token);
这是一个命令 .js 文件,此命令旨在清除 500 - 2 内的 x 条消息
module.exports = {
name: "purge",
description: "Deletes input amout of messages.",
async execute(message, args) {
if (message.member.hasPermission(MANAGE_MESSAGES)) {
const deleteCount = parseInt(args[0], 10);
const deleteMessage = `Deleted ${deleteCount} messages.`;
if (!deleteCount || deleteCount > 500 || deleteCount < 2) return message.reply(`${message.author} please input a number between 2 - 500.`);
const fetched = await message.channel.fetchMessages({
limit: deleteCount
});
message.channel.bulkDelete(fetched)
.catch(err => console.log(`Cannot delete message because of ${err}`))
.then(message.reply(deleteMessage))
.catch(err => {
console.log(err);
})
} else {
message.reply('You do not have permissions to purge.')
}
}
}
错误信息是:
TypeError: Cannot read property 'execute' of undefined
at Client.<anonymous> (C:\Users\ryssu\source\repos\SCP\079\app.js:24:11)
at Client.emit (events.js:315:20)
at MessageCreateAction.handle (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.j
s:4:32)
at WebSocketManager.handlePacket (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\ryssu\source\repos\SCP\079\node_modules\ws\lib\event-target.js:125:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (C:\Users\ryssu\source\repos\SCP\079\node_modules\ws\lib\websocket.js:797:20)
如果有人可以帮助我,我将不胜感激。
解决方案
您实际上并未将任何命令设置到commands
集合中,因此任何获取命令的尝试都将返回未定义。
// create the collection
client.commands = new Discord.Collection();
// get an array of every file in the commands folder
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
// iterate a function through every file
for (file of commandFiles) {
const command = require(`./commands/${file}`);
// map the command to the collection with the key as the command name,
// and the value as the whole exported object
client.commands.set(command.name, command);
};
推荐阅读
- python - 在 GNURadio/GRC 中,如何顺序使用不同的音频源作为输入,而无需手动启动和停止?
- c# - 如何将实体框架与 RootObject DTO 一起使用?
- linux - GNU 以 $(MAKELEVEL) 为条件 - 三个案例 0, 1 更多
- python - mock.assert_call_with 返回不一致的结果
- python - Python - 如何安装属性
- java - 图像未出现在 JFrame 中
- asp.net - MassTransit:为多个 API 实例配置队列
- python - Ford-Fulkerson 和一个例子,验证需求
- r - 对一个特定列进行 glm 建模时出错
- c - 为什么我的程序没有显示任何输出?0 个错误,0 个警告但没有输出?我正在使用 Dev C++ 编译器