Files
Syntrel/cogs/media/__init__.py

161 lines
5.6 KiB
Python
Raw Normal View History

import discord
from discord import app_commands
from discord.ext import commands
from discord.ext.commands import Context
2025-10-05 09:07:10 -04:00
from typing import Optional
from .download import download_command
from .mcquote import mcquote_command
2025-10-05 09:07:10 -04:00
from .img2gif import img2gif_command
2025-10-07 13:33:53 -04:00
from .tweety import tweety_command
from .tts import tts_command
def _require_group_prefix(context: Context) -> bool:
if getattr(context, "interaction", None):
return True
group = getattr(getattr(context, "cog", None), "qualified_name", "").lower()
if not group:
return True
prefix = context.prefix or ""
content = context.message.content.strip().lower()
return content.startswith(f"{prefix}{group} ")
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
@app_commands.allowed_installs(guilds=True, users=True)
class Media(commands.GroupCog, name="media"):
def __init__(self, bot) -> None:
self.bot = bot
super().__init__()
2025-10-07 13:33:53 -04:00
@commands.Cog.listener()
async def on_message(self, message: discord.Message):
"""Listen for bot mentions with 'tweety' command while replying to a message"""
if message.author.bot:
return
2025-11-02 23:32:52 -05:00
if (
self.bot.user in message.mentions
and message.reference
and message.reference.message_id
):
2025-10-07 13:33:53 -04:00
content = message.content.lower()
2025-11-02 23:32:52 -05:00
content_without_mention = (
content.replace(f"<@{self.bot.user.id}>", "")
.replace(f"<@!{self.bot.user.id}>", "")
.strip()
)
if content_without_mention.strip() == "tweety":
2025-10-07 13:33:53 -04:00
ctx = await self.bot.get_context(message)
await self.tweety(ctx)
2025-10-07 13:33:53 -04:00
@commands.group(name="media", invoke_without_command=True)
async def media_group(self, context: Context):
embed = discord.Embed(
title="Media Commands",
description="Use `.media <subcommand>` or `/media <subcommand>`.",
2025-11-02 23:32:52 -05:00
color=0x7289DA,
)
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
embed.add_field(
name="Available",
value="download, mcquote, img2gif, tweety, tts",
inline=False,
)
await context.send(embed=embed)
2025-10-05 09:07:10 -04:00
async def _invoke_hybrid(self, context: Context, name: str, *args, **kwargs):
if name == "download":
2025-11-02 23:32:52 -05:00
await self.download(context, url=kwargs.get("url", ""))
2025-10-05 09:07:10 -04:00
return
if name == "mcquote":
2025-11-02 23:32:52 -05:00
await self.mcquote(context, text=kwargs.get("text", ""))
2025-10-05 09:07:10 -04:00
return
if name == "img2gif":
2025-11-02 23:32:52 -05:00
await self.img2gif(context, attachment=kwargs.get("attachment"))
2025-10-05 09:07:10 -04:00
return
2025-10-07 13:33:53 -04:00
if name == "tweety":
await self.tweety(context)
2025-10-07 13:33:53 -04:00
return
if name == "tts":
2025-11-02 23:32:52 -05:00
await self.tts(context, text=kwargs.get("text"))
return
2025-10-05 09:07:10 -04:00
await context.send(f"Unknown media command: {name}")
@media_group.command(name="download")
async def media_group_download(self, context: Context, *, url: str):
await self._invoke_hybrid(context, "download", url=url)
@media_group.command(name="mcquote")
async def media_group_mcquote(self, context: Context, *, text: str):
await self._invoke_hybrid(context, "mcquote", text=text)
2025-10-05 09:07:10 -04:00
@media_group.command(name="img2gif")
2025-11-02 23:32:52 -05:00
async def media_group_img2gif(
self, context: Context, attachment: Optional[discord.Attachment] = None
):
2025-10-05 09:07:10 -04:00
await self._invoke_hybrid(context, "img2gif", attachment=attachment)
2025-10-07 13:33:53 -04:00
@media_group.command(name="tweety")
async def media_group_tweety(self, context: Context):
await self._invoke_hybrid(context, "tweety")
2025-10-07 13:33:53 -04:00
@media_group.command(name="tts")
async def media_group_tts(self, context: Context, *, text: str = None):
await self._invoke_hybrid(context, "tts", text=text)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="download",
description="Download a video from a URL using yt-dlp.",
)
async def download(self, context, *, url: str):
return await download_command()(self, context, url=url)
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="mcquote",
description="Generate a custom Minecraft quote image.",
)
async def mcquote(self, context, *, text: str):
return await mcquote_command()(self, context, text=text)
2025-10-05 09:07:10 -04:00
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="img2gif",
description="Convert an uploaded image to a GIF.",
)
async def img2gif(self, context, attachment: Optional[discord.Attachment] = None):
return await img2gif_command()(self, context, attachment=attachment)
2025-10-07 13:33:53 -04:00
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="tweety",
description="Convert a replied message to a tweet image.",
)
async def tweety(self, context):
return await tweety_command()(self, context)
2025-10-07 13:33:53 -04:00
@commands.check(_require_group_prefix)
@commands.hybrid_command(
name="tts",
description="Convert text to speech using Google Text-to-Speech.",
)
async def tts(self, context, text: str = None):
return await tts_command()(context, text=text)
2025-11-02 23:32:52 -05:00
async def setup(bot) -> None:
cog = Media(bot)
await bot.add_cog(cog)
bot.logger.info("Loaded extension 'media.download'")
bot.logger.info("Loaded extension 'media.mcquote'")
2025-10-05 09:07:10 -04:00
bot.logger.info("Loaded extension 'media.img2gif'")
2025-10-07 13:33:53 -04:00
bot.logger.info("Loaded extension 'media.tweety'")
bot.logger.info("Loaded extension 'media.tts'")