Skip to content

Commit 27d0b48

Browse files
committed
"Removed unnecessary code, simplified error handling, and refactored bot initialization and webhook setup."
1 parent f93de71 commit 27d0b48

2 files changed

Lines changed: 12 additions & 35 deletions

File tree

src/app.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@ async function main() {
1212
logger.info('Bot initialized');
1313
} catch (error: any) {
1414
logger.error('Error during initialization:', error);
15-
process.exit(1); // Exit if initialization fails
15+
process.exit(1);
1616
}
17-
18-
// Handle graceful shutdown on SIGTERM
1917
process.on('SIGTERM', async () => {
2018
logger.info('SIGTERM signal received. Closing bot...');
2119
try {
2220
const db = ServiceProvider.getInstance();
23-
await db.close(); // Ensure DB is closed before exiting
21+
await db.close();
2422
logger.info('Database closed.');
2523
} catch (error: any) {
2624
logger.error('Error during shutdown:', error);
@@ -29,24 +27,19 @@ async function main() {
2927
process.exit(0);
3028
}
3129
});
32-
33-
// Handle unhandled promise rejections
3430
process.on('unhandledRejection', (reason, promise) => {
3531
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
36-
// Consider gracefully shutting down the app here if necessary
3732
});
38-
39-
// Handle uncaught exceptions
4033
process.on('uncaughtException', async (error: any) => {
4134
logger.error('Uncaught Exception:', error);
4235
try {
4336
const db = ServiceProvider.getInstance();
44-
await db.close(); // Close DB before exiting on uncaught exception
37+
await db.close();
4538
logger.info('Database closed due to uncaught exception.');
4639
} catch (shutdownError: any) {
4740
logger.error('Error during shutdown:', shutdownError);
4841
} finally {
49-
process.exit(1); // Exit the process on uncaught exception
42+
process.exit(1);
5043
}
5144
});
5245
}

src/bot/index.ts

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,24 @@ export class CopBot {
3434
const server = http.createServer(async (req, res) => {
3535
if (req.method === 'POST' && req.url === '/webhook') {
3636
let body = '';
37-
38-
// Collect data chunks
3937
req.on('data', (chunk) => {
4038
body += chunk;
4139
});
42-
43-
// Handle incoming request once the data is complete
4440
req.on('end', async () => {
4541
try {
46-
// Parse the incoming JSON body
4742
const update = JSON.parse(body);
4843
if (!update) {
4944
console.error('Received empty body or malformed JSON.');
5045
return (res.statusCode = 400);
5146
}
5247

5348
console.log('Received webhook body:', update);
54-
await this._bot.handleUpdate(update); // Handle the update
55-
res.statusCode = 200; // Success response to Telegram
49+
await this._bot.handleUpdate(update);
50+
res.statusCode = 200;
5651
res.end();
5752
} catch (error: any) {
5853
console.error('Error parsing JSON in webhook request:', error.message || error.stack);
59-
res.statusCode = 500; // Internal Server Error
54+
res.statusCode = 500;
6055
res.end('Internal Server Error');
6156
}
6257
});
@@ -67,26 +62,15 @@ export class CopBot {
6762
res.end('Bad Request');
6863
});
6964
} else {
70-
res.statusCode = 404; // Not Found
65+
res.statusCode = 404;
7166
res.end();
7267
}
7368
});
7469
server.listen(port, '0.0.0.0', () => {
7570
console.log(`Bot started on port ${port}`);
7671
});
77-
try {
78-
await this._bot.api.setWebhook(`${web_hook}`);
79-
console.log(`Webhook set successfully to: ${web_hook}`);
80-
const webhookStatus = await this._bot.api.getWebhookInfo();
81-
if (webhookStatus.url) {
82-
console.log(`Webhook is already set to: ${webhookStatus.url}`);
83-
} else {
84-
console.log('No webhook set.');
85-
}
86-
} catch (error: any) {
87-
console.error('Error setting webhook:', error.message || error.stack);
88-
process.exit(1); // Exit if critical error
89-
}
72+
await this._bot.api.setWebhook(`${web_hook}`);
73+
console.log(`Webhook set successfully to: ${web_hook}`);
9074
} else {
9175
try {
9276
await this._bot.start({
@@ -96,7 +80,7 @@ export class CopBot {
9680
});
9781
} catch (error) {
9882
console.error('Error starting bot in long-polling mode:', error);
99-
process.exit(1); // Exit if the bot fails to start
83+
process.exit(1);
10084
}
10185
}
10286
}
@@ -179,9 +163,9 @@ export class CopBot {
179163
}
180164
@Catch()
181165
async initial(): Promise<void> {
166+
await this.start();
182167
new GenerateCommand(this._bot).generate();
183168
await this.message();
184-
await this.start();
185169
console.log('Bot Is Start');
186170
}
187171
}

0 commit comments

Comments
 (0)