Compare commits

...

6 Commits

Author SHA1 Message Date
udf e810a977c5
Merge branch 'master' into kate 2019-05-19 23:11:15 +02:00
udf 573f36bf57
fix broken indentation when lots of nested items are present 2019-05-19 23:00:23 +02:00
udf b21f5ec104
Fix trailing whitespaces
kate you're better than this
2019-05-19 22:59:43 +02:00
udf 30c4f29b9c
add delete plugin 2019-05-07 15:16:47 +02:00
udf 8290974235
make gif square with dark background 2019-05-03 23:15:30 +02:00
udf 49ac424183
add thing 2019-05-02 23:17:41 +02:00
3 changed files with 111 additions and 5 deletions

View File

@ -0,0 +1,89 @@
from io import BytesIO
from uniborg import util
from PIL import Image
from telethon import types, utils, events
from telethon.tl.functions.messages import SaveGifRequest, UploadMediaRequest
sticker_to_gif = storage.sticker_to_gif or {}
access_hashes = storage.access_hashes or {}
gif_to_sticker = {str(gif): int(sticker) for sticker, gif in sticker_to_gif.items()}
async def convert_sticker_to_gif(sticker):
gif_id = sticker_to_gif.get(str(sticker.id), None)
if gif_id:
access_hash = access_hashes[str(gif_id)]
return types.InputDocument(gif_id, access_hash, b'')
file = BytesIO()
await borg.download_media(sticker, file=file)
file.seek(0)
# remove alpha
im = Image.open(file)
alpha = im.convert('RGBA').getchannel('A')
size = max(im.width, im.height)
new_im = Image.new('RGBA', (size, size), (40, 40, 40, 255))
xy = (round((size - im.width) / 2), round((size - im.height) / 2))
new_im.paste(im, box=xy, mask=alpha)
file = BytesIO()
new_im.save(file, format='gif')
file.seek(0)
# upload file
file = await borg.upload_file(file, part_size_kb=512)
file = types.InputMediaUploadedDocument(file, 'video/mp4', [])
media = await borg(UploadMediaRequest('me', file))
media = utils.get_input_document(media)
# save (that's right, this is relational json)
sticker_to_gif[str(sticker.id)] = media.id
gif_to_sticker[str(media.id)] = sticker.id
access_hashes[str(sticker.id)] = sticker.access_hash
access_hashes[str(media.id)] = media.access_hash
storage.sticker_to_gif = sticker_to_gif
storage.access_hashes = access_hashes
return media
@borg.on(util.admin_cmd(r'^\.ss$'))
async def on_save(event):
await event.delete()
target = await event.get_reply_message()
media = target.gif or target.sticker
if not media:
return
if target.sticker:
media = await convert_sticker_to_gif(media)
await borg(
SaveGifRequest(id=media, unsave=False)
)
@borg.on(events.NewMessage(outgoing=True))
async def on_sticker(event):
if not event.sticker:
return
media = await convert_sticker_to_gif(event.sticker)
await borg(
SaveGifRequest(id=media, unsave=False)
)
@borg.on(events.NewMessage(outgoing=True))
async def on_gif(event):
if not event.gif:
return
sticker_id = gif_to_sticker.get(str(event.gif.id), None)
if not sticker_id:
return
access_hash = access_hashes[str(sticker_id)]
sticker = types.InputDocument(sticker_id, access_hash, b'')
await event.delete()
await borg.send_message(
await event.get_input_chat(),
file=sticker,
reply_to=event.message.reply_to_msg_id
)

View File

@ -37,7 +37,7 @@ def yaml_format(obj, indent=0):
has_multiple_items = len(items) > 2
if has_multiple_items:
result.append('\n')
indent += 2
indent += 2
for k, v in items:
if k == '_' or v is None:
continue
@ -45,11 +45,14 @@ def yaml_format(obj, indent=0):
if not formatted.strip():
continue
result.append(' ' * (indent if has_multiple_items else 1))
result.append(f'{k}: {formatted}')
result.append(f'{k}:')
if not formatted[0].isspace():
result.append(' ')
result.append(f'{formatted}')
result.append('\n')
result.pop()
indent -= 2
result.append(' ' * indent)
if has_multiple_items:
indent -= 2
elif isinstance(obj, str):
# truncate long strings and display elipsis
result = repr(obj[:STR_LEN_MAX])
@ -76,7 +79,6 @@ def yaml_format(obj, indent=0):
result.append('\n')
result.pop()
indent -= 2
result.append(' ' * indent)
else:
return repr(obj)

View File

@ -0,0 +1,15 @@
"""
Reply to a message with .d <n> to delete <n> messages from that point
(negative values of <n> go backwards in history)
"""
import asyncio
from stdplugins.kbass_core import self_reply_selector
@self_reply_selector(borg, r'\.d')
async def on_save(event, targets, num_offset):
await borg.delete_messages(
await event.get_input_chat(),
targets
)