Compare commits

..

No commits in common. "54c399f7d54a7c783745784aef596f282fb89f16" and "4271767df31139aa59cf254871d20bac9e016823" have entirely different histories.

6 changed files with 20 additions and 44 deletions

View File

@ -4,25 +4,12 @@ Contains code used by other kbass_* plugins
from uniborg import util from uniborg import util
async def get_target_message(borg, event):
"""
If the event is a reply, returns the reply message if it's from us
If event is not a reply, then it tries to return the most recent message
from us
"""
target = await event.get_reply_message()
if event.is_reply and target.from_id == borg.uid:
return target
if not target:
return await util.get_recent_self_message(borg, event)
def self_reply_cmd(borg, pattern): def self_reply_cmd(borg, pattern):
def wrapper(function): def wrapper(function):
@borg.on(util.admin_cmd(pattern)) @borg.on(util.admin_cmd(pattern))
async def wrapped(event, *args, **kwargs): async def wrapped(event, *args, **kwargs):
await event.delete() await event.delete()
target = await get_target_message(borg, event) target = await util.get_target_message(borg, event)
if not target: if not target:
return return
return await function(event, target, *args, **kwargs) return await function(event, target, *args, **kwargs)

View File

@ -14,6 +14,7 @@ from stdplugins.kbass_core import self_reply_cmd
@self_reply_cmd(borg, r"^\.e$") @self_reply_cmd(borg, r"^\.e$")
async def on_edit_start(event, target): async def on_edit_start(event, target):
await asyncio.sleep(3) # tdesktop doesn't sync drafts when the window is active
await borg(SaveDraftRequest( await borg(SaveDraftRequest(
peer=await event.get_input_chat(), peer=await event.get_input_chat(),
message=(target.message or '.') + '\n.e', message=(target.message or '.') + '\n.e',
@ -23,18 +24,18 @@ async def on_edit_start(event, target):
)) ))
@self_reply_cmd(borg, r'(?ms)^(.+\n|\.)\.e$') @self_reply_cmd(borg, r'(?ms)^(.+)\.e$')
async def on_edit_end(event, target): async def on_edit_end(event, target):
text = event.pattern_match.group(1) text = event.pattern_match.group(1)
if text == '.': message = event.message.message[:-2]
text = '' if message.strip() == '.':
chat = await event.get_input_chat() message = ''
try: try:
await borg(EditMessageRequest( await borg(EditMessageRequest(
peer=chat, peer=await event.get_input_chat(),
id=target.id, id=target.id,
no_webpage=not target.media, no_webpage=not target.media,
message=text, message=message,
entities=event.message.entities entities=event.message.entities
)) ))
except MessageEmptyError: except MessageEmptyError:

View File

@ -3,17 +3,13 @@ Reply to a file with .f to send it as a photo
""" """
from io import BytesIO from io import BytesIO
from uniborg import util from stdplugins.kbass_core import self_reply_cmd
from telethon import types from telethon import types
from telethon.errors import PhotoInvalidDimensionsError from telethon.errors import PhotoInvalidDimensionsError
from telethon.tl.functions.messages import SendMediaRequest
@borg.on(util.admin_cmd(r"^\.f$")) @self_reply_cmd(borg, r"^\.f$")
async def on_file_to_photo(event): async def on_file_to_photo(event, target):
await event.delete()
target = await event.get_reply_message()
try: try:
image = target.media.document image = target.media.document
except AttributeError: except AttributeError:
@ -28,15 +24,11 @@ async def on_file_to_photo(event):
file = await borg.download_media(target, file=BytesIO()) file = await borg.download_media(target, file=BytesIO())
file.seek(0) file.seek(0)
img = await borg.upload_file(file) img = await borg.upload_file(file)
img.name = 'image.png'
try: try:
await borg(SendMediaRequest( await event.respond(
peer=await event.get_input_chat(), reply_to=target,
media=types.InputMediaUploadedPhoto(img), file=types.InputMediaUploadedPhoto(img)
message=target.message, )
entities=target.entities,
reply_to_msg_id=target.id
))
except PhotoInvalidDimensionsError: except PhotoInvalidDimensionsError:
return return

View File

@ -21,7 +21,7 @@ async def on_yank(event, targets, num_offset):
message = '\n\n'.join(get_message_html(target) for target in targets) message = '\n\n'.join(get_message_html(target) for target in targets)
message, entities = thtml.parse(message) message, entities = thtml.parse(message)
await borg(SaveDraftRequest( await borg(SaveDraftRequest(
peer=await event.get_input_chat(), peer='me',
message=message, message=message,
entities=entities, entities=entities,
no_webpage=True, no_webpage=True,

View File

@ -11,12 +11,6 @@ import telethon.utils
from uniborg import util from uniborg import util
async def get_target_message(event):
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
return await event.get_reply_message()
return await util.get_recent_self_message(borg, event)
async def await_read(chat, message): async def await_read(chat, message):
if isinstance(chat, InputPeerSelf): if isinstance(chat, InputPeerSelf):
return return
@ -43,7 +37,7 @@ async def delete(event):
text = event.pattern_match.group(2) text = event.pattern_match.group(2)
if not text: if not text:
return return
target = await get_target_message(event) target = await util.get_target_message(borg, event)
if target: if target:
chat = await event.get_input_chat() chat = await event.get_input_chat()
await await_read(chat, target) await await_read(chat, target)

View File

@ -30,7 +30,9 @@ async def is_read(borg, entity, message, is_out=None):
return message_id <= max_id return message_id <= max_id
async def get_recent_self_message(borg, event): async def get_target_message(borg, event):
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
return await event.get_reply_message()
async for message in borg.iter_messages( async for message in borg.iter_messages(
await event.get_input_chat(), limit=20): await event.get_input_chat(), limit=20):
if message.out: if message.out: