mirror of
https://github.com/Fluffy-Bean/Lynxie.git
synced 2025-05-29 06:43:14 +00:00
Add Images command
Clean up Animals command
This commit is contained in:
parent
68d638c7ee
commit
4e685393de
12 changed files with 283 additions and 27 deletions
|
@ -3,6 +3,7 @@ from .ping import Ping
|
|||
from .hello import Hello
|
||||
from .music import Music
|
||||
from .animals import Animals
|
||||
from .image import Img
|
||||
|
||||
__all__ = [
|
||||
"Help",
|
||||
|
@ -10,4 +11,5 @@ __all__ = [
|
|||
"Hello",
|
||||
"Music",
|
||||
"Animals",
|
||||
"Img",
|
||||
]
|
||||
|
|
|
@ -15,7 +15,6 @@ class Animals(commands.Cog):
|
|||
@commands.command()
|
||||
async def animal(self, ctx, animal):
|
||||
animal = animal.lower().strip() or "racc"
|
||||
animal_filename = f"{animal}.png"
|
||||
|
||||
if animal not in TINYFOX_ANIMALS:
|
||||
await ctx.reply(
|
||||
|
@ -27,17 +26,16 @@ class Animals(commands.Cog):
|
|||
return
|
||||
|
||||
async with ctx.typing():
|
||||
animal_image_request = requests.get(
|
||||
f"https://api.tinyfox.dev/img?animal={animal}"
|
||||
).content
|
||||
animal_image = BytesIO(animal_image_request)
|
||||
request = requests.get(f"https://api.tinyfox.dev/img?animal={animal}&json")
|
||||
animal_image = BytesIO(request.content)
|
||||
animal_image.seek(0)
|
||||
animal_file = discord.File(animal_image, filename=animal_filename)
|
||||
animal_file = discord.File(animal_image, filename="image.png")
|
||||
|
||||
embed = discord.Embed(
|
||||
title="Animal",
|
||||
description=f"Here's a random {animal}!",
|
||||
title=animal.capitalize(),
|
||||
colour=discord.Colour.orange(),
|
||||
).set_image(url="attachment://" + animal_filename)
|
||||
).set_image(
|
||||
url="attachment://image.png"
|
||||
)
|
||||
|
||||
await ctx.reply(embed=embed, file=animal_file, mention_author=False)
|
||||
|
|
|
@ -13,7 +13,8 @@ class Help(commands.Cog):
|
|||
"join": "Join the voice channel you're in",
|
||||
"play <url>": "Play a song from YouTube, SoundCloud, etc.",
|
||||
"stop": "Stop the current song and leave the voice channel",
|
||||
"e621": "Search e621.net",
|
||||
"animal <animal>": "Get a random image of an animal!",
|
||||
"overlay <style>": "Overlay an image with a style, e.g. `bubble`",
|
||||
}
|
||||
|
||||
@commands.command()
|
||||
|
|
110
lynxie/commands/image.py
Normal file
110
lynxie/commands/image.py
Normal file
|
@ -0,0 +1,110 @@
|
|||
import os
|
||||
import requests
|
||||
from io import BytesIO
|
||||
import datetime
|
||||
|
||||
from PIL import Image
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
from lynxie.config import IMAGE_EXTENSIONS, IMAGE_OVERLAYS, ASSETS_PATH
|
||||
from lynxie.utils import error_message
|
||||
|
||||
|
||||
class Img(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
self._overlays = {
|
||||
"bubble": Image.open(os.path.join(ASSETS_PATH, "bubble.png")).convert("RGBA"),
|
||||
"gang": Image.open(os.path.join(ASSETS_PATH, "gang.png")).convert("RGBA"),
|
||||
}
|
||||
|
||||
@commands.command()
|
||||
async def overlay(self, ctx, style: str = None):
|
||||
start_time = datetime.datetime.now()
|
||||
|
||||
style = style.lower().strip() if style else None
|
||||
|
||||
# Check if image should be processed
|
||||
async with ctx.typing():
|
||||
if not style or style not in IMAGE_OVERLAYS:
|
||||
error = "That is not a valid option! Valid options are:\n" \
|
||||
f"`{', '.join(IMAGE_OVERLAYS)}`"
|
||||
await ctx.reply(embed=error_message(error))
|
||||
return
|
||||
|
||||
if not ctx.message.attachments:
|
||||
error = "You need to attach an image to use this command!"
|
||||
await ctx.reply(embed=error_message(error))
|
||||
return
|
||||
|
||||
# Extracts file extension from filename
|
||||
if not ctx.message.attachments[0].filename.split(".")[-1].lower() in IMAGE_EXTENSIONS:
|
||||
error = "Unsupported file type! Supported file types are:\n" \
|
||||
f"`{', '.join(IMAGE_EXTENSIONS)}`"
|
||||
await ctx.reply(embed=error_message(error))
|
||||
return
|
||||
|
||||
if ctx.message.attachments[0].size > 8_000_000:
|
||||
error = "That image is too big! Please use an image that is less than 8MB."
|
||||
await ctx.reply(embed=error_message(error))
|
||||
return
|
||||
|
||||
if (
|
||||
not 0 < ctx.message.attachments[0].width <= 3500 or
|
||||
not 0 < ctx.message.attachments[0].height <= 3500
|
||||
):
|
||||
error = "Image must be at least 1x1 and under 3500x3500!"
|
||||
await ctx.reply(embed=error_message(error))
|
||||
return
|
||||
|
||||
# Process image
|
||||
async with ctx.typing():
|
||||
response = requests.get(ctx.message.attachments[0].url)
|
||||
message_attachment = Image.open(BytesIO(response.content))
|
||||
|
||||
if message_attachment.width < message_attachment.height:
|
||||
message_attachment.thumbnail((200, message_attachment.height))
|
||||
else:
|
||||
message_attachment.thumbnail((message_attachment.width, 200))
|
||||
|
||||
if style == "bubble":
|
||||
# The bubble is resized twice as for some reason .copy() doesn't work
|
||||
message_attachment.paste(
|
||||
self._overlays["bubble"].resize(
|
||||
(
|
||||
message_attachment.width,
|
||||
self._overlays["bubble"].height
|
||||
)
|
||||
),
|
||||
(0, 0),
|
||||
self._overlays["bubble"].resize(
|
||||
(
|
||||
message_attachment.width,
|
||||
self._overlays["bubble"].height
|
||||
)
|
||||
),
|
||||
)
|
||||
elif style == "gang":
|
||||
message_attachment.paste(
|
||||
self._overlays["gang"],
|
||||
(
|
||||
((message_attachment.width - self._overlays["gang"].width) // 2),
|
||||
(message_attachment.height - self._overlays["gang"].height)
|
||||
),
|
||||
self._overlays["gang"]
|
||||
)
|
||||
|
||||
message_file = BytesIO()
|
||||
message_attachment.save(message_file, format="PNG")
|
||||
message_file.seek(0)
|
||||
message_file = discord.File(message_file, filename="image.png")
|
||||
|
||||
time_taken = datetime.datetime.now() - start_time
|
||||
embed = discord.Embed(title=style.capitalize(), colour=discord.Colour.orange())\
|
||||
.set_image(url="attachment://image.png")\
|
||||
.set_footer(text=f"{message_attachment.width}x{message_attachment.height}, "
|
||||
f"{time_taken.microseconds / 1000}ms")
|
||||
|
||||
await ctx.reply(embed=embed, file=message_file, mention_author=False)
|
Loading…
Add table
Add a link
Reference in a new issue