forked from uniborg/uniborg
Compare commits
No commits in common. "e810a977c531ed2ce59170742280e175a56b3acb" and "fc78927fa6d3a67603a63adec7f4c4781002f049" have entirely different histories.
e810a977c5
...
fc78927fa6
|
@ -1,89 +0,0 @@
|
||||||
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
|
|
||||||
)
|
|
|
@ -37,7 +37,7 @@ def yaml_format(obj, indent=0):
|
||||||
has_multiple_items = len(items) > 2
|
has_multiple_items = len(items) > 2
|
||||||
if has_multiple_items:
|
if has_multiple_items:
|
||||||
result.append('\n')
|
result.append('\n')
|
||||||
indent += 2
|
indent += 2
|
||||||
for k, v in items:
|
for k, v in items:
|
||||||
if k == '_' or v is None:
|
if k == '_' or v is None:
|
||||||
continue
|
continue
|
||||||
|
@ -45,14 +45,11 @@ 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}:')
|
result.append(f'{k}: {formatted}')
|
||||||
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])
|
||||||
|
@ -79,6 +76,7 @@ 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)
|
||||||
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
"""
|
|
||||||
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