mirror of
https://github.com/Paillat-dev/flagger.git
synced 2026-01-02 01:06:21 +00:00
66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
|
|
# Copyright (c) NiceBots
|
||
|
|
# SPDX-License-Identifier: MIT
|
||
|
|
|
||
|
|
from typing import TYPE_CHECKING
|
||
|
|
|
||
|
|
import discord
|
||
|
|
from discord import ui
|
||
|
|
|
||
|
|
from renderer.flag import Flag
|
||
|
|
|
||
|
|
if TYPE_CHECKING:
|
||
|
|
from pycord_rest import App
|
||
|
|
|
||
|
|
from renderer.base import FlagRenderer
|
||
|
|
from renderer.manager import RendererManager
|
||
|
|
|
||
|
|
|
||
|
|
class FlagDisplayView(ui.DesignerView):
|
||
|
|
def __init__(self, image: discord.File) -> None:
|
||
|
|
container = ui.Container()
|
||
|
|
container.add_text("## Your Flag is Ready!")
|
||
|
|
container.add_gallery(discord.MediaGalleryItem(f"attachment://{image.filename}")) # ty:ignore[invalid-argument-type]
|
||
|
|
super().__init__(container, store=False)
|
||
|
|
|
||
|
|
|
||
|
|
class FlaggerCommands(discord.Cog):
|
||
|
|
def __init__(self, app: "App", manager: "RendererManager", renderer: "FlagRenderer") -> None:
|
||
|
|
self.app: App = app
|
||
|
|
self.manager: RendererManager = manager
|
||
|
|
self.renderer: FlagRenderer = renderer
|
||
|
|
super().__init__()
|
||
|
|
|
||
|
|
async def handle_flag_command(self, ctx: discord.ApplicationContext, image_url: str) -> None:
|
||
|
|
async with self.manager.render_context_manager(self.renderer.render, Flag(image_url)) as gif_path: # ty: ignore[invalid-argument-type]
|
||
|
|
file = discord.File(gif_path, filename=gif_path.name)
|
||
|
|
await ctx.respond(view=FlagDisplayView(file), files=[file])
|
||
|
|
|
||
|
|
flag = discord.SlashCommandGroup("flag", "Commands related to flag rendering.")
|
||
|
|
|
||
|
|
@flag.command(name="user", description="Render a user's flag.")
|
||
|
|
async def user(self, ctx: discord.ApplicationContext, user: discord.Member | None = None) -> None:
|
||
|
|
target = user or ctx.author
|
||
|
|
if target.display_avatar.is_animated():
|
||
|
|
asset = target.display_avatar.with_format("gif")
|
||
|
|
else:
|
||
|
|
asset = target.display_avatar.with_format("png")
|
||
|
|
await ctx.defer()
|
||
|
|
|
||
|
|
await self.handle_flag_command(ctx, asset.url)
|
||
|
|
|
||
|
|
@flag.command(name="custom", description="Render a custom flag from an image attachment.")
|
||
|
|
async def custom_flag(self, ctx: discord.ApplicationContext, attachment: discord.Attachment) -> None:
|
||
|
|
if not attachment.content_type or not attachment.content_type.startswith("image/"):
|
||
|
|
await ctx.respond("Please provide a valid image attachment.", ephemeral=True)
|
||
|
|
return
|
||
|
|
|
||
|
|
if attachment.content_type not in {"image/gif", "image/png", "image/jpeg"}:
|
||
|
|
await ctx.respond("Unsupported image format. Please provide a PNG, JPEG, or GIF image.", ephemeral=True)
|
||
|
|
return
|
||
|
|
|
||
|
|
await ctx.defer()
|
||
|
|
await self.handle_flag_command(ctx, attachment.url)
|
||
|
|
|
||
|
|
|
||
|
|
__all__ = ("FlaggerCommands",)
|