|
1 | 1 | import asyncio |
| 2 | +import contextlib |
2 | 3 | import logging |
3 | 4 | import random |
4 | 5 | import re |
|
18 | 19 | User, |
19 | 20 | ) |
20 | 21 | from telegram.constants import ChatAction, MessageLimit |
| 22 | +from telegram.error import BadRequest |
21 | 23 | from telegram.ext import Application, ApplicationHandlerStop, ContextTypes, Job, JobQueue |
22 | 24 | from telegram.helpers import escape_markdown |
23 | 25 |
|
24 | 26 | from components import const |
25 | 27 | from components.const import ( |
| 28 | + ALLOWED_CHAT_IDS, |
| 29 | + ALLOWED_USERNAMES, |
26 | 30 | BUY_TEXT, |
27 | 31 | DEFAULT_REPO_NAME, |
28 | 32 | DEFAULT_REPO_OWNER, |
@@ -290,8 +294,18 @@ async def delete_message(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: |
290 | 294 | await try_to_delete(cast(Message, update.effective_message)) |
291 | 295 |
|
292 | 296 |
|
293 | | -async def leave_chat(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: |
294 | | - context.application.create_task(cast(Chat, update.effective_chat).leave(), update=update) |
| 297 | +async def leave_chat(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: |
| 298 | + if ( |
| 299 | + not (chat := update.effective_chat) |
| 300 | + or chat.type == chat.PRIVATE |
| 301 | + or chat.username in ALLOWED_USERNAMES |
| 302 | + or chat.id in ALLOWED_CHAT_IDS |
| 303 | + ): |
| 304 | + return |
| 305 | + |
| 306 | + with contextlib.suppress(BadRequest): |
| 307 | + await chat.leave() |
| 308 | + |
295 | 309 | raise ApplicationHandlerStop |
296 | 310 |
|
297 | 311 |
|
@@ -585,5 +599,7 @@ async def privacy(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None: |
585 | 599 | """Reply with the privacy policy""" |
586 | 600 | message = cast(Message, update.effective_message) |
587 | 601 | await message.reply_text( |
588 | | - f"Please read my privacy policy in <a href='{PRIVACY_POLICY}'>here</a>." |
| 602 | + f"Please read my privacy policy in <a href='{PRIVACY_POLICY}'>here</a>.", quote=False |
589 | 603 | ) |
| 604 | + if message.chat.type != message.chat.PRIVATE: |
| 605 | + await try_to_delete(message) |
0 commit comments