Way to delegate command to methods in different files What I want to do is for an example command have it defined in its own file - e.g. command.js - and the same for the other methods. I want to make a separate file for every module - for example &quot;command.js&quot;, &quot;vanish.js&quot; and I can not think of a way to call the methods without making a big switch statement in a file where I would require all the &quot;modules&quot;.
This is how I do it currently:
const client = require('../../../src/bot');
const logger = require('../../../config/logger');
const {isCommand} = require('../../../helpers/checkCommand');
const {addCommand, deleteCommand, updateCommand} = require('../../../db/commandFunctions')
const isAuthorized = require('../isAuthorized');
const builtInCmdHelpers = require('../builtInCommandHelpers');

const USER_MODULES = 'everyone';
const MOD_MODULES = 'moderator';

const executeBuiltInCommand = async ({channel, args}, userstate) =&gt; {
  const isSubArg = await isCommandSubArg(args[1]);
  if(isSubArg) await builtInCmdHelpers[args[1]].apply(null, [{channel, args}, userstate]);
};

const command = async ({channel, args}, userstate) =&gt; {
  const canExecute = await isAuthorized(channel, userstate.badges, MOD_MODULES);
  if(canExecute) await executeBuiltInCommand({channel, args}, userstate);
};

const vanish = async ({channel}, {username}) =&gt; {
  const canExecute = await isAuthorized(channel, username, USER_MODULES);
  if(canExecute) {
    client.timeout(channel, username, 1, &quot;VANISH&quot;)
      .catch((err) =&gt; {
      logger.error(err);
    });
  }
};



module.exports = {
  command,
  vanish,
};

This is how i execute the commands currently:
const executeBuiltIn = async ({channel, args}, userstate) =&gt; {
  await builtin[args[0]].apply(null, [{channel, args}, userstate]);
};
Separate modules
You didn’t specify a file name for the code in the first block- is it something like builtin.js?
The code in the first block should be divisible into two files- one for command with executeBuiltInCommand and MOD_MODULES, and the other for vanish with USER_MODULES. They could be combined to a single module to replace the current file in the first block - what I presume is builtin.js.
Simplify .apply() syntax
The ES6 feature spread syntax can be used to replace calls to .apply().
For example:

if(isSubArg) await builtInCmdHelpers[args[1]].apply(null, [{channel,
args}, userstate]);


can be simplified to:
if(isSubArg) await builtInCmdHelpers[args[1]](...[{channel, args}, userstate]);

However, since there is a fixed length on that array of arguments, the method can be called directly:
if(isSubArg) await builtInCmdHelpers[args[1]]({channel, args}, userstate);