question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

There is a way to separate commands over multiple classes?

See original GitHub issue

Hi,

I have created a Middleware called Menu which contains the following code:

`const TelegrafInlineMenu = require(‘telegraf-inline-menu’); const { BillingCommand } = require(‘./commands/billing’);

class Menu {

constructor(bot) {

    const menu = new TelegrafInlineMenu(ctx => `Hey ${ctx.from.first_name}!`);
    menu.setCommand('start');

    menu.use(new BillingCommand(menu)); // ? how to import other command?

    menu.simpleButton('Come Funziona', 'b', {
        doFunc: ctx => ctx.reply('As am I!')
    });

    bot.use(menu.init());
}

}

exports.Menu = Menu;`

I want organize the menu in different command middleware class, let’s take as example BillingCommand, how can I bound the BillingCommand to the actual menu? The code above isn’t working 'cause I get:

menu.use is not a function

the billing class contains this:

`const TelegrafInlineMenu = require(‘telegraf-inline-menu’);

class BillingCommand {

constructor(menu){
    menu.simpleButton('Crea abbonamento', 'a', {
        doFunc: ctx => ctx.reply('As am I!')
    });
}

}

exports.BillingCommand = BillingCommand;`

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
EdJoPaTocommented, Dec 7, 2019
module.exports = {menu};

is the short form of

module.exports = {
  menu: menu
};

Using an object rather than a direct export you are able to export multiple things at once. I never do for menus but I try to keep it the same everywhere.

Something like this is quite common for me:

function calcA() {…}
function calcB() {…}

module.exports = {
  calcA,
  calcB
}

which is also the short for

module.exports = {
  calcA: calcA,
  calcB: calcB
}

Personally I prefer TypeScript over JavaScript in this case too. In TypeScript you mark each of the to be exported things at the place where you write them:

export const menu = new TelegrafInlineMenu('…')
1reaction
EdJoPaTocommented, Dec 7, 2019

Using multiple files should work this way:

events.js

const menu = new TelegrafInlineMenu('events');
menu.setCommand('events');

menu.simpleButton(…);

module.exports = {menu};

main.js

const eventsMenu = require('./events');

const menu = new TelegrafInlineMenu('events');
menu.setCommand('start');

menu.submenu('Events', 'events', eventsMenu.menu);

bot.use(menu.init(…));

I hope it helps! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I split my Click commands, each with a set of sub ...
Is it possible to organize commands and their subcommands into separate classes? Here's an example of how I would like to separate it:...
Read more >
Bukkit for Beginners - Using Multiple Classes for Commands
Learn how to truly understand Java and Bukkit plugin coding from the beginning. If you're new to Java as well as Bukkit, then...
Read more >
Combine and Execute Multiple Linux Commands - Baeldung
In this tutorial, we'll see the different ways in which we can combine and execute multiple Linux commands efficiently.
Read more >
How to provide multiple statements on a single line in Python?
These statements can very well be written in one line by putting semicolon in between. a=10; b=20; c=1*b; print (c). A new block...
Read more >
Python Statements - Multiline, Simple, and Compound Examples
Python statements are the code instructions that are executed by the Python interpreter. Python executes statements one by one as they appear in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found