类型错误:无法读取未定义(读取"set")不和谐的属性.js v13



我正在制作一个机器人,但是有一个错误!

TypeError: Cannot read properties of undefined (reading 'set')在/home/runner/Bot-legal/structures/slash.js: 12:30在数组中。forEach ()在模块。出口(/home/跑步/Bot-legal/结构/slash.js: 29)在/home/runner/Bot-legal/index.js: 21:39在数组中。forEach ()在对象。(/home/跑步/Bot-legal/index.js: 20:31)

我的代码是
let slash = []
const { readdirSync } = require("fs");
const ascii = require("ascii-table");
let table = new ascii("Slash Commands");
table.setHeading('Slash Command', ' Load status');
module.exports = (client) => {
readdirSync("./Slash/").forEach(dir => {
const commands = readdirSync(`./Slash/${dir}/`).filter(file => file.endsWith(".js"));
for (let file of commands) {
let pull = require(`../Slash/${dir}/${file}`);
if (pull.name) {
client.slash.set(pull.name, pull);
slash.push(pull);
table.addRow(file, '✔️ -> Loaded Slash Command');
} else {
table.addRow(file, `❌  -> missing a help.name, or help.name is not a string.`);
continue;
}
}
});
console.log(table.toString());
client.on("ready",async ()=> {
await client.guilds.cache.get('839096342953328691').commands.set(slash);
})
}

有人能找到错误吗?

尝试一下,更改了一些let并使它们成为const,因为它们永远不会重新分配

const slash = []
const fs = require("fs");
const ascii = require("ascii-table");
const table = new ascii("Slash Commands").setHeading('Slash Command', ' Load status');
module.exports = client => {
fs.readdirSync("./Slash/").forEach(dir => {
const commands = fs.readdirSync(`./Slash/${dir}/`).filter(file => file.endsWith(".js"));
for (const file of commands) {
const pull = require(`./Slash/${dir}/${file}`); // had too many periods
if (pull.name) {
client.commands.set(pull.name, pull);
slash.push(pull);
table.addRow(file, '✔️ -> Loaded Slash Command');
} else {
table.addRow(file, `❌  -> missing a help.name, or help.name is not a string.`);
continue;
}
}
});
console.log(table.toString());
// Below not needed since they are set above
// client.on("ready", async () => {
//  await client.guilds.cache.get('839096342953328691').commands.set(slash);
// })
}

最新更新