Skip to content

Commit 8f966b9

Browse files
committed
feat(bot): add bot command handling and forward channel posts
- Implemented message handling for bot commands using decorators and command handlers. - Integrated `GeneralCommands` and `UserCommands` for processing different command types. - Added functionality for forwarding channel posts to the group. - Enhanced error handling with `Catch` decorator for asynchronous methods. - Initialized bot and started message and channel handlers.
1 parent e1a51b0 commit 8f966b9

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

src/bot/index.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Bot } from 'grammy';
2+
import type { Context } from 'grammy';
3+
import { Catch } from '../decorators/ErrorHandlingDecorator';
4+
import Config from '../config';
5+
import { GenerateCommand } from './handlers/GenerateCommands';
6+
import { GeneralCommands } from './commands/genearl/GeneralCommands';
7+
import { ChatInfo } from '../utils/chat/ChatInfo';
8+
import { UserCommands } from './commands/user/UserCommands';
9+
import { forwardChannelPostToGroup } from './handlers/forward';
10+
export class CopBot {
11+
private _bot: Bot<Context>;
12+
13+
constructor() {
14+
this._bot = new Bot<Context>(Config.token);
15+
}
16+
start() {
17+
this._bot.start();
18+
}
19+
@Catch()
20+
async message(): Promise<void> {
21+
this._bot.on('message', async (ctx) => {
22+
const chatinfo = new ChatInfo(ctx);
23+
const message = chatinfo.message();
24+
const entity = chatinfo.entities();
25+
26+
if (entity?.type === 'bot_command' && message?.text) {
27+
const lowerCaseCommand = message.text.toLowerCase().replace('/', '');
28+
const commandHandler = (GeneralCommands as any)[lowerCaseCommand];
29+
const commandUser = (UserCommands as any)[lowerCaseCommand];
30+
if (commandHandler && typeof commandHandler === 'function') {
31+
await commandHandler(ctx);
32+
} else if (commandHandler && typeof commandUser === 'function') {
33+
await commandUser(ctx);
34+
}
35+
}
36+
});
37+
}
38+
@Catch()
39+
async initial(): Promise<void> {
40+
new GenerateCommand(this._bot).generate();
41+
this.start();
42+
await this.message();
43+
await this.channel();
44+
}
45+
@Catch()
46+
async channel(): Promise<void> {
47+
this._bot.on('channel_post', async (ctx) => {
48+
forwardChannelPostToGroup(ctx);
49+
});
50+
}
51+
}

0 commit comments

Comments
 (0)