forked from kate/uniborg
add tdesktop keyboard assistance plugins
This commit is contained in:
parent
599f794525
commit
4f05e252be
|
@ -0,0 +1,43 @@
|
|||
from uniborg import util
|
||||
|
||||
|
||||
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 util.get_target_message(borg, event)
|
||||
if not target:
|
||||
return
|
||||
return await function(event, target, *args, **kwargs)
|
||||
return wrapped
|
||||
return wrapper
|
||||
|
||||
|
||||
def self_reply_selector(borg, cmd):
|
||||
def wrapper(function):
|
||||
@borg.on(util.admin_cmd(cmd + r"( [+-]?\d+)?$"))
|
||||
async def wrapped(event, *args, **kwargs):
|
||||
await event.delete()
|
||||
reply = await event.get_reply_message()
|
||||
if not reply:
|
||||
return
|
||||
num_offset = int(event.pattern_match.group(1) or 0)
|
||||
reverse = num_offset > 0
|
||||
|
||||
targets = [reply] if reverse else []
|
||||
targets.extend(await borg.get_messages(
|
||||
await event.get_input_chat(),
|
||||
limit=abs(num_offset),
|
||||
offset_id=reply.id,
|
||||
reverse=reverse
|
||||
))
|
||||
if not reverse:
|
||||
# reverse the list because we want things to always be in
|
||||
# history order
|
||||
targets.reverse()
|
||||
targets.append(reply)
|
||||
|
||||
return await function(event, targets, num_offset, *args, **kwargs)
|
||||
return wrapped
|
||||
return wrapper
|
|
@ -0,0 +1,38 @@
|
|||
import asyncio
|
||||
|
||||
from telethon.errors import MessageEmptyError
|
||||
from telethon.tl.functions.messages import SaveDraftRequest
|
||||
from telethon.tl.functions.messages import EditMessageRequest
|
||||
|
||||
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',
|
||||
entities=target.entities,
|
||||
no_webpage=not target.media,
|
||||
reply_to_msg_id=target.id
|
||||
))
|
||||
|
||||
|
||||
@self_reply_cmd(borg, r'(?ms)^(.+)\.e$')
|
||||
async def on_edit_end(event, target):
|
||||
text = event.pattern_match.group(1)
|
||||
message = event.message.message[:-2]
|
||||
if message.strip() == '.':
|
||||
message = ''
|
||||
try:
|
||||
await borg(EditMessageRequest(
|
||||
peer=await event.get_input_chat(),
|
||||
id=target.id,
|
||||
no_webpage=not target.media,
|
||||
message=message,
|
||||
entities=event.message.entities
|
||||
))
|
||||
except MessageEmptyError:
|
||||
# Can't make text message empty, so delete it
|
||||
await borg.delete_messages(chat, target)
|
|
@ -0,0 +1,27 @@
|
|||
from io import BytesIO
|
||||
|
||||
from stdplugins.kbass_core import self_reply_cmd
|
||||
from telethon import types
|
||||
from telethon.errors import PhotoInvalidDimensionsError
|
||||
|
||||
|
||||
@self_reply_cmd(borg, r"^\.f$")
|
||||
async def on_file_to_photo(event, target):
|
||||
try:
|
||||
image = target.media.document
|
||||
except AttributeError:
|
||||
return
|
||||
if image.mime_type == 'image/webp' or not image.mime_type.startswith('image/'):
|
||||
return
|
||||
|
||||
file = await borg.download_media(target, file=BytesIO())
|
||||
file.seek(0)
|
||||
img = await borg.upload_file(file)
|
||||
|
||||
try:
|
||||
await event.respond(
|
||||
reply_to=target,
|
||||
file=types.InputMediaUploadedPhoto(img)
|
||||
)
|
||||
except PhotoInvalidDimensionsError:
|
||||
return
|
|
@ -0,0 +1,11 @@
|
|||
import asyncio
|
||||
|
||||
from stdplugins.kbass_core import self_reply_selector
|
||||
|
||||
|
||||
@self_reply_selector(borg, r'\.s')
|
||||
async def on_save(event, targets, num_offset):
|
||||
await borg.forward_messages('me', targets)
|
||||
msg = await event.respond(f'Saved {abs(num_offset) + 1} messages!')
|
||||
await asyncio.sleep(3)
|
||||
await borg.delete_messages(msg.to_id, msg)
|
|
@ -0,0 +1,22 @@
|
|||
from telethon.errors import MessageNotModifiedError
|
||||
from telethon.tl.functions.messages import EditMessageRequest
|
||||
|
||||
from stdplugins.kbass_core import self_reply_cmd
|
||||
|
||||
|
||||
@self_reply_cmd(borg, r"^\.p(?: ?)([yn])?$")
|
||||
async def on_edit_preview(event, target):
|
||||
preview = event.pattern_match.group(1) == 'y'
|
||||
if not event.pattern_match.group(1):
|
||||
preview = not bool(target.media)
|
||||
try:
|
||||
await borg(EditMessageRequest(
|
||||
peer=await event.get_input_chat(),
|
||||
id=target.id,
|
||||
no_webpage=not preview,
|
||||
message=target.message,
|
||||
entities=target.entities
|
||||
))
|
||||
except MessageNotModifiedError:
|
||||
# There was no preview to modify
|
||||
pass
|
|
@ -0,0 +1,25 @@
|
|||
|
||||
import html
|
||||
|
||||
from telethon.tl.functions.messages import SaveDraftRequest
|
||||
from telethon.extensions import html as thtml
|
||||
|
||||
from stdplugins.kbass_core import self_reply_selector
|
||||
|
||||
|
||||
def get_message_html(message):
|
||||
if message.action:
|
||||
return html.escape(str(message.action))
|
||||
return thtml.unparse(message.message, message.entities)
|
||||
|
||||
|
||||
@self_reply_selector(borg, r'\.y')
|
||||
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='me',
|
||||
message=message,
|
||||
entities=entities,
|
||||
no_webpage=True,
|
||||
))
|
Loading…
Reference in New Issue