grammyjs / commands

Work-in-progress plugin for managing commands with grammY.
https://grammy.dev
MIT License
12 stars 2 forks source link

Support for Exporting and Importing Commands #38

Closed nicetomytyuk closed 4 days ago

nicetomytyuk commented 3 months ago

It would be cool if the plugin supported exporting and importing commands. This feature would enhance the modularity and reusability of command definitions, making it easier to manage and maintain large bot projects.

Something like this:

1. Exporting a Command:

import { Command } from "@grammyjs/commands";

const startCommand = new Command("start", "Initializes bot configuration")
    .addToScope(
        { type: "all_group_chats" },
        (ctx) => ctx.reply(`Hello, members of ${ctx.chat.title}!`),
    );

export default startCommand;

2. Importing a Command

import { Bot } from "grammy";
import { Commands } from "@grammyjs/commands";
import startCommand from "./commands/start.js";

const bot = new Bot("<telegram token>");

const myCommands = new Commands();

myCommands.define(startCommand);

// Calls `setMyCommands`
await myCommands.setCommands(bot);

// Registers the command handlers
bot.use(myCommands);

bot.start();

*Here maybe the .define could support both single Command or an array of Command (?)

Benefits:

carafelix commented 3 months ago

I have a local working branch with the needed changes to support this. I'll pr maybe in the weekend

carafelix commented 4 days ago

marked as completed from merging #51