Skip to content

Commit 9287bb7

Browse files
committed
refactor(CopBot): copbot class to use instance properties and remove duplicated logic`
1 parent 42eaf52 commit 9287bb7

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/bot/index.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export class CopBot {
1313
private static instance: CopBot;
1414
private _bot: Bot<Context>;
1515
private healthCheckInterval: NodeJS.Timeout | null = null;
16+
private isProduction = Config.environment === 'production';
17+
private webhookPath = `/bot/${Config.token}`;
18+
private webhookURL = `${Config.web_hook}${this.webhookPath}`;
19+
private mode = this.isProduction ? 'webhook' : 'long-polling';
1620
private constructor() {
1721
this._bot = new Bot<Context>(Config.token);
1822
}
@@ -42,21 +46,15 @@ export class CopBot {
4246
},
4347
});
4448
};
45-
46-
const isProduction = Config.environment === 'production';
47-
const webhookPath = `/bot/${Config.token}`;
48-
const webhookURL = `${Config.web_hook}${webhookPath}`;
49-
const mode = isProduction ? 'webhook' : 'long-polling';
50-
5149
logger.info(`Environment: ${Config.environment}`);
52-
logger.info(`Webhook URL: ${webhookURL}`);
53-
logger.info(`Running in ${mode} mode`);
50+
logger.info(`Webhook URL: ${this.webhookURL}`);
51+
logger.info(`Running in ${this.mode} mode`);
5452

55-
if (isProduction) {
53+
if (this.isProduction) {
5654
try {
5755
const app = express();
5856
app.use(express.json());
59-
app.post(webhookPath, async (req, res) => {
57+
app.post(this.webhookPath, async (req, res) => {
6058
res.sendStatus(200);
6159
try {
6260
await webhookCallback(this._bot, 'express')(req, res);
@@ -83,16 +81,16 @@ export class CopBot {
8381
const port = process.env.PORT || 3000;
8482
app.listen(port, async () => {
8583
logger.info(`Webhook server running on port ${port}`);
86-
await this.setupWebhook(webhookURL);
87-
await startBot(mode);
84+
await this.setupWebhook(this.webhookURL);
85+
await startBot(this.mode);
8886
});
8987
} catch (err: any) {
9088
console.error('Error setting up webhook:', err);
9189
process.exit(1);
9290
}
9391
} else {
9492
try {
95-
await startBot(mode);
93+
await startBot(this.mode);
9694
} catch (err: any) {
9795
console.error('Error during long-polling mode:', err);
9896
process.exit(1);
@@ -125,7 +123,12 @@ export class CopBot {
125123
logger.warn('Database health check failed. Attempting to reconnect...');
126124
await ServiceProvider.initialize();
127125
}
128-
logger.info('Bot is live');
126+
const webhookInfo = await this._bot.api.getWebhookInfo();
127+
if (!webhookInfo.url || webhookInfo.url !== this.webhookURL) {
128+
logger.warn('Webhook mismatch detected. Resetting webhook...');
129+
await this.setupWebhook(this.webhookURL);
130+
}
131+
logger.info('Health check passed: Bot is live');
129132
} catch (error: any) {
130133
logger.error(`Health check failed: ${error.message}`);
131134
}

0 commit comments

Comments
 (0)