|
1 | 1 | import { Bot } from 'grammy'; |
2 | 2 | import type { Context } from 'grammy'; |
3 | | -import { Catch } from '../decorators/ErrorHandlingDecorator'; |
| 3 | +import { Catch } from '../decorators/Catch'; |
4 | 4 | import Config from '../config'; |
5 | 5 | import { GenerateCommand } from './handlers/GenerateCommands'; |
6 | 6 | import { GeneralCommands } from './commands/genearl/GeneralCommands'; |
7 | 7 | import { ChatInfo } from '../utils/chat/ChatInfo'; |
8 | 8 | import { UserCommands } from './commands/user/UserCommands'; |
9 | 9 | import { forwardChannelPostToGroup } from './handlers/forward'; |
| 10 | +import { ServiceProvider } from '../service/database/ServiceProvider'; |
| 11 | +import { AdminCommands } from './commands/admin/AdminCommands'; |
| 12 | +import { BotReply } from '../utils/chat/BotReply'; |
10 | 13 | export class CopBot { |
11 | 14 | private _bot: Bot<Context>; |
12 | 15 |
|
13 | 16 | constructor() { |
14 | 17 | this._bot = new Bot<Context>(Config.token); |
15 | 18 | } |
16 | | - start() { |
| 19 | + async start() { |
| 20 | + await this._bot.api.getUpdates({ offset: -1 }); |
17 | 21 | this._bot.start(); |
18 | 22 | } |
19 | 23 | @Catch() |
20 | 24 | async message(): Promise<void> { |
21 | 25 | this._bot.on('message', async (ctx) => { |
| 26 | + const reply = new BotReply(ctx); |
| 27 | + if (!ctx.message) { |
| 28 | + await reply.textReply('No message context found. Unable to process the command.'); |
| 29 | + return; |
| 30 | + } |
22 | 31 | const chatinfo = new ChatInfo(ctx); |
23 | 32 | const message = chatinfo.message(); |
24 | 33 | const entity = chatinfo.entities(); |
25 | | - |
| 34 | + const datanaseService = ServiceProvider.getInstance(); |
| 35 | + // Save user and group info only for specific message types |
| 36 | + const [userService, groupService] = await Promise.all([datanaseService.getUserService(), datanaseService.getGroupService()]); |
| 37 | + const userData = { first_name: ctx!.from.first_name!, id: ctx.from.id!, username: ctx.from.username! }; |
| 38 | + await userService.save(userData); |
| 39 | + if (ctx.chat.type === 'group' || (ctx.chat.type === 'supergroup' && ctx.chat.id)) { |
| 40 | + await groupService.save(ctx); |
| 41 | + await groupService.updateMembers(ctx.chat!.id!, ctx.from?.id!); |
| 42 | + } |
| 43 | + // Only process valid commands |
26 | 44 | if (entity?.type === 'bot_command' && message?.text) { |
27 | 45 | const lowerCaseCommand = message.text.toLowerCase().replace('/', ''); |
28 | | - const commandHandler = (GeneralCommands as any)[lowerCaseCommand]; |
| 46 | + const commandGeneral = (GeneralCommands as any)[lowerCaseCommand]; |
29 | 47 | const commandUser = (UserCommands as any)[lowerCaseCommand]; |
30 | | - if (commandHandler && typeof commandHandler === 'function') { |
31 | | - await commandHandler(ctx); |
32 | | - } else if (commandHandler && typeof commandUser === 'function') { |
| 48 | + const commandAdmin = (AdminCommands as any)[lowerCaseCommand]; |
| 49 | + if (commandGeneral && typeof commandGeneral === 'function') { |
| 50 | + await commandGeneral(ctx); |
| 51 | + } else if (commandUser && typeof commandUser === 'function') { |
33 | 52 | await commandUser(ctx); |
| 53 | + } else if (commandAdmin && typeof commandAdmin === 'function') { |
| 54 | + await commandAdmin(ctx); |
34 | 55 | } |
35 | 56 | } |
36 | 57 | }); |
37 | 58 | } |
38 | | - @Catch() |
39 | 59 | async initial(): Promise<void> { |
40 | 60 | new GenerateCommand(this._bot).generate(); |
41 | | - this.start(); |
| 61 | + await this.start(); |
42 | 62 | await this.message(); |
43 | 63 | await this.channel(); |
44 | 64 | } |
|
0 commit comments