MacND / voluspa

A Discord bot for scheduling Destiny 2 raids.
https://voluspa.app
5 stars 0 forks source link

Update module passthrough #39

Closed MacND closed 5 years ago

MacND commented 5 years ago

Current behaviour

At present, custom modules (db, moment etc.) are required in main.js and attached to the client object. This means calls to those modules are prepended with client., why creates messy and confusing code.

Desired behaviour

Custom modules should be required in a standard fashion, and then passed to the events as arguments in the bind command e.g.

// bot.js
const customModule = require('/path/to/customModule.js');

fs.readdir(__basedir + '/events/', (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    const event = require(__basedir + `/events/${file}`)(customModule);
    let eventName = file.split('.')[0];
    client.on(eventName, event.bind(null, client));
  });
});
// events/message.js
module.exports = (client, message, customModule) => {
// ...
cmd.run(client, message, args, customModule)

Completion criteria

All command files will have their run function declaration adjusted. Old: run: async (client, message, args) => { New: run: async (client, message, args, db, moment) => {

MacND commented 5 years ago

I'm not sure how much confidence I have in this approach actually being better - from a code perspective it would probably be cleaner to import these utilities within message.js. I'll need to think on this.

At the very least I want this stuff out of the client object, even if that means using a utils object to house them.

MacND commented 5 years ago

I'm so sick of trying to find a "solution" to this - all commands are being refactored to require what they need, as they need it, and let caching do its job.

MacND commented 5 years ago

Resolved in https://github.com/MacND/voluspa/commit/8ed1c76740534f931a5f6d8110b0d9a5a95a12c5