make it work for different chats

kate
udf 2019-04-27 12:22:23 +02:00
parent d2c6607429
commit ccd47186b1
Signed by: kate
GPG Key ID: F17AECB58C3A3096
1 changed files with 14 additions and 9 deletions

View File

@ -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))