gpt4 book ai didi

javascript - 如何使用 forEach() 定义文件路径集合

转载 作者:行者123 更新时间:2023-12-03 00:15:49 25 4
gpt4 key购买 nike

尝试通过将重复操作放入 forEach() 函数中来修复它。 console.log 提供所需的结果,但不提供代码本身。你能解释一下为什么它不起作用吗?

const bot = new Discord.Client({disableEveryone: true});
const cmdHandler = ["commands","automation"];
const fileSys = require("fs");

cmdHandler.forEach((v, y) => {
bot.v = new Discord.Collection();
console.log(v);

fileSys.readdir(`./${v}/`, (error, file) => {

if(error) console.log(error);

let jsfile = file.filter(f => f.split (".").pop() === "js")
if(jsfile.length <= 0){
console.log("Couldn't find the commands.");
return
}

jsfile.forEach((f, i) => {
let props = require(`./${v}/${f}`);
console.log(`${f} loaded.`);
bot.v.set(props.help.name, props);
});
});
});

let fullCmd = msg.content.substr(prefixlen);
let splitCmd = fullCmd.split(" ");
let mainCmd = splitCmd[0];
let args = splitCmd.slice(1);

// Set variable for directory content called from ./commands
let commandFile = bot.commands.get(mainCmd);
// execute "run" section of command
if(commandFile){
commandFile.run(bot,msg,args);

无法读取未定义的属性“get”

最佳答案

这只是向机器人添加一个属性v并分配其值。

bot.v = new Discord.Collection();
// bot.v has a value

看起来您正尝试使用数组元素作为属性名称。

bot[v] = new Discord.Collection();
// bot.commands assigned, ... bot.automation assigned

关于javascript - 如何使用 forEach() 定义文件路径集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54524700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com