Skip to content

Commit 58c335d

Browse files
committed
refactor(webhook-service): "Refactor bot startup and webhook setup code"
1 parent 3639569 commit 58c335d

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/bot/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { GenerateCommand } from './handlers/GenerateCommands';
66
import { GeneralCommands } from './commands/genearl/GeneralCommands';
77
import { UserCommands } from './commands/user/UserCommands';
88
import { AdminCommands } from './commands/admin/AdminCommands';
9-
import * as http from 'http';
109
import { SaveUserData } from '../decorators/Database';
1110
import { MessageValidator } from '../decorators/Context';
1211
import { BotReply } from '../utils/chat/BotReply';
@@ -31,6 +30,13 @@ export class CopBot {
3130

3231
// Start the bot
3332
async start(): Promise<void> {
33+
const startBot = async () => {
34+
await this._bot.start({
35+
onStart: (botInfo) => {
36+
console.log(`Bot started in long-polling mode! Username: ${botInfo.username}`);
37+
},
38+
});
39+
};
3440
const isProduction = Config.environment === 'production';
3541
const webhookURL = `${Config.web_hook}/bot/${Config.token}`;
3642
console.log(`Environment: ${Config.environment}`);
@@ -39,6 +45,7 @@ export class CopBot {
3945
if (isProduction) {
4046
console.log('Setting webhook...');
4147
try {
48+
await startBot();
4249
console.log('Setting up webhook...');
4350
const _webhookService = new WebHookService(this._bot);
4451
await _webhookService.setupWebHook();
@@ -52,11 +59,7 @@ export class CopBot {
5259
console.log('Running in long-polling mode...');
5360
try {
5461
await this._bot.api.deleteWebhook();
55-
await this._bot.start({
56-
onStart: (botInfo) => {
57-
console.log(`Bot started in long-polling mode! Username: ${botInfo.username}`);
58-
},
59-
});
62+
await startBot();
6063
} catch (err) {
6164
console.error('Error in long-polling mode:', err);
6265
process.exit(1);

src/service/WebHook.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class WebHookService {
4242
}
4343
async setupWebHook() {
4444
try {
45+
await this._bot.api.deleteWebhook();
4546
const webhookInfo = await this._bot.api.getWebhookInfo();
4647
if (webhookInfo.url !== this._web_hook_url) {
4748
await this._bot.api.setWebhook(this._web_hook_url);

0 commit comments

Comments
 (0)