142 lines
5.0 KiB
Python
142 lines
5.0 KiB
Python
from core.module import Module
|
|
|
|
|
|
class MediaModule(Module):
|
|
name = "media"
|
|
version = "0.1.0"
|
|
description = "Media handling commands"
|
|
optional_dependencies = ["lib:Pillow"]
|
|
|
|
async def on_load(self) -> None:
|
|
builder = self.commands
|
|
|
|
@builder.command(
|
|
name="dl",
|
|
description="Stub download command",
|
|
category="media",
|
|
usage=".dl <url>",
|
|
example=".dl https://...",
|
|
)
|
|
async def download_cmd(event, args):
|
|
message = getattr(event, "message", None)
|
|
if not message or not getattr(message, "reply_to_msg_id", None):
|
|
await event.reply("Reply to media to download")
|
|
return
|
|
reply = await event.get_reply_message()
|
|
if not reply or not reply.media:
|
|
await event.reply("No media found")
|
|
return
|
|
settings = self.get_config().get("settings", {})
|
|
download_path = settings.get("download_path", "data/downloads")
|
|
path = await event.client.download_media(reply, file=download_path)
|
|
await event.reply(f"Downloaded: {path}")
|
|
|
|
@builder.command(
|
|
name="convert",
|
|
description="Stub media convert",
|
|
category="media",
|
|
usage=".convert <format>",
|
|
)
|
|
async def convert_cmd(event, args):
|
|
try:
|
|
from PIL import Image
|
|
except ImportError:
|
|
await event.reply("Pillow not installed")
|
|
return
|
|
if not args:
|
|
await event.reply("Format required")
|
|
return
|
|
fmt = args[0].upper()
|
|
reply = await event.get_reply_message()
|
|
if not reply or not reply.media:
|
|
await event.reply("Reply to an image")
|
|
return
|
|
temp = await event.client.download_media(reply)
|
|
img = Image.open(temp)
|
|
output = f"{temp}.{fmt.lower()}"
|
|
img.save(output, fmt)
|
|
await event.reply(file=output)
|
|
|
|
@builder.command(
|
|
name="resize",
|
|
description="Stub media resize",
|
|
category="media",
|
|
usage=".resize <width> <height>",
|
|
)
|
|
async def resize_cmd(event, args):
|
|
try:
|
|
from PIL import Image
|
|
except ImportError:
|
|
await event.reply("Pillow not installed")
|
|
return
|
|
if len(args) < 2:
|
|
await event.reply("Usage: .resize <width> <height>")
|
|
return
|
|
reply = await event.get_reply_message()
|
|
if not reply or not reply.media:
|
|
await event.reply("Reply to an image")
|
|
return
|
|
temp = await event.client.download_media(reply)
|
|
img = Image.open(temp)
|
|
img = img.resize((int(args[0]), int(args[1])))
|
|
output = f"{temp}_resized.png"
|
|
img.save(output, "PNG")
|
|
await event.reply(file=output)
|
|
|
|
@builder.command(
|
|
name="compress",
|
|
description="Stub media compression",
|
|
category="media",
|
|
usage=".compress",
|
|
)
|
|
async def compress_cmd(event, args):
|
|
try:
|
|
from PIL import Image
|
|
except ImportError:
|
|
await event.reply("Pillow not installed")
|
|
return
|
|
reply = await event.get_reply_message()
|
|
if not reply or not reply.media:
|
|
await event.reply("Reply to an image")
|
|
return
|
|
temp = await event.client.download_media(reply)
|
|
img = Image.open(temp)
|
|
output = f"{temp}_compressed.jpg"
|
|
img.save(output, "JPEG", quality=60)
|
|
await event.reply(file=output)
|
|
|
|
@builder.command(
|
|
name="sticker",
|
|
description="Stub sticker tools",
|
|
category="media",
|
|
usage=".sticker",
|
|
)
|
|
async def sticker_cmd(event, args):
|
|
try:
|
|
from PIL import Image
|
|
except ImportError:
|
|
await event.reply("Pillow not installed")
|
|
return
|
|
reply = await event.get_reply_message()
|
|
if not reply or not reply.media:
|
|
await event.reply("Reply to an image")
|
|
return
|
|
temp = await event.client.download_media(reply)
|
|
img = Image.open(temp).convert("RGBA")
|
|
output = f"{temp}.webp"
|
|
img.save(output, "WEBP")
|
|
await event.reply(file=output)
|
|
|
|
@builder.command(
|
|
name="gif",
|
|
description="Stub GIF tools",
|
|
category="media",
|
|
usage=".gif",
|
|
)
|
|
async def gif_cmd(event, args):
|
|
reply = await event.get_reply_message()
|
|
if not reply or not reply.media:
|
|
await event.reply("Reply to a video or animation")
|
|
return
|
|
await event.reply(file=reply, force_document=False)
|