11import datetime
2- from typing import Any , Dict , Tuple , Union , cast
2+ from typing import Tuple , Union , cast
33
44from telegram import (
55 CallbackQuery ,
@@ -30,19 +30,14 @@ def get_dtm_str() -> str:
3030async def approve_user (
3131 user : Union [int , User ], chat_id : int , group_name : str , context : ContextTypes .DEFAULT_TYPE
3232) -> None :
33- user_data = cast (Dict [Any , Any ], context .user_data )
3433 try :
3534 if isinstance (user , User ):
3635 await user .approve_join_request (chat_id = chat_id )
3736 else :
3837 await context .bot .approve_chat_join_request (user_id = user , chat_id = chat_id )
39- user_data .setdefault (int (chat_id ), {}).setdefault ("approved" , []).append (get_dtm_str ())
4038 except BadRequest as exc :
4139 user_mention = f"{ user .username } - { user .id } " if isinstance (user , User ) else str (user )
4240 error_message = f"{ exc } - { user_mention } - { group_name } "
43- user_data .setdefault (int (chat_id ), {}).setdefault ("approve failed" , []).append (
44- f"{ get_dtm_str ()} : { exc } "
45- )
4641 raise BadRequest (error_message ) from exc
4742 except Forbidden as exc :
4843 if "user is deactivated" not in exc .message :
@@ -52,19 +47,14 @@ async def approve_user(
5247async def decline_user (
5348 user : Union [int , User ], chat_id : int , group_name : str , context : ContextTypes .DEFAULT_TYPE
5449) -> None :
55- user_data = cast (Dict [Any , Any ], context .user_data )
5650 try :
5751 if isinstance (user , User ):
5852 await user .decline_join_request (chat_id = chat_id )
5953 else :
6054 await context .bot .decline_chat_join_request (user_id = user , chat_id = chat_id )
61- user_data .setdefault (int (chat_id ), {}).setdefault ("declined" , []).append (get_dtm_str ())
6255 except BadRequest as exc :
6356 user_mention = f"{ user .username } - { user .id } " if isinstance (user , User ) else str (user )
6457 error_message = f"{ exc } - { user_mention } - { group_name } "
65- user_data .setdefault (int (chat_id ), {}).setdefault ("declined failed" , []).append (
66- f"{ get_dtm_str ()} : { exc } "
67- )
6858 raise BadRequest (error_message ) from exc
6959 except Forbidden as exc :
7060 if "user is deactivated" not in exc .message :
@@ -81,16 +71,9 @@ async def join_request_callback(update: Update, context: ContextTypes.DEFAULT_TY
8171 # No need to ping the user again if we already did
8272 return
8373
84- user_data = cast (Dict [Any , Any ], context .user_data )
8574 on_topic = join_request .chat .username == ONTOPIC_USERNAME
8675 group_mention = ONTOPIC_CHAT_ID if on_topic else OFFTOPIC_CHAT_ID
8776
88- user_data .setdefault (int (chat_id ), {}).setdefault ("received join request" , []).append (
89- get_dtm_str ()
90- )
91- user_data [int (chat_id )]["user_chat_id" ] = user_chat_id
92- user_data [int (chat_id )]["user_id" ] = user .id
93-
9477 text = (
9578 f"Hi, { user .mention_html ()} ! I'm { context .bot .bot .mention_html ()} , the "
9679 f"guardian of the group { group_mention } , that you requested to join.\n \n Before you can "
@@ -114,7 +97,6 @@ async def join_request_callback(update: Update, context: ContextTypes.DEFAULT_TY
11497 # If the user blocked the bot, let's give the admins a chance to handle that
11598 # TG also notifies the user and forwards the message once the user unblocks the bot, but
11699 # forwarding it still doesn't hurt ...
117- user_data .setdefault ("blocked bot" , []).append (get_dtm_str ())
118100 text = (
119101 f"User { user .mention_html ()} with id { user .id } requested to join the group "
120102 f"{ join_request .chat .username } but has blocked me. Please manually handle this."
@@ -133,23 +115,16 @@ async def join_request_callback(update: Update, context: ContextTypes.DEFAULT_TY
133115
134116
135117async def join_request_buttons (update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
136- user_data = cast (Dict [Any , Any ], context .user_data )
137118 callback_query = cast (CallbackQuery , update .callback_query )
138119 user = cast (User , update .effective_user )
139120 _ , press , chat_id = cast (str , callback_query .data ).split ()
140121 if press == "2" :
141- user_data .setdefault (int (chat_id ), {}).setdefault ("pressed button 2" , []).append (
142- get_dtm_str ()
143- )
144122 jobs = cast (JobQueue , context .job_queue ).get_jobs_by_name (
145123 f"JOIN_TIMEOUT { chat_id } { user .id } "
146124 )
147125 if jobs :
148126 for job in jobs :
149127 job .schedule_removal ()
150- user_data .setdefault (int (chat_id ), {}).setdefault (
151- "removed join timeout" , []
152- ).append (get_dtm_str ())
153128
154129 try :
155130 await approve_user (
@@ -162,9 +137,6 @@ async def join_request_buttons(update: Update, context: ContextTypes.DEFAULT_TYP
162137
163138 reply_markup = None
164139 else :
165- user_data .setdefault (int (chat_id ), {}).setdefault ("pressed button 1" , []).append (
166- get_dtm_str ()
167- )
168140 reply_markup = InlineKeyboardMarkup .from_button (
169141 InlineKeyboardButton (
170142 text = "⚠️ Tap again to confirm" ,
@@ -193,3 +165,11 @@ async def join_request_timeout_job(context: ContextTypes.DEFAULT_TYPE) -> None:
193165 except Forbidden as exc :
194166 if "user is deactivated" not in exc .message :
195167 raise exc
168+ except BadRequest as exc :
169+ # These apparently happen frequently, e.g. when user clear the chat
170+ if exc .message not in [
171+ "Message to edit not found" ,
172+ "Can't access the chat" ,
173+ "Chat not found" ,
174+ ]:
175+ raise exc
0 commit comments