带有子类别文件夹的命令处理程序 [英] Command Handler with Sub-Categorized folders

查看:49
本文介绍了带有子类别文件夹的命令处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前正在使用的命令处理程序,它可以按预期的方式工作。

  try {
let ops = {
active:active
}

let commandFile = require(`./commands / $ {cmd} .js` )
commandFile.run(client,message,args,ops);
} catch(e){
console.log(e);
}

但是您可以看到,它只是读入commands文件夹并拉出 .js 个文件。

我要做的是将命令分类为自己的 OCD用途,这样我就可以更好地跟踪它们。

此命令处理程序可以这样做吗?



此外,我已经尝试过 discord.js-commando ,我个人不喜欢它使用的命令结构。

解决方案

我会使用<一个href = https://www.npmjs.com/package/require-all rel = nofollow noreferrer> 全部需要 包。 / p>

我们假设您具有以下文件结构:




 命令:
文件夹1:
file1.js
文件夹2:
子文件夹:
file2.js

您可以使用全部必需完全需要所有这些文件:

  const required = require( 'require-all']({
dirname:__dirname +'/ commands',//到'commands'目录的路径
filter:/(.+)\.js$/,//与文件名
匹配的RegExp excludeDirs:/^\.(git|svn)|samples$/,//要排除
的目录递归:true //允许递归(子文件夹)研究
});

上面的必需变量看起来像这样:

  // / * export * /表示从模块
导出的对象{
文件夹1:{file1:/ * export * /},
文件夹2:{
子文件夹:{file2:/ * export * /}
}
}

为了获得所有命令,您需要使用递归函数扫描该对象:

  const命令= {}; 

(函数searchIn(obj = {}){
for(在obj中输入键){
const potentialCommand = obj [key];

//如果是命令,则将其保存在命令对象
中,如果(potentialCommand.run)命令[key] = potentialCommand;
//如果是目录,则也以递归方式搜索
searchIn(potentialCommand);
}
})(必需);

要执行命令时,只需调用:

  commands ['command-name']。run(client,message,args,ops)

您可以在代表。


This is the command handler I'm currently using, and it works as it's supposed to.

try {
  let ops = {
    active: active
  }

  let commandFile = require(`./commands/${cmd}.js`)
  commandFile.run(client, message, args, ops);
} catch (e) {
  console.log(e);
}

But as you can see, it just reads into the commands folder and pulls the .js files from there.
What I'm looking to do, is to have the commands be sub-categorized for my own "OCD" purposes, so that I can keep track of them better on my end.
Is there any way with this command handler to do so?

Also, I've already tried discord.js-commando and I don't personally like the command structure it uses.

解决方案

I would use the require-all package.

Let's assume that you have a file structure like the following:

commands:
  folder1:
    file1.js
  folder2:
    subfolder:
      file2.js

You can use require-all to require all those files altogether:

const required = require('require-all')({
  dirname: __dirname + '/commands', // Path to the 'commands' directory
  filter: /(.+)\.js$/, // RegExp that matches the file names
  excludeDirs: /^\.(git|svn)|samples$/, // Directories to exclude
  recursive: true // Allow for recursive (subfolders) research
});

The above required variable will look like this:

// /*export*/ represents the exported object from the module
{
  folder1: { file1: /*export*/ },
  folder2: { 
    subfolder: { file2: /*export*/ } 
  }
}

In order to get all the commands you need to scan that object with a recursive function:

const commands = {};

(function searchIn(obj = {}) {
  for (let key in obj) {
    const potentialCommand = obj[key];

    // If it's a command save it in the commands object
    if (potentialCommand.run) commands[key] = potentialCommand;
    // If it's a directory, search recursively in that too
    else searchIn(potentialCommand);
  }
})(required);

When you want to execute the command, simply call:

commands['command-name'].run(client, message, args, ops)

You can find a working demo (with strings) at this repl.

这篇关于带有子类别文件夹的命令处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆