0

I am unable to use the same instance of an object in another java-script file using nodejs.

I'm working on a bot for telegram. Because the file gets large and chaotic, i would like to split the functions of my bot into a few extra js files. But i don't know any way how to share the same instance of an object between multiple javascript files.

///////////////////////8Ball File
const {eightBall} = require("./main");
const ballBot = myAwseomeBot;

function eightBall() {

    ballBot.onText(/\/8ball/, (msg, callback) => {
        let ranNum = Math.floor(Math.random() * 15) + 1;
        const chatId = msg.chat.id;
        const reply_to_message_id = msg.message_id;
        console.log(ranNum);
        switch (ranNum) {
            case 1:
                ballBot.sendMessage(chatId, "Ja");
                break;
      }
    })
}


//main file

let myAwesomeBot  = new TelegramBot(botToken, {polling:true});
exports.myAwesomeBot = myAwesomeBot;










ballBot.onText(/\/8ball/, (msg, callback) => {
        ^
TypeError: Cannot read property 'onText' of undefined
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Drake
  • 33
  • 6
  • 2
    Cyclic import dependency. A requires B, B requires A, your application is poorly designed – tehhowch Jul 07 '19 at 21:32
  • You are right, I'm still new object oriented programming. But how should i design it then ? I mean of course eightball.js needs main.js and vice versa. How should i do it your suggestion ? Write everything into one big file ? – Drake Jul 07 '19 at 21:39
  • No, eightball doesn't need main. It should implement a handler, and main should attach that handler to whatever event it needs – tehhowch Jul 08 '19 at 00:05

3 Answers3

1

Did you check that ballBot was defined? Try to remove the brackets when requiring the main file. I would also suggest using the Singleton pattern if you want to share the same instance across your code.

Tomer
  • 119
  • 1
  • 8
1

It isn't shown in your code here, but you probably have a cyclic dependency, where A requires B, and B requires A.

The simplest solution relevant to your use case is to define and implement commands for your bot in additional files, and let your bot file attach / consume them:

8ball.js

import { telegram stuff } from 'wherever';

export myCommand1 = {
  pattern: /\/8ball/,
  eventName: 'ontext',
  callback: (msg, msgCallback) => { /* use "this" as if it were the bot instance */};
};

main.js

import .... from ....;
import { myCommand1 } from '8ball';

...
bot.onText(myCommand1.pattern, myCommand1.callback.bind(bot));
...

There are probably other bot class methods more suited for attaching generic event handlers/listeners, and also other methods of specifying your module exports, but the idea is that your command files don't need to import the bot file. I have not researched the telegram bot API so it may have some manner of delegating the bot instance when attaching an event handler. If so, use it!

tehhowch
  • 9,645
  • 4
  • 24
  • 42
0

Could it be that there is a typo on line 2? Should be myAwesomeBot not myAwseomeBot.

const ballBot = myAwseomeBot;

skitzo
  • 1
  • 3
  • Thanks. Good Eye. But no the typo is only because i changed the name here in stack overflow to something more generic. – Drake Jul 07 '19 at 21:28