我如何优化多个 require() 调用?

How do i optimise multiple require() call?

我正在为我公司最重要的服务器制作一个机器人(比如 discord 机器人),对于我的机器人命令,我为每个命令创建了一个文件。

想法是要发送命令,用户必须发送类似“!giveChannelPerms arg1 arg 2 ...”的消息。机器人将解析消息以识别命令(在本例中为 !giveChannelPerms)并执行与命令相关的代码。

问题是对于每个命令,我必须 require() 文件并创建一个 if {} else if {} else if {} ... 来找到命令,正如您在代码中看到的下面。

const giveChannelPerms = require('../cmd/giveChannelPerms');
const removeChannelPerms = require('../cmd/removeChannelPerms');

[...]

if (cmd == "!giveChannelPerms") {
    giveChannelPerms.giveChannelPerms(post, args, db, obj);
} else if (cmd == "!removeChannelPerms") {
    removeChannelPerms.removeChannelPerms(post, args, db, obj);
}

如果我们的机器人只有 2 个命令,这段代码很好,但我创建的命令越多,require() 和 if {} else if {} 就会越多。

难道没有更“优化”的方法来完成我想做的事情吗?我曾想过做一些类似 C 函数指针的事情,但我不知道该怎么做。

也许这样的事情会奏效

[...]
require('../cmd/giveChannelPerms').then(res => { 
    if (res) {
        res.giveChannelPerms(post, args, db, obj);
    } else {
        require('../cmd/removeChannelPerms').then(rslt =>  {
             if (rslt) rslt.removeChannelPerms(post, args, db, obj);
        })
    }
});
  

借用 尝试:

const commandFiles = {
    giveChannelPerms: require('../cmd/giveChannelPerms'),
    removeChannelPerms: require('../cmd/removeChannelPerms')
};

const commandList = Object.keys(commandFiles);

if (cmd.match(new RegExp('^!(' + commandList.join('|') + ')$'))) {
    let baseCommand = cmd.replace('!', '');
    commandFiles[baseCommand][baseCommand](post, args, db, obj);
}

现在您需要做的就是将命令添加到 commandFiles 对象。

如果您想要减少 require 和 reduce if elses,我建议您创建一个文件来导入您的命令并返回关联的映射

const { giveChannelPerms } = require('../cmd/giveChannelPerms');
const { removeChannelPerms } = require('../cmd/removeChannelPerms');

const cmdMap = new Map();

cmdMap.set('!giveChannelPerms', giveChannelPerms)
cmdMap.set('!removeChannelPerms', removeChannelPerms)

export default cmdMap

然后您将只能导入一次并在您的文件中无条件地使用它:

// Imported multiples functions in one require
const commands = require('../cmd/commands');

// Will execute function associated to cmd string without conditions
commands.get(cmd)(post, args, db, obj);

我认为您正在寻找以下两种解决方案,希望对您有所帮助

  1. 更优雅的导入管理方式 - 为此,您可以创建一个索引文件 (index.js),它将从不同的文件导入所有命令,并从一个地方导出所有命令,即 index.js
  2. 简化您的 if else 条件 - 从您的代码片段来看,您似乎需要针对每个命令进行评估,因为随后执行的代码对于每个命令都是不同的。由于无法逃避,switch-case 将提供比 if-else
  3. 更好的代码可读性
const Cmds = require('../cmd/index');
// or you use import as below
//const {giveChannelPerms, removeChannelPerms} = require('../cmd/index')

switch(cmd)
case: '!giveChannelPerms'
   Cmds.giveChannelPerms.giveChannelPerms(post, args, db, obj);
case: '!removeChannelPerms' 
    Cmds.removeChannelPerms.removeChannelPerms(post, args, db, obj);
defualt:
     // do-somthing
}