forked from uniborg/uniborg
Compare commits
9 Commits
a3981e0994
...
fc78927fa6
Author | SHA1 | Date |
---|---|---|
udf | fc78927fa6 | |
udf | 9f61d75ec9 | |
udf | 5fa301f563 | |
udf | 0948bd1d5b | |
udf | 3b71397a37 | |
udf | ccd47186b1 | |
udf | d2c6607429 | |
udf | 0b64a64568 | |
udf | b98bc8eb02 |
|
@ -1,17 +1,30 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import re
|
import re
|
||||||
|
import html
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events, utils
|
||||||
from telethon.tl.functions.channels import EditTitleRequest
|
from telethon.tl.functions.channels import EditTitleRequest
|
||||||
from telethon.errors.rpcerrorlist import ChatNotModifiedError
|
from telethon.errors.rpcerrorlist import ChatNotModifiedError
|
||||||
|
|
||||||
|
|
||||||
MULTI_EDIT_TIMEOUT = 80
|
MULTI_EDIT_TIMEOUT = 80
|
||||||
REVERT_TIMEOUT = 2 * 60 * 60
|
REVERT_TIMEOUT = 2 * 60 * 60
|
||||||
CHANNEL_ID = 1040270887
|
|
||||||
DEFAULT_TITLE = "Programming & Tech"
|
|
||||||
prog_tech_channel = None
|
@dataclass
|
||||||
|
class Group:
|
||||||
|
id: int
|
||||||
|
title: str
|
||||||
rename_lock = asyncio.Lock()
|
rename_lock = asyncio.Lock()
|
||||||
revert_task = None
|
revert_task: asyncio.Task = None
|
||||||
|
|
||||||
|
|
||||||
|
GROUPS = {group.id: group for group in (
|
||||||
|
# Group(1040270887, 'Programming & Tech'),
|
||||||
|
Group(1384391544, 'Programming & Tech for girls'),
|
||||||
|
Group(1286178907, 'test supergroup')
|
||||||
|
)}
|
||||||
|
|
||||||
|
|
||||||
def fix_title(s):
|
def fix_title(s):
|
||||||
|
@ -24,56 +37,59 @@ def fix_title(s):
|
||||||
return re.sub(r'(\S+)(\s+)?', replace, s)
|
return re.sub(r'(\S+)(\s+)?', replace, s)
|
||||||
|
|
||||||
|
|
||||||
async def edit_title(title):
|
async def edit_title(chat, title):
|
||||||
global prog_tech_channel
|
|
||||||
if prog_tech_channel is None:
|
|
||||||
prog_tech_channel = await borg.get_entity(CHANNEL_ID)
|
|
||||||
try:
|
try:
|
||||||
await borg(EditTitleRequest(
|
await borg(EditTitleRequest(
|
||||||
channel=prog_tech_channel, title=title
|
channel=chat, title=title
|
||||||
))
|
))
|
||||||
except ChatNotModifiedError:
|
except ChatNotModifiedError:
|
||||||
pass # Everything is ok
|
pass # Everything is ok
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_delete(deleted_fut, timeout):
|
async def wait_and_revert(chat_id, title, timeout):
|
||||||
try:
|
await asyncio.sleep(timeout)
|
||||||
await asyncio.wait_for(deleted_fut, timeout)
|
await edit_title(chat_id, title)
|
||||||
return True
|
|
||||||
except asyncio.TimeoutError:
|
|
||||||
pass
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
async def wait_and_revert(deleted_fut, timeout):
|
|
||||||
await wait_for_delete(deleted_fut, timeout)
|
|
||||||
await edit_title(DEFAULT_TITLE)
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(
|
@borg.on(events.NewMessage(
|
||||||
pattern=re.compile(r"(?i)programming (?:&|and) (.+)"), chats=CHANNEL_ID))
|
pattern=re.compile(r"(?i)programming (?:&|and) (.+)"),
|
||||||
|
chats=list(GROUPS.keys())))
|
||||||
async def on_name(event):
|
async def on_name(event):
|
||||||
global revert_task
|
|
||||||
new_topic = fix_title(event.pattern_match.group(1))
|
new_topic = fix_title(event.pattern_match.group(1))
|
||||||
new_title = f"Programming & {new_topic}"
|
new_title = f"Programming & {new_topic}"
|
||||||
if "Tech" not in new_title:
|
if "Tech" not in new_title:
|
||||||
new_title += " & Tech"
|
new_title += " & Tech"
|
||||||
|
|
||||||
if len(new_title) > 255 or rename_lock.locked():
|
group = GROUPS[utils.get_peer_id(event.chat_id, False)] # Thanks Lonami
|
||||||
|
|
||||||
|
logger.info(f'{event.from_id} in {group.id} '
|
||||||
|
f'requested a title change to {new_title}')
|
||||||
|
|
||||||
|
if len(new_title) > 255:
|
||||||
|
logger.info('Not changing group title because new title is too long')
|
||||||
return
|
return
|
||||||
|
|
||||||
with (await rename_lock):
|
if group.rename_lock.locked():
|
||||||
await edit_title(new_title)
|
logger.info('Not changing group title because the rename lock is already held')
|
||||||
deleted_fut = borg.await_event(events.MessageDeleted(
|
return
|
||||||
chats=CHANNEL_ID,
|
|
||||||
func=lambda e: e.deleted_id == event.message.id
|
with (await group.rename_lock):
|
||||||
))
|
logger.info(f'Changing group title to {new_title}')
|
||||||
if await wait_for_delete(asyncio.shield(deleted_fut), MULTI_EDIT_TIMEOUT):
|
await event.respond(
|
||||||
await edit_title(DEFAULT_TITLE)
|
f'<a href="tg://user?id={event.from_id}">{html.escape(event.sender.first_name)}</a>'
|
||||||
|
' changed the group title!',
|
||||||
|
parse_mode='html'
|
||||||
|
)
|
||||||
|
await edit_title(event.chat_id, new_title)
|
||||||
|
logger.info(f'Holding rename lock for {MULTI_EDIT_TIMEOUT} seconds')
|
||||||
await asyncio.sleep(MULTI_EDIT_TIMEOUT)
|
await asyncio.sleep(MULTI_EDIT_TIMEOUT)
|
||||||
return
|
|
||||||
|
|
||||||
if revert_task and not revert_task.done():
|
if group.revert_task and not group.revert_task.done():
|
||||||
revert_task.cancel()
|
logger.info('Cancelling previous revert task')
|
||||||
|
group.revert_task.cancel()
|
||||||
revert_task = asyncio.create_task(wait_and_revert(deleted_fut, REVERT_TIMEOUT))
|
logger.info('Creating revert task')
|
||||||
|
group.revert_task = asyncio.create_task(wait_and_revert(
|
||||||
|
event.chat_id,
|
||||||
|
group.title,
|
||||||
|
REVERT_TIMEOUT
|
||||||
|
))
|
||||||
|
|
Loading…
Reference in New Issue