From ccd47186b1bbdd749e88a52bd82eed0cdd56d844 Mon Sep 17 00:00:00 2001 From: udf Date: Sat, 27 Apr 2019 12:22:23 +0200 Subject: [PATCH] make it work for different chats --- stdplugins/sp_prog_rename.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/stdplugins/sp_prog_rename.py b/stdplugins/sp_prog_rename.py index db2202b..d2cfb0a 100644 --- a/stdplugins/sp_prog_rename.py +++ b/stdplugins/sp_prog_rename.py @@ -8,8 +8,11 @@ from telethon.errors.rpcerrorlist import ChatNotModifiedError MULTI_EDIT_TIMEOUT = 80 REVERT_TIMEOUT = 2 * 60 * 60 -CHANNEL_ID = 1286178907 -DEFAULT_TITLE = "Programming & Tech" +DEFAULT_TITLES = { + 1040270887: 'Programming & Tech', + 1384391544: 'Programming & Tech for girls', + 1286178907: 'test supergroup' +} rename_lock = asyncio.Lock() revert_task = None @@ -24,10 +27,10 @@ def fix_title(s): return re.sub(r'(\S+)(\s+)?', replace, s) -async def edit_title(title): +async def edit_title(chat, title): try: await borg(EditTitleRequest( - channel=CHANNEL_ID, title=title + channel=chat, title=title )) except ChatNotModifiedError: pass # Everything is ok @@ -42,13 +45,14 @@ async def wait_for_delete(deleted_fut, timeout): return False -async def wait_and_revert(timeout): +async def wait_and_revert(chat_id, timeout): await asyncio.sleep(timeout) - await edit_title(DEFAULT_TITLE) + await edit_title(chat_id, DEFAULT_TITLES[chat_id]) @borg.on(events.NewMessage( - pattern=re.compile(r"(?i)programming (?:&|and) (.+)"), chats=CHANNEL_ID)) + pattern=re.compile(r"(?i)programming (?:&|and) (.+)"), + chats=list(DEFAULT_TITLES.keys()))) async def on_name(event): global revert_task new_topic = fix_title(event.pattern_match.group(1)) @@ -66,9 +70,10 @@ async def on_name(event): ) with (await rename_lock): - await edit_title(new_title) + await edit_title(event.chat_id, new_title) await asyncio.sleep(MULTI_EDIT_TIMEOUT) if revert_task and not revert_task.done(): revert_task.cancel() - revert_task = asyncio.create_task(wait_and_revert(REVERT_TIMEOUT)) + revert_task = asyncio.create_task( + wait_and_revert(event.chat_id, REVERT_TIMEOUT))