Skip to content

Commit 5da4f62

Browse files
committed
feat: Updated package versions, modified start script, added error handling, and refactored bot code
1 parent 7c71116 commit 5da4f62

6 files changed

Lines changed: 34 additions & 66 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"clean": "rm -rf dist",
2121
"main": "npm run build & node --env-file=.env dist/app.js",
2222
"start": "NODE_ENV=development node -r ts-node/register --env-file=.env src/app.ts",
23-
"start:deploy": "NODE_ENV=production node -r ts-node/register src/app.ts"
23+
"start:deploy": "npm run build && NODE_ENV=production node dist/app.js"
2424
},
2525
"repository": {
2626
"type": "git",

src/app.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,24 @@ async function main() {
88
await cop.initial();
99
logger.info('initial bot');
1010
process.on('SIGTERM', async () => {
11-
await db.close();
12-
process.exit(0);
11+
logger.info('SIGTERM signal received. Closing bot...');
12+
try {
13+
const db = ServiceProvider.getInstance();
14+
await db.close();
15+
logger.info('Database closed.');
16+
} catch (error:any) {
17+
logger.error('Error during shutdown:', error);
18+
} finally {
19+
process.exit(0);
20+
}
21+
});
22+
process.on('unhandledRejection', (reason, promise) => {
23+
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
24+
});
25+
26+
process.on('uncaughtException', (error) => {
27+
console.error('Uncaught Exception:', error);
28+
process.exit(1);
1329
});
1430
}
1531
main().catch((error) => console.error('Application error:', error));

src/bot/index.ts

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Bot } from 'grammy';
1+
import { Bot, webhookCallback } from 'grammy';
22
import type { Context } from 'grammy';
33
import { Catch } from '../decorators/Catch';
44
import Config from '../config';
@@ -26,7 +26,16 @@ export class CopBot {
2626
return CopBot.instance;
2727
}
2828
async start() {
29-
this._bot.start();
29+
try {
30+
await this._bot.start({
31+
onStart: (botInfo) => {
32+
console.log(`Bot started successfully! Username: ${botInfo.username}`);
33+
},
34+
});
35+
} catch (error) {
36+
console.error('Error starting the bot:', error);
37+
process.exit(1); // Exit the process if the bot fails to start
38+
}
3039
}
3140
@Catch()
3241
async message(): Promise<void> {
@@ -47,7 +56,7 @@ export class CopBot {
4756

4857
if (ctx.chat.type === 'group' || (ctx.chat.type === 'supergroup' && ctx.chat.id)) {
4958
await groupService.save(ctx);
50-
await groupService.updateMembers(ctx.chat!.id!, ctx.from?.id!,ctx);
59+
await groupService.updateMembers(ctx.chat!.id!, ctx.from?.id!, ctx);
5160
}
5261
// Only process valid commands
5362
if (entity?.type === 'bot_command') {
@@ -112,60 +121,3 @@ export class CopBot {
112121
await this.message();
113122
}
114123
}
115-
/* === Bot Command === */
116-
/**
117-
admin:
118-
- manage_approvals:
119-
- approve_user
120-
- disapprove_user
121-
- view_approved_list
122-
- manage_users:
123-
- ban_user
124-
- unban_user
125-
- warn_user
126-
- clear_user_warnings
127-
- view_user_warnings
128-
- mute_user
129-
- unmute_user
130-
- grant_admin
131-
- revoke_admin
132-
- manage_blacklist:
133-
- add_to_blacklist
134-
- remove_from_blacklist
135-
- view_blacklist
136-
- manage_rules:
137-
- add_rule
138-
- edit_rule
139-
- delete_rule
140-
- delete_last_rule
141-
- view_rules
142-
- manage_pinning:
143-
- pin_message
144-
- unpin_message
145-
- view_pinned_list
146-
- manage_messages:
147-
- purge_messages
148-
- group_settings:
149-
- lock_group
150-
- unlock_group
151-
- change_group_title
152-
- set_welcome_message
153-
- view_group_stats
154-
155-
user:
156-
- view_rules
157-
- codetime
158-
- upcoming_features
159-
- view_news
160-
- request_group_info
161-
- report_issue
162-
- cancel_report
163-
- request_invite_link
164-
- view_admin_list
165-
166-
general:
167-
- view_date
168-
- about_bot
169-
- view_support_contact
170-
- get_bot_info
171-
*/

src/decorators/Catch.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export function Catch(customResponse?: ErrorResponse) {
2929
if (ctx && typeof ctx.reply === 'function') {
3030
await ctx.reply(errorResponse.message);
3131
}
32-
throw error
3332
}
3433
};
3534

src/types/ResponseTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface DatabaseConfig {
55
password: string;
66
port: number;
77
url: string;
8+
serverUrl: string;
89
}
910

1011
export interface BotConfig {

0 commit comments

Comments
 (0)