Skip to content

Commit 4b97907

Browse files
committed
refactor(bot): Refactor CopBot to use ServiceProvider for database initialization and add new admin commands
1 parent 5000d1e commit 4b97907

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { CopBot } from './bot';
2-
import { DatabaseService } from './database';
2+
import { ServiceProvider } from './service/database/ServiceProvider';
33
import logger from './utils/logger';
44
async function main() {
55
const cop = new CopBot();
6-
await new DatabaseService().initialize();
6+
await ServiceProvider.initialize();
77
logger.info('initialize Database');
88
await cop.initial();
99
logger.info('initial bot');

src/bot/index.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,64 @@
11
import { Bot } from 'grammy';
22
import type { Context } from 'grammy';
3-
import { Catch } from '../decorators/ErrorHandlingDecorator';
3+
import { Catch } from '../decorators/Catch';
44
import Config from '../config';
55
import { GenerateCommand } from './handlers/GenerateCommands';
66
import { GeneralCommands } from './commands/genearl/GeneralCommands';
77
import { ChatInfo } from '../utils/chat/ChatInfo';
88
import { UserCommands } from './commands/user/UserCommands';
99
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';
1013
export class CopBot {
1114
private _bot: Bot<Context>;
1215

1316
constructor() {
1417
this._bot = new Bot<Context>(Config.token);
1518
}
16-
start() {
19+
async start() {
20+
await this._bot.api.getUpdates({ offset: -1 });
1721
this._bot.start();
1822
}
1923
@Catch()
2024
async message(): Promise<void> {
2125
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+
}
2231
const chatinfo = new ChatInfo(ctx);
2332
const message = chatinfo.message();
2433
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
2644
if (entity?.type === 'bot_command' && message?.text) {
2745
const lowerCaseCommand = message.text.toLowerCase().replace('/', '');
28-
const commandHandler = (GeneralCommands as any)[lowerCaseCommand];
46+
const commandGeneral = (GeneralCommands as any)[lowerCaseCommand];
2947
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') {
3352
await commandUser(ctx);
53+
} else if (commandAdmin && typeof commandAdmin === 'function') {
54+
await commandAdmin(ctx);
3455
}
3556
}
3657
});
3758
}
38-
@Catch()
3959
async initial(): Promise<void> {
4060
new GenerateCommand(this._bot).generate();
41-
this.start();
61+
await this.start();
4262
await this.message();
4363
await this.channel();
4464
}

0 commit comments

Comments
 (0)