Skip to content

Commit ec45567

Browse files
committed
fixes
1 parent 7011ae3 commit ec45567

11 files changed

Lines changed: 207 additions & 121 deletions

File tree

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
aiohttp
12
emoji
23
future
34
# psycopg[binary]

tg_bot/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,14 @@ async def post_init(app: Application):
174174

175175

176176
# Build application with post_init callback
177-
application = ApplicationBuilder().token(TOKEN).post_init(post_init).build()
177+
# Configure concurrent update processing
178+
application = (
179+
ApplicationBuilder()
180+
.token(TOKEN)
181+
.post_init(post_init)
182+
.concurrent_updates(True) # Enable concurrent update processing
183+
.build()
184+
)
178185

179186

180187
# For backward compatibility, create updater-like object

tg_bot/modules/afk.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional
22

33
from telegram import Message, MessageEntity, Update, User
4+
from telegram.error import BadRequest
45
from telegram.ext import ContextTypes, MessageHandler, filters
56

67
from tg_bot import dispatcher
@@ -57,10 +58,12 @@ async def reply_afk(update: Update, context: ContextTypes.DEFAULT_TYPE):
5758
message.text[ent.offset : ent.offset + ent.length]
5859
)
5960
if not user_id:
60-
# Should never happen, since for a user to become AFK they must have spoken. Maybe changed username?
6161
return
62-
chat = await bot.get_chat(user_id)
63-
fst_name = chat.first_name
62+
try:
63+
chat = await bot.get_chat(user_id)
64+
fst_name = chat.first_name
65+
except BadRequest:
66+
return
6467

6568
else:
6669
return

tg_bot/modules/blacklist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ async def del_blacklist(update: Update, context: ContextTypes.DEFAULT_TYPE):
147147
try:
148148
await message.delete()
149149
except BadRequest as excp:
150-
if excp.message == "Message to delete not found":
150+
if excp.message in ("Message to delete not found", "Message can't be deleted"):
151151
pass
152152
else:
153153
LOGGER.exception("Error while deleting blacklist message.")

tg_bot/modules/cust_filters.py

Lines changed: 85 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
User,
1111
)
1212
from telegram.constants import MessageLimit
13-
from telegram.error import BadRequest
13+
from telegram.error import BadRequest, TimedOut, NetworkError, TelegramError
1414
from telegram.ext import (
1515
CallbackQueryHandler,
1616
CommandHandler,
@@ -322,9 +322,14 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
322322
)
323323
except BadRequest as excp:
324324
if excp.message in ("Message to be replied not found", "Replied message not found"):
325-
await bot.send_sticker(chat.id, filt.reply, reply_markup=keyboard)
325+
try:
326+
await bot.send_sticker(chat.id, filt.reply, reply_markup=keyboard)
327+
except (TimedOut, NetworkError):
328+
LOGGER.warning("Timeout/Network error sending sticker filter in chat %s", str(chat.id))
326329
else:
327330
raise
331+
except (TimedOut, NetworkError):
332+
LOGGER.warning("Timeout/Network error replying with sticker filter in chat %s", str(chat.id))
328333
elif filt.is_document:
329334
try:
330335
await reply_msg.reply_document(
@@ -336,15 +341,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
336341
)
337342
except BadRequest as excp:
338343
if excp.message in ("Message to be replied not found", "Replied message not found"):
339-
await bot.send_document(
340-
chat.id,
341-
filt.reply,
342-
caption=media_caption,
343-
parse_mode="Markdown",
344-
reply_markup=keyboard,
345-
)
344+
try:
345+
await bot.send_document(
346+
chat.id,
347+
filt.reply,
348+
caption=media_caption,
349+
parse_mode="Markdown",
350+
reply_markup=keyboard,
351+
)
352+
except (TimedOut, NetworkError):
353+
LOGGER.warning("Timeout/Network error sending document filter in chat %s", str(chat.id))
346354
else:
347355
raise
356+
except (TimedOut, NetworkError):
357+
LOGGER.warning("Timeout/Network error replying with document filter in chat %s", str(chat.id))
348358
elif filt.is_image:
349359
try:
350360
await reply_msg.reply_photo(
@@ -356,15 +366,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
356366
)
357367
except BadRequest as excp:
358368
if excp.message in ("Message to be replied not found", "Replied message not found"):
359-
await bot.send_photo(
360-
chat.id,
361-
filt.reply,
362-
caption=media_caption,
363-
parse_mode="Markdown",
364-
reply_markup=keyboard,
365-
)
369+
try:
370+
await bot.send_photo(
371+
chat.id,
372+
filt.reply,
373+
caption=media_caption,
374+
parse_mode="Markdown",
375+
reply_markup=keyboard,
376+
)
377+
except (TimedOut, NetworkError):
378+
LOGGER.warning("Timeout/Network error sending photo filter in chat %s", str(chat.id))
366379
else:
367380
raise
381+
except (TimedOut, NetworkError):
382+
LOGGER.warning("Timeout/Network error replying with photo filter in chat %s", str(chat.id))
368383
elif filt.is_audio:
369384
try:
370385
await reply_msg.reply_audio(
@@ -376,15 +391,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
376391
)
377392
except BadRequest as excp:
378393
if excp.message in ("Message to be replied not found", "Replied message not found"):
379-
await bot.send_audio(
380-
chat.id,
381-
filt.reply,
382-
caption=media_caption,
383-
parse_mode="Markdown",
384-
reply_markup=keyboard,
385-
)
394+
try:
395+
await bot.send_audio(
396+
chat.id,
397+
filt.reply,
398+
caption=media_caption,
399+
parse_mode="Markdown",
400+
reply_markup=keyboard,
401+
)
402+
except (TimedOut, NetworkError):
403+
LOGGER.warning("Timeout/Network error sending audio filter in chat %s", str(chat.id))
386404
else:
387405
raise
406+
except (TimedOut, NetworkError):
407+
LOGGER.warning("Timeout/Network error replying with audio filter in chat %s", str(chat.id))
388408
elif filt.is_voice:
389409
try:
390410
await reply_msg.reply_voice(
@@ -396,15 +416,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
396416
)
397417
except BadRequest as excp:
398418
if excp.message in ("Message to be replied not found", "Replied message not found"):
399-
await bot.send_voice(
400-
chat.id,
401-
filt.reply,
402-
caption=media_caption,
403-
parse_mode="Markdown",
404-
reply_markup=keyboard,
405-
)
419+
try:
420+
await bot.send_voice(
421+
chat.id,
422+
filt.reply,
423+
caption=media_caption,
424+
parse_mode="Markdown",
425+
reply_markup=keyboard,
426+
)
427+
except (TimedOut, NetworkError):
428+
LOGGER.warning("Timeout/Network error sending voice filter in chat %s", str(chat.id))
406429
else:
407430
raise
431+
except (TimedOut, NetworkError):
432+
LOGGER.warning("Timeout/Network error replying with voice filter in chat %s", str(chat.id))
408433
elif filt.is_video:
409434
try:
410435
await reply_msg.reply_video(
@@ -416,15 +441,20 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
416441
)
417442
except BadRequest as excp:
418443
if excp.message in ("Message to be replied not found", "Replied message not found"):
419-
await bot.send_video(
420-
chat.id,
421-
filt.reply,
422-
caption=media_caption,
423-
parse_mode="Markdown",
424-
reply_markup=keyboard,
425-
)
444+
try:
445+
await bot.send_video(
446+
chat.id,
447+
filt.reply,
448+
caption=media_caption,
449+
parse_mode="Markdown",
450+
reply_markup=keyboard,
451+
)
452+
except (TimedOut, NetworkError):
453+
LOGGER.warning("Timeout/Network error sending video filter in chat %s", str(chat.id))
426454
else:
427455
raise
456+
except (TimedOut, NetworkError):
457+
LOGGER.warning("Timeout/Network error replying with video filter in chat %s", str(chat.id))
428458

429459
elif filt.has_markdown:
430460
keyb = build_keyboard(buttons)
@@ -449,13 +479,16 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
449479
"again, or ask in @ELSupport for help."
450480
)
451481
elif excp.message in ("Message to be replied not found", "Replied message not found"):
452-
await bot.send_message(
453-
chat.id,
454-
filt.reply,
455-
parse_mode="Markdown",
456-
link_preview_options={"is_disabled": should_preview_disabled},
457-
reply_markup=keyboard,
458-
)
482+
try:
483+
await bot.send_message(
484+
chat.id,
485+
filt.reply,
486+
parse_mode="Markdown",
487+
link_preview_options={"is_disabled": should_preview_disabled},
488+
reply_markup=keyboard,
489+
)
490+
except (TimedOut, NetworkError):
491+
LOGGER.warning("Timeout/Network error sending markdown filter in chat %s", str(chat.id))
459492
else:
460493
await reply_msg.reply_text(
461494
"This note could not be sent, as it is incorrectly formatted. Ask in "
@@ -469,15 +502,22 @@ async def reply_filter(update: Update, context: ContextTypes.DEFAULT_TYPE):
469502
str(filt.keyword),
470503
str(chat.id),
471504
)
505+
except (TimedOut, NetworkError):
506+
LOGGER.warning("Timeout/Network error replying with markdown filter in chat %s", str(chat.id))
472507

473508
else:
474509
try:
475510
await reply_msg.reply_text(filt.reply)
476511
except BadRequest as excp:
477512
if excp.message in ("Message to be replied not found", "Replied message not found"):
478-
await bot.send_message(chat.id, filt.reply)
513+
try:
514+
await bot.send_message(chat.id, filt.reply)
515+
except (TimedOut, NetworkError):
516+
LOGGER.warning("Timeout/Network error sending text filter in chat %s", str(chat.id))
479517
else:
480518
raise
519+
except (TimedOut, NetworkError):
520+
LOGGER.warning("Timeout/Network error replying with text filter in chat %s", str(chat.id))
481521
break
482522

483523

tg_bot/modules/dbcleanup.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from time import sleep
1+
import asyncio
22

33
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
44
from telegram.error import BadRequest, Forbidden
@@ -16,8 +16,12 @@ async def get_invalid_chats(bot, update: Update, remove: bool = False):
1616
chat_list = []
1717
progress_message = None
1818

19-
for chat in chats:
20-
if ((100 * chats.index(chat)) / len(chats)) > progress:
19+
for idx, chat in enumerate(chats):
20+
# Yield control to event loop frequently to allow other commands to process
21+
if idx % 10 == 0:
22+
await asyncio.sleep(0)
23+
24+
if ((100 * idx) / len(chats)) > progress:
2125
progress_bar = f"{progress}% completed in getting invalid chats."
2226
if progress_message:
2327
try:
@@ -31,7 +35,7 @@ async def get_invalid_chats(bot, update: Update, remove: bool = False):
3135
progress += 5
3236

3337
cid = chat.chat_id
34-
sleep(0.1)
38+
await asyncio.sleep(0.1)
3539
try:
3640
await bot.get_chat(cid)
3741
except (BadRequest, Forbidden):
@@ -49,8 +53,12 @@ async def get_invalid_chats(bot, update: Update, remove: bool = False):
4953
if not remove:
5054
return kicked_chats
5155
else:
52-
for muted_chat in chat_list:
53-
sleep(0.1)
56+
for idx, muted_chat in enumerate(chat_list):
57+
# Yield control to event loop frequently to allow other commands to process
58+
if idx % 10 == 0:
59+
await asyncio.sleep(0)
60+
61+
await asyncio.sleep(0.1)
5462
user_sql.rem_chat(muted_chat)
5563
return kicked_chats
5664

@@ -60,9 +68,13 @@ async def get_invalid_gban(bot, update: Update, remove: bool = False):
6068
ungbanned_users = 0
6169
ungban_list = []
6270

63-
for user in banned:
71+
for idx, user in enumerate(banned):
72+
# Yield control to event loop frequently to allow other commands to process
73+
if idx % 10 == 0:
74+
await asyncio.sleep(0)
75+
6476
user_id = user["user_id"]
65-
sleep(0.1)
77+
await asyncio.sleep(0.1)
6678
try:
6779
await bot.get_chat(user_id)
6880
except BadRequest:
@@ -74,8 +86,12 @@ async def get_invalid_gban(bot, update: Update, remove: bool = False):
7486
if not remove:
7587
return ungbanned_users
7688
else:
77-
for user_id in ungban_list:
78-
sleep(0.1)
89+
for idx, user_id in enumerate(ungban_list):
90+
# Yield control to event loop frequently to allow other commands to process
91+
if idx % 10 == 0:
92+
await asyncio.sleep(0)
93+
94+
await asyncio.sleep(0.1)
7995
gban_sql.ungban_user(user_id)
8096
return ungbanned_users
8197

@@ -107,8 +123,12 @@ async def get_muted_chats(bot, update: Update, leave: bool = False):
107123
chat_list = []
108124
progress_message = None
109125

110-
for chat in chats:
111-
if ((100 * chats.index(chat)) / len(chats)) > progress:
126+
for idx, chat in enumerate(chats):
127+
# Yield control to event loop frequently to allow other commands to process
128+
if idx % 10 == 0:
129+
await asyncio.sleep(0)
130+
131+
if ((100 * idx) / len(chats)) > progress:
112132
progress_bar = f"{progress}% completed in getting muted chats."
113133
if progress_message:
114134
try:
@@ -122,7 +142,7 @@ async def get_muted_chats(bot, update: Update, leave: bool = False):
122142
progress += 5
123143

124144
cid = chat.chat_id
125-
sleep(0.1)
145+
await asyncio.sleep(0.1)
126146

127147
try:
128148
await bot.send_chat_action(cid, "typing")
@@ -141,8 +161,12 @@ async def get_muted_chats(bot, update: Update, leave: bool = False):
141161
if not leave:
142162
return muted_chats
143163
else:
144-
for muted_chat in chat_list:
145-
sleep(0.1)
164+
for idx, muted_chat in enumerate(chat_list):
165+
# Yield control to event loop frequently to allow other commands to process
166+
if idx % 10 == 0:
167+
await asyncio.sleep(0)
168+
169+
await asyncio.sleep(0.1)
146170
try:
147171
await bot.leave_chat(muted_chat)
148172
except:

0 commit comments

Comments
 (0)