Discord.js 有没有办法在handler命令中创建子文件夹?
我正在尝试制作另一个更高级的命令处理程序。我已经看到了几种类型的代码,用folders/subfolders/command解释了handler命令,但我仍然不太明白如何做到这一点 我已经尝试过使用fs,我想用它来做这件事,但我还是做不到 这是我当前的代码(没有尝试)Discord.js 有没有办法在handler命令中创建子文件夹?,discord.js,Discord.js,我正在尝试制作另一个更高级的命令处理程序。我已经看到了几种类型的代码,用folders/subfolders/command解释了handler命令,但我仍然不太明白如何做到这一点 我已经尝试过使用fs,我想用它来做这件事,但我还是做不到 这是我当前的代码(没有尝试) const Discord=require(“Discord.js”); const client=new Discord.client(); const db=require('quick.db'); 常数fs=要求(“fs”)
const Discord=require(“Discord.js”);
const client=new Discord.client();
const db=require('quick.db');
常数fs=要求(“fs”);
client.commands=new Discord.Collection();
client.alias=new Discord.Collection();
client.events=new Discord.Collection();
const-utils=require(“./utils/utils”);
const config=require(“./utils/config.json”);
fs.readdir(“./src/events/”,(错误,文件)=>{
if(err)返回控制台。error(err);
files.forEach(文件=>{
让eventFunction=require(`./src/events/${file}`);
让eventStart=eventFunction.run.bind(null,client);
让eventName=file.split(“.”[0];
client.events.set(eventName,eventStart);
on(eventName,(…args)=>eventFunction.run(client,utils,…args));
});
});
fs.readdir(“./src/commands/”,(错误,文件)=>{
if(err)控制台错误(err);
files.forEach(f=>{
让props=require(`./src/commands/${f}`);
props.fileName=f;
client.commands.set(props.help.name,props);
props.help.alias.forEach(别名=>{
client.alias.set(别名、props.help.name);
});
});
});
client.on(“消息”,异步消息=>{
试一试{
let prefix=await db.fetch(`prefixo_${message.guild.id}`);
if(prefix==null)prefix=“m!”;
if(message.author.bot)返回;
if(message.content.indexOf(prefix)!==0)返回;
const args=message.content.slice(prefix.length.trim().split(+/+/g);
let command=args.shift().toLowerCase();
如果(client.aliases.has(command))command=client.commands.get(client.aliases.get(command)).help.name;
if(client.commands.get(command.config.restricted==true){
if(message.author.id!==config.ownerID)返回utils.errorEmbed(消息“无权限”);
}
if(client.commands.get(command.config.args==true){
如果(!args[0])返回utils.errorEmbed(消息,`无效参数。使用:${prefix+'help'+client.commands.get(command.help.name}`);
}
让commandFile=require(`./src/commands/${command}.js`);
run(客户端、消息、参数、utils);
}捕捉(错误){
if(err.message==='cannotreadproperty'config'of undefined`)返回;
if(err.code==“未找到模块”)返回;
控制台错误(err);
}
});
client.login(config.token);
我也想要同样的东西,我花了一段时间才弄明白!
这是我的ready.js
(ready事件)中的命令处理程序:
我的client.loadCommand()
方法接收文件名f
,以及该文件的文件路径/commands/${c}/${f}
,并使用您的props
方法要求文件路径,并将props.help.name
添加到client.commands
请记住,模块名(const stuff=[“module”,“anothermod”]
)与子文件夹名完全相同,都位于命令的父文件夹下
const { promisify } = require("util");
const readdir = promisify(require("fs").readdir);
const stuff = ['dev','donators', 'fun', 'misc', 'moderation']; //my subfolders (i.e: modules)
stuff.forEach(c => { //loop through each module in the array
readdir(`./commands/${c}/`, (err, files) => { //use fs to read the directory with module name as path
if (err) throw err;
console.log(`Loading a total of ${files.length} commands (${c})`);
files.forEach(f => {
if (!f.endsWith(".js")) return;
client.loadCommand(`${f}`, `./commands/${c}/${f}`);
});
});
});