munkee
munkee

Reputation: 769

Node module.exports pattern - Is this correct?

I'm new to JS and I am currently working on an IRC bot. I have produced the following module which I want to use for creating bot commands.

 /***
 * Module contains available bot commands including
 * access levels to view/utilise command and help text.
 * 
 * Usage example: commands.tg.play(payload);
 * 
***/
module.exports = commands = { 
  tg: {
    reg: '^[. - !]tg',
    help: 'some help text for tg',
    play: function(payload){tg(payload);}
  },
  help: {
    reg: '^[. - !]help',
    description: 'some help text for intel',
    play: function(payload){help(payload);}
  }

};

function tg(payload){
//Example: msg_route: pm msg_from: munkee msg_data: .tg munkee testing message via tg command msg_match: .tg msg_method: .          
  console.log("msg_route:" + payload.msg_route + ' msg_from: ' + payload.msg_from + ' msg_data: ' + payload.msg_data + ' msg_match: ' + payload.msg_match + ' msg_method: ' + payload.msg_method);
  return;
}

function help(payload){
var output='Available commands: ';
  for (var i in commands){
    output=output+ ' ' + i.toString();
  }
 return console.log(output);
}

As you can see I am currently defining some methods within my commands object. In order to try and keep things a bit cleaner I define the functions to actually be run below the commands object. I can access these easily via commands.help.play(payload). However I wanted to know whether there is a better way to do this or is the direction I am going correct? At the moment the commands are very skeleton and will be carrying out quite a bit more work but I just wanted to post something to give the general idea.

Upvotes: 2

Views: 106

Answers (2)

A.B
A.B

Reputation: 20445

It is about my personal prefrence but i will go for this one. Singleton pattern with Constructor

 function Commands(){
    this.commands = {};
    this.tg = function (payload){
        //Example: msg_route: pm msg_from: munkee msg_data: .tg munkee testing message via tg command msg_match: .tg msg_method: .          
          console.log("msg_route:" + payload.msg_route + ' msg_from: ' + payload.msg_from + ' msg_data: ' + payload.msg_data + ' msg_match: ' + payload.msg_match + ' msg_method: ' + payload.msg_method);
          return;
        };



this.help = function (payload){
    var output='Available commands: ';
      for (var i in commands){
        output=output+ ' ' + i.toString();
      }
}
  this.commands.tg= {
    reg: '^[. - !]tg',
    help: 'some help text for tg',
    play: this.tg
  };
  this.commands.help= {
    reg: '^[. - !]help',
    description: 'some help text for intel',
    play: this.help
   };


}


if(!!obj)
obj = new Commands();

module.exports = obj;

Upvotes: 1

Lucio M. Tato
Lucio M. Tato

Reputation: 5805

I don't like the extra fn call you're making: play:function(payload){tg(payload);} should be just play:tg since functions are first-order citizens in js. I do also prefer to assign to module.exports at the end of the file.

/***
 * Module contains available bot commands including
 * access levels to view/utilise command and help text.
 * 
 * Usage example: commands.tg.play(payload);
 * 
***/

var commands = {};

function tg(payload){
//Example: msg_route: pm msg_from: munkee msg_data: .tg munkee testing message via tg command msg_match: .tg msg_method: .          
  console.log("msg_route:" + payload.msg_route + ' msg_from: ' + payload.msg_from + ' msg_data: ' + payload.msg_data + ' msg_match: ' + payload.msg_match + ' msg_method: ' + payload.msg_method);
  return;
}

function help(payload){
var output='Available commands: ';
  for (var i in commands){
    output=output+ ' ' + i.toString();
  }
 return console.log(output);
}


// export
module.exports = commands = { 
  tg: {
    reg: '^[. - !]tg',
    help: 'some help text for tg',
    play: tg;
  },
  help: {
    reg: '^[. - !]help',
    description: 'some help text for intel',
    play: help}
  }

};

Upvotes: 1

Related Questions