uniborg/stdplugins/sp_prog_rename.py

52 lines
1.3 KiB
Python
Raw Normal View History

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
prog_tech_id = 1040270887
prog_tech_channel = None
default_title = "Programming & Tech"
lock = asyncio.Lock()
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 = '&'
return token.capitalize() + (' ' if m.group(2) else '')
return re.sub(r'(\S+)(\s+)?', replace, s)
2018-06-03 12:16:14 +00:00
async def edit_title(title):
global prog_tech_channel
if prog_tech_channel is None:
prog_tech_channel = await borg.get_entity(prog_tech_id)
await borg(EditTitleRequest(
channel=prog_tech_channel, title=title
))
@borg.on(events.NewMessage(
pattern=re.compile(r"(?i)programming (?:&|and) (.+)"), chats=prog_tech_id))
async def on_name(event):
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
if len(new_title) > 255 or lock.locked():
return
with (await lock):
await edit_title(new_title)
await asyncio.sleep(80)
2018-06-10 19:54:00 +00:00
await asyncio.sleep(2 * 60 * 60)
2018-06-03 12:16:14 +00:00
if lock.locked():
return
with (await lock):
await edit_title(default_title)