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
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 wrapper(function):
@borg.on(util.admin_cmd(pattern))
async def wrapped(event, *args, **kwargs):
await event.delete()
target = await get_target_message(borg, event)
target = await util.get_target_message(borg, event)
if not target:
return
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$")
async def on_edit_start(event, target):
await asyncio.sleep(3) # tdesktop doesn't sync drafts when the window is active
await borg(SaveDraftRequest(
peer=await event.get_input_chat(),
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):
text = event.pattern_match.group(1)
if text == '.':
text = ''
chat = await event.get_input_chat()
message = event.message.message[:-2]
if message.strip() == '.':
message = ''
try:
await borg(EditMessageRequest(
peer=chat,
peer=await event.get_input_chat(),
id=target.id,
no_webpage=not target.media,
message=text,
message=message,
entities=event.message.entities
))
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 uniborg import util
from stdplugins.kbass_core import self_reply_cmd
from telethon import types
from telethon.errors import PhotoInvalidDimensionsError
from telethon.tl.functions.messages import SendMediaRequest
@borg.on(util.admin_cmd(r"^\.f$"))
async def on_file_to_photo(event):
await event.delete()
target = await event.get_reply_message()
@self_reply_cmd(borg, r"^\.f$")
async def on_file_to_photo(event, target):
try:
image = target.media.document
except AttributeError:
@ -28,15 +24,11 @@ async def on_file_to_photo(event):
file = await borg.download_media(target, file=BytesIO())
file.seek(0)
img = await borg.upload_file(file)
img.name = 'image.png'
try:
await borg(SendMediaRequest(
peer=await event.get_input_chat(),
media=types.InputMediaUploadedPhoto(img),
message=target.message,
entities=target.entities,
reply_to_msg_id=target.id
))
await event.respond(
reply_to=target,
file=types.InputMediaUploadedPhoto(img)
)
except PhotoInvalidDimensionsError:
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, entities = thtml.parse(message)
await borg(SaveDraftRequest(
peer=await event.get_input_chat(),
peer='me',
message=message,
entities=entities,
no_webpage=True,

View File

@ -11,12 +11,6 @@ import telethon.utils
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):
if isinstance(chat, InputPeerSelf):
return
@ -43,7 +37,7 @@ async def delete(event):
text = event.pattern_match.group(2)
if not text:
return
target = await get_target_message(event)
target = await util.get_target_message(borg, event)
if target:
chat = await event.get_input_chat()
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
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(
await event.get_input_chat(), limit=20):
if message.out: