[英]discord.js | Cannot read property 'set' of undefined
I'm making my command handler in discord.js v12, and I keep on getting this error, which is:我正在 discord.js v12 中创建我的命令处理程序,但我不断收到此错误,即:
Cannot read property 'set' of undefined
无法读取未定义的属性“设置”
Here is my code, this code is in a folder called handlers
这是我的代码,此代码位于名为
handlers
的文件夹中
const { readdirSync, readdir } = require('fs');
const ascii = require('ascii-table');
let table = new ascii('commands');
table.setHeading('Commands', 'Status');
module.exports = (client) => {
readdirSync('./commands/').forEach((dir) => {
const commands = readdirSync(`./commands/${dir}/`).filter((file) =>
file.endsWith('.js')
);
for (let file of commands) {
let pull = require(`../commands/${dir}/${file}`);
if (pull.name) {
client.commands.set(pull.name, pull);
table.addRow(file, '✅');
} else {
table.addRow(
file,
`❌ -> Missing a help.name, or help.name in not a string.`
);
continue;
}
if (pull.aliases && Array.isArray(pull.aliases))
pull.aliases.forEach((alias) => client.aliases.set(aliases, pull.name));
}
});
console.log(table.toString());
};
In my entry point, which is main.js
, I have made a new collection.在我的入口点
main.js
,我创建了一个新集合。
The problem here is your working in a separate file so you haven't got access to the variables defined in the other这里的问题是您在一个单独的文件中工作,因此您无法访问另一个文件中定义的变量
to get around this, in you main.js file you need to use a module.exports.client = client
and the in the handler.js file you can do const client = require("../main.js").client
为了解决这个问题,在你的 main.js 文件中你需要使用
module.exports.client = client
并且在module.exports.client = client
文件中你可以做const client = require("../main.js").client
You must also make sure that when defining client.commands
you give it the correct object type.您还必须确保在定义
client.commands
时为其提供正确的对象类型。 It must be client.commands = new Discord.Collection()
它必须是
client.commands = new Discord.Collection()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.