Skip to content

Commit 93c32be

Browse files
committed
refactor(AdminCommands and ConnectionPool) return await expressions instead of awaiting them separately
1 parent 55e7138 commit 93c32be

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/bot/commands/admin/AdminCommands.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ export class AdminCommands {
3737
const { groupId, userId } = validationResult;
3838
const isApproved = await ApprovedService.updateApproved(groupId, userId);
3939
if (!isApproved) {
40-
await reply.textReply(`This user has already been approved.`);
40+
return await reply.textReply(`This user has already been approved.`);
4141
} else {
42-
await reply.textReply(
42+
return await reply.textReply(
4343
`The user with ID ${userId} has been successfully approved to join the group ${groupId}. You can view the list of approved users using /approvedlist, and manage disapproved users with /disapproved.`
4444
);
4545
}
@@ -63,9 +63,9 @@ export class AdminCommands {
6363
const { groupId, userId } = validationResult;
6464
const disapprovedUser = await ApprovedService.updateDisapproved(groupId, userId);
6565
if (!disapprovedUser) {
66-
await reply.textReply('This user is not currently approved, so they cannot be disapproved.');
66+
return await reply.textReply('This user is not currently approved, so they cannot be disapproved.');
6767
} else {
68-
await reply.textReply(`User with ID ${userId} has been successfully disapproved and removed from group ${groupId}.`);
68+
return await reply.textReply(`User with ID ${userId} has been successfully disapproved and removed from group ${groupId}.`);
6969
}
7070
}
7171

@@ -90,7 +90,7 @@ export class AdminCommands {
9090
const approvedListMessage = approvedUsers.map((user) => `• **ID:** ${user.telegram_id}\n **Name:** ${user.first_name}`).join('\n\n');
9191

9292
// Send a formatted message with the list of approved users
93-
await reply.markdownReply(`Here is the list of approved users in this group:\n\n${approvedListMessage}`);
93+
return await reply.markdownReply(`Here is the list of approved users in this group:\n\n${approvedListMessage}`);
9494
}
9595
/** Ban Commands */
9696
@RestrictToGroupChats()
@@ -158,9 +158,9 @@ export class AdminCommands {
158158
const { warningRemoved, warnings } = await WarnService.removeWarn(ctx);
159159

160160
if (warningRemoved) {
161-
await reply.textReply(`User ${user.first_name} now has ${warnings} warnings after the removal.`);
161+
return await reply.textReply(`User ${user.first_name} now has ${warnings} warnings after the removal.`);
162162
} else {
163-
await reply.textReply('User or group not found or no warnings to remove.');
163+
return await reply.textReply('User or group not found or no warnings to remove.');
164164
}
165165
}
166166
@RestrictToGroupChats()
@@ -190,7 +190,7 @@ export class AdminCommands {
190190
static async warnslist(ctx: Context) {
191191
const reply = new BotReply(ctx);
192192
const warns = await WarnService.getAllWarns(ctx);
193-
await reply.markdownReply(`${warns}`);
193+
return await reply.markdownReply(`${warns}`);
194194
}
195195
/** Mute Commands */
196196
@RestrictToGroupChats()
@@ -228,7 +228,7 @@ export class AdminCommands {
228228
static async grant(ctx: Context) {
229229
const reply = new BotReply(ctx);
230230
const grantUser = await AdminService.grant(ctx);
231-
await reply.textReply(grantUser);
231+
return await reply.textReply(grantUser);
232232
}
233233
@RestrictToGroupChats()
234234
@ReplyToBot()
@@ -240,7 +240,7 @@ export class AdminCommands {
240240
static async revoke(ctx: Context) {
241241
const reply = new BotReply(ctx);
242242
const revokeUser = await AdminService.revoke(ctx);
243-
await reply.textReply(revokeUser);
243+
return await reply.textReply(revokeUser);
244244
}
245245

246246
/** BlackList Command */
@@ -277,11 +277,10 @@ Group Type: ${group?.type || 'Unknown'}
277277

278278
// Send the group info along with the blacklist
279279
if (blackList.length === 0) {
280-
await ctx.api.sendMessage(userId, `${groupInfo}\nThe blacklist is currently empty.`);
280+
return await ctx.api.sendMessage(userId, `${groupInfo}\nThe blacklist is currently empty.`);
281281
} else {
282-
await ctx.api.sendMessage(userId, `${groupInfo}\nBlacklist:\n${blackList.join('\n')}`);
282+
return await ctx.api.sendMessage(userId, `${groupInfo}\nBlacklist:\n${blackList.join('\n')}`);
283283
}
284-
return;
285284
}
286285

287286
/** Add a Word to the Blacklist */
@@ -305,7 +304,7 @@ Group Type: ${group?.type || 'Unknown'}
305304
return;
306305
}
307306
await BlackListService.add(groupId, word, ctx);
308-
await reply.sendToTopic('Blacklist has been updated.', ctx.message?.reply_to_message?.message_thread_id!);
307+
return await reply.sendToTopic('Blacklist has been updated.', ctx.message?.reply_to_message?.message_thread_id!);
309308
}
310309

311310
/** Remove the Last Word from the Blacklist */
@@ -325,7 +324,7 @@ Group Type: ${group?.type || 'Unknown'}
325324
const groupId = ctx.chat?.id!;
326325
const wordToRemove = ctx.message?.text?.split(' ')[1];
327326
await BlackListService.remove(groupId, ctx, wordToRemove);
328-
await reply.sendToTopic('Blacklist has been updated.', ctx.message?.reply_to_message?.message_thread_id!);
327+
return await reply.sendToTopic('Blacklist has been updated.', ctx.message?.reply_to_message?.message_thread_id!);
329328
}
330329

331330
/** Clear the Entire Blacklist */
@@ -344,7 +343,7 @@ Group Type: ${group?.type || 'Unknown'}
344343
const reply = new BotReply(ctx);
345344
const groupId = ctx.chat?.id!;
346345
await BlackListService.clear(groupId, ctx);
347-
await reply.sendToTopic('The blacklist has been cleared.', ctx.message?.reply_to_message?.message_thread_id!);
346+
return await reply.sendToTopic('The blacklist has been cleared.', ctx.message?.reply_to_message?.message_thread_id!);
348347
}
349348
/** Pin Command */
350349
@RestrictToGroupChats()
@@ -371,7 +370,7 @@ Group Type: ${group?.type || 'Unknown'}
371370
const groupId = ctx.chat?.id!;
372371
const messageId = ctx.message?.reply_to_message?.message_id!;
373372
await ctx.api.unpinChatMessage(groupId, messageId);
374-
await reply.textReply('The pinned message has been unpinned.');
373+
return await reply.textReply('The pinned message has been unpinned.');
375374
}
376375
/** Purge Command */
377376
@RestrictToGroupChats()
@@ -417,7 +416,7 @@ Group Type: ${group?.type || 'Unknown'}
417416
};
418417

419418
await deleteMessagesInBatches(messagesToDelete);
420-
await reply.send('Deleting done.');
419+
return await reply.send('Deleting done.');
421420
}
422421
/** Group Setting Command */
423422
@RestrictToGroupChats()
@@ -459,6 +458,6 @@ Group Type: ${group?.type || 'Unknown'}
459458
}
460459

461460
// Default response if no valid action is detected
462-
await reply.textReply('Invalid usage of the /welcome command. Use "/welcome" to view the current message, "/welcome r" to remove it, or "/welcome [message]" to set a new welcome message.');
461+
return await reply.textReply('Invalid usage of the /welcome command. Use "/welcome" to view the current message, "/welcome r" to remove it, or "/welcome [message]" to set a new welcome message.');
463462
}
464463
}

src/database/ConnectionPool.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@ export class ConnectionPool {
2323
if (error.code === '3D000') {
2424
console.log(`Database does not exist. Creating database ${Config.database.databaseName}...`);
2525
await this.createDatabase();
26-
await this._pool.end(); // End the current pool connection
27-
const newConnectionString = this.getConnectionString();
28-
this._pool = this.initializePool(newConnectionString);
29-
console.log('Retrying connection after creating the database...');
26+
await this.reinitializePool();
3027
await this._pool.connect();
3128
} else {
3229
console.error('Unexpected error connecting to the database:', error);
@@ -72,9 +69,15 @@ export class ConnectionPool {
7269
return new Pool({
7370
connectionString,
7471
ssl: this._isProduction === 'production' ? { rejectUnauthorized: false } : false,
75-
connectionTimeoutMillis: 500,
76-
max: 10,
77-
idleTimeoutMillis: 500,
72+
connectionTimeoutMillis: 60000,
73+
max: 15,
74+
idleTimeoutMillis: 60000,
7875
});
7976
}
77+
private async reinitializePool() {
78+
await this._pool.end(); // Close old connections
79+
const newConnectionString = this.getConnectionString();
80+
this._pool = this.initializePool(newConnectionString);
81+
console.warn('Connection pool reinitialized.');
82+
}
8083
}

0 commit comments

Comments
 (0)