@@ -34,11 +34,36 @@ def create_moderation_handlers(moderation_service: ModerationService) -> list:
3434 CommandHandler ("mute" , _handle_mute ),
3535 CommandHandler ("unmute" , _handle_unmute ),
3636 CommandHandler ("report" , _handle_report ),
37+ CommandHandler ("forcegroupregistration" , _handle_force_group_registration ),
3738 ]
3839
3940
41+ async def _handle_force_group_registration (
42+ update : Update , context : ContextTypes .DEFAULT_TYPE
43+ ) -> None :
44+ """Force registration of current chat in bot_chats table. Admin only."""
45+ moderation_service : ModerationService = context .bot_data ["moderation_service" ]
46+ message = update .message
47+ if message is None or message .from_user is None :
48+ return
49+
50+ chat = update .effective_chat
51+ if chat is None or chat .type == "private" :
52+ await message .reply_text ("Questo comando funziona solo nei gruppi." )
53+ return
54+
55+ if not await _is_admin (context , chat .id , message .from_user .id ):
56+ await message .reply_text (
57+ "Solo gli amministratori possono usare questo comando."
58+ )
59+ return
60+
61+ await moderation_service .register_chat (chat .id )
62+ await message .reply_text (f"Gruppo registrato. Chat ID: { chat .id } " )
63+
64+
4065async def _handle_ban (update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
41- """Ban a user. Usage: /ban @username or /ban user_id [reason] or reply to message with /ban [reason]"""
66+ """Ban a user globally . Usage: /ban user_id [reason] or reply to message with /ban [reason]"""
4267 moderation_service : ModerationService = context .bot_data ["moderation_service" ]
4368 message = update .message
4469 if message is None or message .from_user is None :
@@ -60,30 +85,41 @@ async def _handle_ban(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non
6085 reason : str | None = None
6186 if message .reply_to_message and message .reply_to_message .from_user :
6287 user_id = message .reply_to_message .from_user .id
63- reason = args [ 0 ] if args else None
88+ reason = " " . join ( args ) if args else None
6489 elif args :
6590 target = args [0 ]
6691 reason = args [1 ] if len (args ) > 1 else None
6792 user_id = await _resolve_user_id (context , chat .id , target )
6893
6994 if user_id is None :
7095 await message .reply_text (
71- "Uso: /ban @username, /ban user_id [motivo], o rispondi al messaggio con /ban [motivo]. "
72- "Per @username funziona solo con amministratori."
96+ "Uso: /ban user_id [motivo], o rispondi al messaggio con /ban [motivo]."
7397 )
7498 return
7599
76- try :
77- await context .bot .ban_chat_member (chat .id , user_id )
78- await moderation_service .add_ban (user_id , chat .id , message .from_user .id , reason )
79- await message .reply_text (f"Utente bannato. Motivo: { reason or 'Nessuno' } " )
80- except Exception as e :
81- logger .warning ("Ban failed: %s" , e )
82- await message .reply_text ("Impossibile bannare l'utente." )
100+ chat_ids = await moderation_service .add_global_ban (
101+ user_id , message .from_user .id , reason
102+ )
103+
104+ success_count = 0
105+ fail_count = 0
106+ for cid in chat_ids :
107+ try :
108+ await context .bot .ban_chat_member (cid , user_id )
109+ success_count += 1
110+ except Exception as e :
111+ logger .debug ("Ban in chat %s failed: %s" , cid , e )
112+ fail_count += 1
113+
114+ msg = f"Utente bannato globalmente in { success_count } gruppi."
115+ if fail_count > 0 :
116+ msg += f" ({ fail_count } falliti)"
117+ msg += f"\n Motivo: { reason or 'Nessuno' } "
118+ await message .reply_text (msg )
83119
84120
85121async def _handle_unban (update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
86- """Unban a user. Usage: /unban @username or /unban user_id """
122+ """Unban a user globally . Usage: /unban user_id or reply to message with /unban"""
87123 moderation_service : ModerationService = context .bot_data ["moderation_service" ]
88124 message = update .message
89125 if message is None or message .from_user is None :
@@ -108,19 +144,26 @@ async def _handle_unban(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N
108144
109145 if user_id is None :
110146 await message .reply_text (
111- "Uso: /unban @username, /unban user_id, o rispondi al messaggio"
147+ "Uso: /unban user_id, o rispondi al messaggio con /unban "
112148 )
113- if user_id is None :
114- await message .reply_text ("Utente non trovato." )
115149 return
116150
117- try :
118- await context .bot .unban_chat_member (chat .id , user_id )
119- await moderation_service .remove_ban (user_id , chat .id )
120- await message .reply_text ("Utente sbannato." )
121- except Exception as e :
122- logger .warning ("Unban failed: %s" , e )
123- await message .reply_text ("Impossibile sbannare l'utente." )
151+ chat_ids = await moderation_service .remove_global_ban (user_id )
152+
153+ success_count = 0
154+ fail_count = 0
155+ for cid in chat_ids :
156+ try :
157+ await context .bot .unban_chat_member (cid , user_id )
158+ success_count += 1
159+ except Exception as e :
160+ logger .debug ("Unban in chat %s failed: %s" , cid , e )
161+ fail_count += 1
162+
163+ msg = f"Utente sbannato globalmente da { success_count } gruppi."
164+ if fail_count > 0 :
165+ msg += f" ({ fail_count } falliti)"
166+ await message .reply_text (msg )
124167
125168
126169async def _handle_mute (update : Update , context : ContextTypes .DEFAULT_TYPE ) -> None :
0 commit comments