forked from uniborg/uniborg
Compare commits
6 Commits
fc78927fa6
...
e810a977c5
Author | SHA1 | Date |
---|---|---|
udf | e810a977c5 | |
udf | 573f36bf57 | |
udf | b21f5ec104 | |
udf | 30c4f29b9c | |
udf | 8290974235 | |
udf | 49ac424183 |
|
@ -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
|
||||||
|
)
|
|
@ -45,11 +45,14 @@ def yaml_format(obj, indent=0):
|
||||||
if not formatted.strip():
|
if not formatted.strip():
|
||||||
continue
|
continue
|
||||||
result.append(' ' * (indent if has_multiple_items else 1))
|
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.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
|
if has_multiple_items:
|
||||||
indent -= 2
|
indent -= 2
|
||||||
result.append(' ' * indent)
|
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
# truncate long strings and display elipsis
|
# truncate long strings and display elipsis
|
||||||
result = repr(obj[:STR_LEN_MAX])
|
result = repr(obj[:STR_LEN_MAX])
|
||||||
|
@ -76,7 +79,6 @@ def yaml_format(obj, indent=0):
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
result.pop()
|
result.pop()
|
||||||
indent -= 2
|
indent -= 2
|
||||||
result.append(' ' * indent)
|
|
||||||
else:
|
else:
|
||||||
return repr(obj)
|
return repr(obj)
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
)
|
Loading…
Reference in New Issue