Skip to content

Commit 5000d1e

Browse files
committed
feat(bot): implement command registration system with GenerateCommand class
- Introduce `GenerateCommand` class: - Provides a unified mechanism to register bot commands dynamically. - Leverages `grammy` bot framework for command registration. - Add `registerCommand` method: - Dynamically binds and registers command handlers from `GeneralCommands` and `UserCommands`. - Safely checks for the existence of a command handler before registration. - Uses the `bind` method to ensure the proper context (`this`) for command handlers. - Add `generate` method: - Iterates over all command names defined in the `COMMANDS` array. - Registers each command by delegating to the `registerCommand` method.
1 parent 34913bd commit 5000d1e

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Bot } from 'grammy';
2+
import { CommandName, COMMANDS } from '../../utils';
3+
import { GeneralCommands } from '../../bot/commands/genearl/GeneralCommands';
4+
import { UserCommands } from '../commands/user/UserCommands';
5+
export class GenerateCommand {
6+
private bot: Bot;
7+
constructor(bot: Bot) {
8+
this.bot = bot;
9+
}
10+
11+
private registerCommand(name: CommandName) {
12+
// Safely access the command handler on GeneralCommands
13+
const generalCommandHandler = (GeneralCommands as any)[name];
14+
const userCommandHandler = (UserCommands as any)[name];
15+
if (generalCommandHandler) {
16+
this.bot.command(name, generalCommandHandler.bind(GeneralCommands));
17+
} else if (userCommandHandler) {
18+
this.bot.command(name, userCommandHandler.bind(UserCommands));
19+
}
20+
}
21+
22+
/**
23+
* Registers all commands defined in COMMANDS.
24+
*/
25+
generate() {
26+
COMMANDS.forEach((command) => this.registerCommand(command));
27+
}
28+
}

0 commit comments

Comments
 (0)