Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3d33809

Browse files
authored
Stabilize Leaving of Disallowed Chats & Delete Privacy Command in Groups (#143)
* Stabilize Leaving of Disallowed Chats* Delete Privacy Command in Groups
1 parenta555e0b commit3d33809

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

‎components/callbacks.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
importasyncio
2+
importcontextlib
23
importlogging
34
importrandom
45
importre
@@ -18,11 +19,14 @@
1819
User,
1920
)
2021
fromtelegram.constantsimportChatAction,MessageLimit
22+
fromtelegram.errorimportBadRequest
2123
fromtelegram.extimportApplication,ApplicationHandlerStop,ContextTypes,Job,JobQueue
2224
fromtelegram.helpersimportescape_markdown
2325

2426
fromcomponentsimportconst
2527
fromcomponents.constimport (
28+
ALLOWED_CHAT_IDS,
29+
ALLOWED_USERNAMES,
2630
BUY_TEXT,
2731
DEFAULT_REPO_NAME,
2832
DEFAULT_REPO_OWNER,
@@ -290,8 +294,18 @@ async def delete_message(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
290294
awaittry_to_delete(cast(Message,update.effective_message))
291295

292296

293-
asyncdefleave_chat(update:Update,context:ContextTypes.DEFAULT_TYPE)->None:
294-
context.application.create_task(cast(Chat,update.effective_chat).leave(),update=update)
297+
asyncdefleave_chat(update:Update,_:ContextTypes.DEFAULT_TYPE)->None:
298+
if (
299+
not (chat:=update.effective_chat)
300+
orchat.type==chat.PRIVATE
301+
orchat.usernameinALLOWED_USERNAMES
302+
orchat.idinALLOWED_CHAT_IDS
303+
):
304+
return
305+
306+
withcontextlib.suppress(BadRequest):
307+
awaitchat.leave()
308+
295309
raiseApplicationHandlerStop
296310

297311

@@ -585,5 +599,7 @@ async def privacy(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
585599
"""Reply with the privacy policy"""
586600
message=cast(Message,update.effective_message)
587601
awaitmessage.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
589603
)
604+
ifmessage.chat.type!=message.chat.PRIVATE:
605+
awaittry_to_delete(message)

‎components/const.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
OFFTOPIC_USERNAME="pythontelegrambottalk"
2020
ONTOPIC_USERNAME="pythontelegrambotgroup"
2121
DEV_GROUP_USERNAME="pythontelegrambotdev"
22+
CHANNEL_USERNAME="pythontelegrambotchannel"
23+
CHANNEL_CHAT_ID="@"+CHANNEL_USERNAME
2224
OFFTOPIC_CHAT_ID="@"+OFFTOPIC_USERNAME
2325
ONTOPIC_CHAT_ID="@"+ONTOPIC_USERNAME
2426
ERROR_CHANNEL_CHAT_ID=-1001397960657
@@ -34,7 +36,7 @@
3436
WIKI_FAQ_URL=urljoin(WIKI_URL,"Frequently-Asked-Questions")
3537
WIKI_FRDP_URL=urljoin(WIKI_URL,"Frequently-requested-design-patterns")
3638
EXAMPLES_URL=urljoin(DOCS_URL,"/examples.html")
37-
ALLOWED_USERNAMES= (OFFTOPIC_USERNAME,ONTOPIC_USERNAME,DEV_GROUP_USERNAME)
39+
ALLOWED_USERNAMES= (OFFTOPIC_USERNAME,ONTOPIC_USERNAME,DEV_GROUP_USERNAME,CHANNEL_USERNAME)
3840
ALLOWED_CHAT_IDS= (
3941
ERROR_CHANNEL_CHAT_ID,
4042
-1001494805131,# dev chat

‎rules_bot.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Defaults,
2121
InlineQueryHandler,
2222
MessageHandler,
23+
TypeHandler,
2324
filters,
2425
)
2526

@@ -45,8 +46,6 @@
4546
tag_hint,
4647
)
4748
fromcomponents.constimport (
48-
ALLOWED_CHAT_IDS,
49-
ALLOWED_USERNAMES,
5049
COMPAT_ERRORS,
5150
DESCRIPTION,
5251
ERROR_CHANNEL_CHAT_ID,
@@ -135,11 +134,9 @@ def main() -> None:
135134
)
136135
# Leave groups that are not maintained by PTB
137136
application.add_handler(
138-
MessageHandler(
139-
filters.ChatType.GROUPS
140-
&~filters.StatusUpdate.LEFT_CHAT_MEMBER
141-
&~(filters.Chat(username=ALLOWED_USERNAMES)|filters.Chat(chat_id=ALLOWED_CHAT_IDS)),
142-
leave_chat,
137+
TypeHandler(
138+
type=Update,
139+
callback=leave_chat,
143140
),
144141
group=-2,
145142
)
@@ -153,9 +150,7 @@ def main() -> None:
153150

154151
application.add_handler(
155152
MessageHandler(
156-
filters.SenderChat.CHANNEL
157-
&~filters.IS_AUTOMATIC_FORWARD
158-
&~filters.Chat(chat_id=ERROR_CHANNEL_CHAT_ID),
153+
filters.SenderChat.CHANNEL&~filters.ChatType.CHANNEL&~filters.IS_AUTOMATIC_FORWARD,
159154
ban_sender_channels,
160155
block=False,
161156
)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp