Search code examples
javascriptdiscorderis

requiring code fails to work no matter what I try


I'm attempting to break down my Discord bot into different files to keep everything neat. I've created a test file and tested it out, and it either fails to load or doesn't at all. Please let me know if there's anything I should add / fix.

required code:

module.exports.stupid = (bot) => {

bot.on('messageCreate', (msg) => {
    if (msg.content === 'yes'){
        bot.createMessage(msg.channel.id,'yes!!')
    }
});

}

How I'm requiring it: var cm1 = require('./staff.js') -- I've tried const var & let

The require is placed after all of the npm libraries have been required.


Solution

  • Assuming the file require_me.js contains the following

    module.exports.someMethod = () => {
    
    }
    

    And you require the file from main.js.

    const myModule = require('./require_me.js');
    

    You need to call your method after requiring a module.

    myModule.someMethod();
    

    https://nodejs.org/api/modules.html#modules_module_exports