7
\$\begingroup\$

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 "command.js", "vanish.js" 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 "modules".

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) => {
  const isSubArg = await isCommandSubArg(args[1]);
  if(isSubArg) await builtInCmdHelpers[args[1]].apply(null, [{channel, args}, userstate]);
};

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

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



module.exports = {
  command,
  vanish,
};

This is how i execute the commands currently:

const executeBuiltIn = async ({channel, args}, userstate) => {
  await builtin[args[0]].apply(null, [{channel, args}, userstate]);
};
\$\endgroup\$
2
  • \$\begingroup\$ Do you mean that you want to put each function in a separate file? E.g. one for add, one for remove , one for edit, etc.? \$\endgroup\$ Nov 3 '20 at 13:02
  • \$\begingroup\$ i mean a custom file for "command" and "vanish" i thought of using a index file where i would import the files command.js and vanish.js but when i did that it did not work. I moved add, remove, edit and show already to an own file. \$\endgroup\$
    – roboJS
    Nov 3 '20 at 13:08
1
\$\begingroup\$

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);
\$\endgroup\$
2
  • \$\begingroup\$ I'm not sure .apply()/spread needed to be used in the first place. Couldn't it just be builtInCmdHelpers[args[1]]({channel, args}, userstate)? \$\endgroup\$ Jan 16 at 4:35
  • \$\begingroup\$ Yes- that seems to be sufficient given that the array of args is not very variadic. Thanks for pointing that out! \$\endgroup\$ Jan 20 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.