2018-06-03 12:16:14 +00:00
|
|
|
import asyncio
|
2018-11-01 21:02:34 +00:00
|
|
|
import re
|
|
|
|
|
2018-06-03 12:16:14 +00:00
|
|
|
from telethon import events
|
|
|
|
from telethon.tl.functions.channels import EditTitleRequest
|
2018-11-22 14:02:48 +00:00
|
|
|
from telethon.errors.rpcerrorlist import ChatNotModifiedError
|
2018-06-03 12:16:14 +00:00
|
|
|
|
2019-04-02 20:46:36 +00:00
|
|
|
MULTI_EDIT_TIMEOUT = 80
|
|
|
|
REVERT_TIMEOUT = 2 * 60 * 60
|
|
|
|
CHANNEL_ID = 1040270887
|
2018-11-13 22:33:35 +00:00
|
|
|
DEFAULT_TITLE = "Programming & Tech"
|
2019-04-02 20:21:26 +00:00
|
|
|
rename_lock = asyncio.Lock()
|
2019-04-02 20:46:36 +00:00
|
|
|
revert_task = None
|
2018-06-03 12:16:14 +00:00
|
|
|
|
|
|
|
|
2018-11-01 21:04:15 +00:00
|
|
|
def fix_title(s):
|
|
|
|
# Ideally this would be a state machine, but ¯\_(ツ)_/¯
|
|
|
|
def replace(m):
|
|
|
|
token = m.group(1)
|
|
|
|
if token.lower() == 'and':
|
|
|
|
token = '&'
|
2018-11-01 21:24:17 +00:00
|
|
|
return token[0].upper() + token[1:] + (' ' if m.group(2) else '')
|
2018-11-01 21:04:15 +00:00
|
|
|
return re.sub(r'(\S+)(\s+)?', replace, s)
|
|
|
|
|
|
|
|
|
2018-06-03 12:16:14 +00:00
|
|
|
async def edit_title(title):
|
2018-11-22 14:02:48 +00:00
|
|
|
try:
|
|
|
|
await borg(EditTitleRequest(
|
2019-04-27 10:01:40 +00:00
|
|
|
channel=CHANNEL_ID, title=title
|
2018-11-22 14:02:48 +00:00
|
|
|
))
|
|
|
|
except ChatNotModifiedError:
|
|
|
|
pass # Everything is ok
|
2018-06-03 12:16:14 +00:00
|
|
|
|
|
|
|
|
2018-11-13 22:37:10 +00:00
|
|
|
async def wait_for_delete(deleted_fut, timeout):
|
|
|
|
try:
|
|
|
|
await asyncio.wait_for(deleted_fut, timeout)
|
|
|
|
return True
|
2018-11-14 14:34:29 +00:00
|
|
|
except asyncio.TimeoutError:
|
2018-11-13 22:37:10 +00:00
|
|
|
pass
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-04-02 20:46:36 +00:00
|
|
|
async def wait_and_revert(deleted_fut, timeout):
|
|
|
|
await wait_for_delete(deleted_fut, timeout)
|
|
|
|
await edit_title(DEFAULT_TITLE)
|
|
|
|
|
|
|
|
|
2018-06-03 12:16:14 +00:00
|
|
|
@borg.on(events.NewMessage(
|
2018-11-13 22:33:35 +00:00
|
|
|
pattern=re.compile(r"(?i)programming (?:&|and) (.+)"), chats=CHANNEL_ID))
|
2018-06-03 12:16:14 +00:00
|
|
|
async def on_name(event):
|
2019-04-02 20:46:36 +00:00
|
|
|
global revert_task
|
2018-11-01 21:04:15 +00:00
|
|
|
new_topic = fix_title(event.pattern_match.group(1))
|
2018-06-10 19:54:00 +00:00
|
|
|
new_title = f"Programming & {new_topic}"
|
|
|
|
if "Tech" not in new_title:
|
|
|
|
new_title += " & Tech"
|
2018-06-03 12:16:14 +00:00
|
|
|
|
2019-04-02 20:21:26 +00:00
|
|
|
if len(new_title) > 255 or rename_lock.locked():
|
2018-06-03 12:16:14 +00:00
|
|
|
return
|
|
|
|
|
2019-04-02 20:21:26 +00:00
|
|
|
with (await rename_lock):
|
2018-06-03 12:16:14 +00:00
|
|
|
await edit_title(new_title)
|
2018-11-14 14:34:29 +00:00
|
|
|
deleted_fut = borg.await_event(events.MessageDeleted(
|
2018-11-13 22:37:10 +00:00
|
|
|
chats=CHANNEL_ID,
|
|
|
|
func=lambda e: e.deleted_id == event.message.id
|
2018-11-14 14:34:29 +00:00
|
|
|
))
|
|
|
|
if await wait_for_delete(asyncio.shield(deleted_fut), MULTI_EDIT_TIMEOUT):
|
2019-04-02 20:46:36 +00:00
|
|
|
await edit_title(DEFAULT_TITLE)
|
2018-11-14 14:22:43 +00:00
|
|
|
await asyncio.sleep(MULTI_EDIT_TIMEOUT)
|
2018-11-13 22:37:10 +00:00
|
|
|
return
|
2019-04-02 20:46:36 +00:00
|
|
|
|
|
|
|
if revert_task and not revert_task.done():
|
|
|
|
revert_task.cancel()
|
|
|
|
|
|
|
|
revert_task = asyncio.create_task(wait_and_revert(deleted_fut, REVERT_TIMEOUT))
|