chore: ruff formatting

This commit is contained in:
neoarz
2025-11-02 23:32:52 -05:00
parent 2ce2c69a87
commit 1eff6c9f53
91 changed files with 3824 additions and 2518 deletions

View File

@@ -34,12 +34,20 @@ class Media(commands.GroupCog, name="media"):
"""Listen for bot mentions with 'tweety' command while replying to a message"""
if message.author.bot:
return
if self.bot.user in message.mentions and message.reference and message.reference.message_id:
if (
self.bot.user in message.mentions
and message.reference
and message.reference.message_id
):
content = message.content.lower()
content_without_mention = content.replace(f'<@{self.bot.user.id}>', '').replace(f'<@!{self.bot.user.id}>', '').strip()
if content_without_mention.strip() == 'tweety':
content_without_mention = (
content.replace(f"<@{self.bot.user.id}>", "")
.replace(f"<@!{self.bot.user.id}>", "")
.strip()
)
if content_without_mention.strip() == "tweety":
ctx = await self.bot.get_context(message)
await self.tweety(ctx)
@@ -48,27 +56,33 @@ class Media(commands.GroupCog, name="media"):
embed = discord.Embed(
title="Media Commands",
description="Use `.media <subcommand>` or `/media <subcommand>`.",
color=0x7289DA
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,
)
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)
async def _invoke_hybrid(self, context: Context, name: str, *args, **kwargs):
if name == "download":
await self.download(context, url=kwargs.get('url', ''))
await self.download(context, url=kwargs.get("url", ""))
return
if name == "mcquote":
await self.mcquote(context, text=kwargs.get('text', ''))
await self.mcquote(context, text=kwargs.get("text", ""))
return
if name == "img2gif":
await self.img2gif(context, attachment=kwargs.get('attachment'))
await self.img2gif(context, attachment=kwargs.get("attachment"))
return
if name == "tweety":
await self.tweety(context)
return
if name == "tts":
await self.tts(context, text=kwargs.get('text'))
await self.tts(context, text=kwargs.get("text"))
return
await context.send(f"Unknown media command: {name}")
@@ -81,7 +95,9 @@ class Media(commands.GroupCog, name="media"):
await self._invoke_hybrid(context, "mcquote", text=text)
@media_group.command(name="img2gif")
async def media_group_img2gif(self, context: Context, attachment: Optional[discord.Attachment] = None):
async def media_group_img2gif(
self, context: Context, attachment: Optional[discord.Attachment] = None
):
await self._invoke_hybrid(context, "img2gif", attachment=attachment)
@media_group.command(name="tweety")
@@ -132,6 +148,7 @@ class Media(commands.GroupCog, name="media"):
async def tts(self, context, text: str = None):
return await tts_command()(context, text=text)
async def setup(bot) -> None:
cog = Media(bot)
await bot.add_cog(cog)

View File

@@ -10,6 +10,7 @@ import logging
logger = logging.getLogger("discord_bot")
def download_command():
@commands.hybrid_command(
name="download",
@@ -23,8 +24,10 @@ def download_command():
description="This command can only be used in servers.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -41,8 +44,10 @@ def download_command():
description="The bot needs the `send messages` permission in this channel.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -52,15 +57,17 @@ def download_command():
else:
await context.send(embed=embed, ephemeral=True)
return
if not url:
embed = discord.Embed(
title="Error",
description="Please provide a valid URL to download.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -74,7 +81,9 @@ def download_command():
# Check if bot has send messages permission before starting download
try:
test_embed = discord.Embed(title="Testing permissions...", color=0x7289DA)
test_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
test_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.channel.send(embed=test_embed, delete_after=0.1)
except discord.Forbidden:
embed = discord.Embed(
@@ -82,8 +91,10 @@ def download_command():
description="The bot needs the `send messages` permission to execute this command.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -102,12 +113,16 @@ def download_command():
description="Please provide a valid URL.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
await interaction.response.send_message(embed=embed, ephemeral=True)
await interaction.response.send_message(
embed=embed, ephemeral=True
)
else:
await interaction.followup.send(embed=embed, ephemeral=True)
else:
@@ -119,8 +134,10 @@ def download_command():
description="Please provide a valid URL.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -136,35 +153,41 @@ def download_command():
description="<a:mariospin:1423677027013103709> Downloading video... This may take a moment.",
color=0x7289DA,
)
processing_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
processing_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
await interaction.response.send_message(embed=processing_embed, ephemeral=True)
await interaction.response.send_message(
embed=processing_embed, ephemeral=True
)
else:
await interaction.followup.send(embed=processing_embed, ephemeral=True)
else:
processing_msg = await context.send(embed=processing_embed)
temp_dir = tempfile.mkdtemp()
# Try Docker path first, fallback to local path for development
cookie_path = '/bot/cogs/media/files/cookies.txt'
cookie_path = "/bot/cogs/media/files/cookies.txt"
if not os.path.exists(cookie_path):
cookie_path = os.path.join(os.path.dirname(__file__), 'files', 'cookies.txt')
cookie_path = os.path.join(
os.path.dirname(__file__), "files", "cookies.txt"
)
ydl_opts = {
'format': 'bestvideo[filesize<200M]+bestaudio[filesize<200M]/best[filesize<200M]/bestvideo+bestaudio/best',
'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'),
'noplaylist': True,
'extract_flat': False,
'writesubtitles': False,
'writeautomaticsub': False,
'writethumbnail': False,
'ignoreerrors': False,
'merge_output_format': 'mp4',
'cookiefile': cookie_path,
"format": "bestvideo[filesize<200M]+bestaudio[filesize<200M]/best[filesize<200M]/bestvideo+bestaudio/best",
"outtmpl": os.path.join(temp_dir, "%(title)s.%(ext)s"),
"noplaylist": True,
"extract_flat": False,
"writesubtitles": False,
"writeautomaticsub": False,
"writethumbnail": False,
"ignoreerrors": False,
"merge_output_format": "mp4",
"cookiefile": cookie_path,
}
try:
@@ -172,63 +195,103 @@ def download_command():
info = await asyncio.get_event_loop().run_in_executor(
None, lambda: ydl.extract_info(url, download=True)
)
if not info:
raise Exception("Could not extract video information")
video_title = info.get('title', 'Unknown Title')
video_duration_seconds = int(info.get('duration') or 0)
video_uploader = info.get('uploader', 'Unknown')
video_url = info.get('webpage_url') or info.get('original_url') or url
platform = info.get('extractor') or info.get('extractor_key') or 'Unknown'
view_count = info.get('view_count')
files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))]
video_title = info.get("title", "Unknown Title")
video_duration_seconds = int(info.get("duration") or 0)
video_uploader = info.get("uploader", "Unknown")
video_url = info.get("webpage_url") or info.get("original_url") or url
platform = (
info.get("extractor") or info.get("extractor_key") or "Unknown"
)
view_count = info.get("view_count")
files = [
f
for f in os.listdir(temp_dir)
if os.path.isfile(os.path.join(temp_dir, f))
]
if not files:
raise Exception("No video file was downloaded")
video_file = os.path.join(temp_dir, files[0])
file_size = os.path.getsize(video_file)
logger.info(f"File size: {file_size} bytes ({file_size / (1024*1024):.2f} MB)")
logger.info(
f"File size: {file_size} bytes ({file_size / (1024 * 1024):.2f} MB)"
)
if file_size > 24 * 1024 * 1024: # 24MB limit
logger.info("File is over 24MB, uploading to Catbox")
async def upload_to_catbox(path: str) -> str:
try:
file_size_bytes = os.path.getsize(path)
except Exception:
file_size_bytes = -1
logger.info(f"Catbox upload start: name={os.path.basename(path)} size={file_size_bytes}")
logger.info(
f"Catbox upload start: name={os.path.basename(path)} size={file_size_bytes}"
)
form = aiohttp.FormData()
form.add_field('reqtype', 'fileupload')
form.add_field('fileToUpload', open(path, 'rb'), filename=os.path.basename(path))
form.add_field("reqtype", "fileupload")
form.add_field(
"fileToUpload",
open(path, "rb"),
filename=os.path.basename(path),
)
timeout = aiohttp.ClientTimeout(total=600)
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post('https://catbox.moe/user/api.php', data=form) as resp:
async with session.post(
"https://catbox.moe/user/api.php", data=form
) as resp:
text = await resp.text()
logger.info(f"Catbox response: status={resp.status} body_len={len(text)}")
if resp.status == 200 and text.startswith('https://'):
logger.info(
f"Catbox response: status={resp.status} body_len={len(text)}"
)
if resp.status == 200 and text.startswith("https://"):
url_text = text.strip()
logger.info(f"Catbox upload success: url={url_text}")
logger.info(
f"Catbox upload success: url={url_text}"
)
return url_text
logger.error(f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}")
logger.error(
f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}"
)
raise RuntimeError(f"Upload failed: {text.strip()}")
try:
link = await upload_to_catbox(video_file)
minutes, seconds = divmod(video_duration_seconds, 60)
duration_str = f"{minutes}:{seconds:02d}"
description_text = f"### **[{video_title}]({video_url})**" if video_url else f"### **{video_title}**"
description_text = (
f"### **[{video_title}]({video_url})**"
if video_url
else f"### **{video_title}**"
)
embed = discord.Embed(
title="Download",
description=description_text,
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.add_field(name="Uploader", value=video_uploader or "Unknown", inline=True)
embed.add_field(name="Duration", value=duration_str, inline=True)
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
embed.add_field(
name="Uploader",
value=video_uploader or "Unknown",
inline=True,
)
embed.add_field(
name="Duration", value=duration_str, inline=True
)
embed.add_field(name="Platform", value=platform, inline=True)
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
embed.set_footer(
text=f"Requested by {context.author.name}",
icon_url=context.author.display_avatar.url,
)
if interaction is not None:
await context.channel.send(embed=embed)
@@ -249,13 +312,16 @@ def download_command():
description = "The video is too large to upload. The file exceeds 200MB (Catbox limit) and cannot be sent via Discord (25MB limit)."
else:
description = f"The video is over 25MB and upload to hosting failed: {upload_error}"
embed = discord.Embed(
title="Error",
description=description,
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
if interaction is not None:
try:
@@ -271,22 +337,34 @@ def download_command():
logger.info("File is under 24MB, sending directly to Discord")
minutes, seconds = divmod(video_duration_seconds, 60)
duration_str = f"{minutes}:{seconds:02d}"
description_text = f"### **[{video_title}]({video_url})**" if video_url else f"### **{video_title}**"
description_text = (
f"### **[{video_title}]({video_url})**"
if video_url
else f"### **{video_title}**"
)
embed = discord.Embed(
title="Download",
description=description_text,
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.add_field(name="Uploader", value=video_uploader or "Unknown", inline=True)
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
embed.add_field(
name="Uploader", value=video_uploader or "Unknown", inline=True
)
embed.add_field(name="Duration", value=duration_str, inline=True)
embed.add_field(name="Platform", value=platform, inline=True)
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
embed.set_footer(
text=f"Requested by {context.author.name}",
icon_url=context.author.display_avatar.url,
)
try:
with open(video_file, 'rb') as f:
with open(video_file, "rb") as f:
file = discord.File(f, filename=files[0])
if interaction is not None:
await context.channel.send(embed=embed)
await context.channel.send(file=file)
@@ -300,40 +378,82 @@ def download_command():
await context.channel.send(file=file)
except discord.HTTPException as e:
if e.status == 413:
logger.info("Discord rejected file (413), falling back to Catbox upload")
logger.info(
"Discord rejected file (413), falling back to Catbox upload"
)
async def upload_to_catbox(path: str) -> str:
try:
file_size_bytes = os.path.getsize(path)
except Exception:
file_size_bytes = -1
logger.info(f"Catbox upload start: name={os.path.basename(path)} size={file_size_bytes}")
logger.info(
f"Catbox upload start: name={os.path.basename(path)} size={file_size_bytes}"
)
form = aiohttp.FormData()
form.add_field('reqtype', 'fileupload')
form.add_field('fileToUpload', open(path, 'rb'), filename=os.path.basename(path))
form.add_field("reqtype", "fileupload")
form.add_field(
"fileToUpload",
open(path, "rb"),
filename=os.path.basename(path),
)
timeout = aiohttp.ClientTimeout(total=600)
async with aiohttp.ClientSession(timeout=timeout) as session:
async with session.post('https://catbox.moe/user/api.php', data=form) as resp:
async with aiohttp.ClientSession(
timeout=timeout
) as session:
async with session.post(
"https://catbox.moe/user/api.php", data=form
) as resp:
text = await resp.text()
logger.info(f"Catbox response: status={resp.status} body_len={len(text)}")
if resp.status == 200 and text.startswith('https://'):
logger.info(
f"Catbox response: status={resp.status} body_len={len(text)}"
)
if resp.status == 200 and text.startswith(
"https://"
):
url_text = text.strip()
logger.info(f"Catbox upload success: url={url_text}")
logger.info(
f"Catbox upload success: url={url_text}"
)
return url_text
logger.error(f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}")
raise RuntimeError(f"Upload failed: {text.strip()}")
logger.error(
f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}"
)
raise RuntimeError(
f"Upload failed: {text.strip()}"
)
try:
link = await upload_to_catbox(video_file)
description_text_with_link = f"### **[{video_title}]({video_url})**\n\n{link}" if video_url else f"### **{video_title}**\n\n{link}"
description_text_with_link = (
f"### **[{video_title}]({video_url})**\n\n{link}"
if video_url
else f"### **{video_title}**\n\n{link}"
)
embed = discord.Embed(
title="Download",
description=description_text,
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.add_field(name="Uploader", value=video_uploader or "Unknown", inline=True)
embed.add_field(name="Duration", value=duration_str, inline=True)
embed.add_field(name="Platform", value=platform, inline=True)
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
embed.add_field(
name="Uploader",
value=video_uploader or "Unknown",
inline=True,
)
embed.add_field(
name="Duration", value=duration_str, inline=True
)
embed.add_field(
name="Platform", value=platform, inline=True
)
embed.set_footer(
text=f"Requested by {context.author.name}",
icon_url=context.author.display_avatar.url,
)
if interaction is not None:
await context.channel.send(embed=embed)
@@ -347,33 +467,42 @@ def download_command():
await context.channel.send(embed=embed)
await context.channel.send(link)
except Exception as upload_error:
logger.exception(f"Catbox upload exception: {upload_error}")
logger.exception(
f"Catbox upload exception: {upload_error}"
)
embed = discord.Embed(
title="Error",
description=f"Discord rejected the file and Catbox upload failed: {upload_error}",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
if interaction is not None:
try:
await interaction.delete_original_response()
except:
pass
await interaction.followup.send(embed=embed, ephemeral=True)
await interaction.followup.send(
embed=embed, ephemeral=True
)
else:
await processing_msg.delete()
await context.send(embed=embed, ephemeral=True)
else:
raise e
except Exception as e:
embed = discord.Embed(
title="Error",
description=f"Failed to download video: {str(e)}",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
if interaction is not None:
try:
@@ -387,7 +516,7 @@ def download_command():
except:
pass
await context.send(embed=embed, ephemeral=True)
finally:
for file in os.listdir(temp_dir):
try:
@@ -398,5 +527,5 @@ def download_command():
os.rmdir(temp_dir)
except:
pass
return download
return download

View File

@@ -6,8 +6,10 @@ from PIL import Image
import subprocess
import shutil
from typing import Optional
try:
import pillow_heif
pillow_heif.register_heif_opener()
except Exception:
pass
@@ -20,7 +22,7 @@ async def send_error_message(context, description: str):
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -30,6 +32,7 @@ async def send_error_message(context, description: str):
else:
await context.send(embed=embed, ephemeral=True)
def img2gif_command():
@commands.hybrid_command(
name="img2gif",
@@ -39,14 +42,30 @@ def img2gif_command():
async def img2gif(self, context, attachment: Optional[discord.Attachment] = None):
resolved_attachment = attachment
if resolved_attachment is None:
if context.message and context.message.reference and context.message.reference.resolved:
if (
context.message
and context.message.reference
and context.message.reference.resolved
):
ref_msg = context.message.reference.resolved
if isinstance(ref_msg, discord.Message) and ref_msg.attachments:
resolved_attachment = ref_msg.attachments[0]
if resolved_attachment is None and context.message and context.message.attachments:
if (
resolved_attachment is None
and context.message
and context.message.attachments
):
resolved_attachment = context.message.attachments[0]
if resolved_attachment is None or not resolved_attachment.filename.lower().endswith((".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff", ".heic", ".heif")):
await send_error_message(context, "Provide or reply to an image (png/jpg/jpeg/webp/bmp/tiff/heic/heif).")
if (
resolved_attachment is None
or not resolved_attachment.filename.lower().endswith(
(".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tiff", ".heic", ".heif")
)
):
await send_error_message(
context,
"Provide or reply to an image (png/jpg/jpeg/webp/bmp/tiff/heic/heif).",
)
return
interaction = getattr(context, "interaction", None)
@@ -59,12 +78,16 @@ def img2gif_command():
description="<a:mariospin:1423677027013103709> Converting image...",
color=0x7289DA,
)
processing_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
processing_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
processing_msg = await context.send(embed=processing_embed)
tmp_dir = tempfile.mkdtemp()
src_path = os.path.join(tmp_dir, resolved_attachment.filename)
out_path = os.path.join(tmp_dir, os.path.splitext(resolved_attachment.filename)[0] + ".gif")
out_path = os.path.join(
tmp_dir, os.path.splitext(resolved_attachment.filename)[0] + ".gif"
)
try:
await resolved_attachment.save(src_path)
@@ -75,20 +98,42 @@ def img2gif_command():
img = img.convert("RGBA")
duration_ms = 100
loop = 0
img.save(out_path, format="GIF", save_all=True, optimize=True, duration=duration_ms, loop=loop)
img.save(
out_path,
format="GIF",
save_all=True,
optimize=True,
duration=duration_ms,
loop=loop,
)
except Exception:
if resolved_attachment.filename.lower().endswith((".heic", ".heif")) and shutil.which("ffmpeg"):
png_path = os.path.join(tmp_dir, os.path.splitext(resolved_attachment.filename)[0] + ".png")
if resolved_attachment.filename.lower().endswith(
(".heic", ".heif")
) and shutil.which("ffmpeg"):
png_path = os.path.join(
tmp_dir,
os.path.splitext(resolved_attachment.filename)[0] + ".png",
)
try:
subprocess.run([
"ffmpeg", "-y", "-i", src_path, png_path
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
subprocess.run(
["ffmpeg", "-y", "-i", src_path, png_path],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
with Image.open(png_path) as img:
if img.mode not in ("RGB", "RGBA"):
img = img.convert("RGBA")
duration_ms = 100
loop = 0
img.save(out_path, format="GIF", save_all=True, optimize=True, duration=duration_ms, loop=loop)
img.save(
out_path,
format="GIF",
save_all=True,
optimize=True,
duration=duration_ms,
loop=loop,
)
except Exception as conv_err:
raise conv_err
else:
@@ -103,18 +148,30 @@ def img2gif_command():
await context.send(file=file)
except Exception as e:
if interaction is not None:
await interaction.followup.send(embed=discord.Embed(
title="Error",
description=f"Failed to convert image: {e}",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
await interaction.followup.send(
embed=discord.Embed(
title="Error",
description=f"Failed to convert image: {e}",
color=0xE02B2B,
).set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
),
ephemeral=True,
)
else:
await processing_msg.delete()
await context.send(embed=discord.Embed(
title="Error",
description=f"Failed to convert image: {e}",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"), ephemeral=True)
await context.send(
embed=discord.Embed(
title="Error",
description=f"Failed to convert image: {e}",
color=0xE02B2B,
).set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
),
ephemeral=True,
)
finally:
try:
for f in os.listdir(tmp_dir):
@@ -127,5 +184,3 @@ def img2gif_command():
pass
return img2gif

View File

@@ -6,6 +6,7 @@ from discord.ext import commands
import aiohttp
import random
def mcquote_command():
@commands.hybrid_command(
name="mcquote",
@@ -19,8 +20,10 @@ def mcquote_command():
description="This command can only be used in servers.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -37,8 +40,10 @@ def mcquote_command():
description="The bot needs the `send messages` permission in this channel.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -55,8 +60,10 @@ def mcquote_command():
description="Please provide text for the Minecraft quote.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -73,8 +80,10 @@ def mcquote_command():
description="Text must be 25 characters or less.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -88,7 +97,9 @@ def mcquote_command():
# Check if bot has send messages permission before starting quote generation
try:
test_embed = discord.Embed(title="Testing permissions...", color=0x7289DA)
test_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
test_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.channel.send(embed=test_embed, delete_after=0.1)
except discord.Forbidden:
embed = discord.Embed(
@@ -96,8 +107,10 @@ def mcquote_command():
description="The bot needs the `send messages` permission to execute this command.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -113,12 +126,16 @@ def mcquote_command():
description="<a:mariospin:1423677027013103709> Generating quote... This may take a moment.",
color=0x7289DA,
)
processing_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
processing_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
await interaction.response.send_message(embed=processing_embed, ephemeral=True)
await interaction.response.send_message(
embed=processing_embed, ephemeral=True
)
else:
await interaction.followup.send(embed=processing_embed, ephemeral=True)
else:
@@ -126,7 +143,7 @@ def mcquote_command():
quote_text = text.replace(" ", "+")
random_number = random.randint(1, 39)
mc_quote_url = f'https://skinmc.net/achievement/{random_number}/Achievement+Unlocked!/{quote_text}'
mc_quote_url = f"https://skinmc.net/achievement/{random_number}/Achievement+Unlocked!/{quote_text}"
try:
async with aiohttp.ClientSession() as session:
@@ -142,12 +159,17 @@ def mcquote_command():
title="Minecraft Quote",
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_footer(text=f"Requested by {context.author.name}", icon_url=context.author.display_avatar.url)
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
embed.set_footer(
text=f"Requested by {context.author.name}",
icon_url=context.author.display_avatar.url,
)
with open(temp_file_path, 'rb') as f:
with open(temp_file_path, "rb") as f:
file = discord.File(f, filename="mcquote.png")
interaction = getattr(context, "interaction", None)
if interaction is not None:
await context.channel.send(embed=embed)
@@ -168,8 +190,10 @@ def mcquote_command():
description="Failed to generate Minecraft quote. Please try again later.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
try:
@@ -186,8 +210,10 @@ def mcquote_command():
description=f"An unexpected error occurred: {str(e)}",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
try:
@@ -198,5 +224,5 @@ def mcquote_command():
else:
await processing_msg.delete()
await context.send(embed=embed, ephemeral=True)
return mcquote
return mcquote

View File

@@ -11,8 +11,8 @@ from gtts import gTTS
DEFAULT_LANG = "en"
def tts_command():
def tts_command():
async def send_embed(
context: commands.Context,
embed: discord.Embed,
@@ -24,14 +24,20 @@ def tts_command():
if interaction is not None:
if interaction.response.is_done():
if file:
await interaction.followup.send(embed=embed, file=file, ephemeral=ephemeral)
await interaction.followup.send(
embed=embed, file=file, ephemeral=ephemeral
)
else:
await interaction.followup.send(embed=embed, ephemeral=ephemeral)
else:
if file:
await interaction.response.send_message(embed=embed, file=file, ephemeral=ephemeral)
await interaction.response.send_message(
embed=embed, file=file, ephemeral=ephemeral
)
else:
await interaction.response.send_message(embed=embed, ephemeral=ephemeral)
await interaction.response.send_message(
embed=embed, ephemeral=ephemeral
)
else:
if file:
await context.send(embed=embed, file=file)
@@ -42,8 +48,7 @@ def tts_command():
try:
loop = asyncio.get_event_loop()
audio_bytes = await loop.run_in_executor(
None,
lambda: _generate_tts_sync(text)
None, lambda: _generate_tts_sync(text)
)
return audio_bytes, None
except Exception as e:
@@ -70,8 +75,10 @@ def tts_command():
description="This command can only be used in servers.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -88,8 +95,10 @@ def tts_command():
description="The bot needs the `send messages` permission in this channel.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -101,29 +110,33 @@ def tts_command():
return
if not text or not text.strip():
if context.message and context.message.reference and context.message.reference.resolved:
if (
context.message
and context.message.reference
and context.message.reference.resolved
):
referenced = context.message.reference.resolved
if isinstance(referenced, discord.Message) and referenced.content:
text = referenced.content
if not text or not text.strip():
embed = (
discord.Embed(
title="Error",
description="Please provide text to convert or reply to a message containing text.",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed = discord.Embed(
title="Error",
description="Please provide text to convert or reply to a message containing text.",
color=0xE02B2B,
).set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await send_embed(context, embed, ephemeral=True)
return
text = text.strip()
if len(text) > 500:
embed = (
discord.Embed(
title="Error",
description="Text is too long. Please limit to 500 characters.",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed = discord.Embed(
title="Error",
description="Text is too long. Please limit to 500 characters.",
color=0xE02B2B,
).set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await send_embed(context, embed, ephemeral=True)
return
@@ -131,7 +144,9 @@ def tts_command():
# Check if bot has send messages permission before starting TTS generation
try:
test_embed = discord.Embed(title="Testing permissions...", color=0x7289DA)
test_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
test_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.channel.send(embed=test_embed, delete_after=0.1)
except discord.Forbidden:
embed = discord.Embed(
@@ -139,8 +154,10 @@ def tts_command():
description="The bot needs the `send messages` permission to execute this command.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
interaction = getattr(context, "interaction", None)
if interaction is not None:
if not interaction.response.is_done():
@@ -151,43 +168,45 @@ def tts_command():
await context.send(embed=embed, ephemeral=True)
return
processing_embed = (
discord.Embed(
title="TTS (Processing)",
description="<a:mariospin:1423677027013103709> Generating speech...",
color=0x7289DA,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
)
processing_embed = discord.Embed(
title="TTS (Processing)",
description="<a:mariospin:1423677027013103709> Generating speech...",
color=0x7289DA,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
interaction = getattr(context, "interaction", None)
processing_message = None
sent_initial_interaction_response = False
if interaction is not None:
if interaction.response.is_done():
processing_message = await interaction.followup.send(embed=processing_embed, ephemeral=True)
processing_message = await interaction.followup.send(
embed=processing_embed, ephemeral=True
)
else:
await interaction.response.send_message(embed=processing_embed, ephemeral=True)
await interaction.response.send_message(
embed=processing_embed, ephemeral=True
)
sent_initial_interaction_response = True
if not interaction.response.is_done():
await interaction.response.defer(ephemeral=False)
else:
processing_embed = (
discord.Embed(
title="TTS (Processing)",
description="<a:mariospin:1423677027013103709> Generating speech...",
color=0x7289DA,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
processing_embed = discord.Embed(
title="TTS (Processing)",
description="<a:mariospin:1423677027013103709> Generating speech...",
color=0x7289DA,
).set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
processing_message = await context.send(embed=processing_embed)
audio_bytes, error = await generate_tts_audio(text)
if error or not audio_bytes:
embed = (
discord.Embed(
title="Error",
description=f"Failed to generate speech. {error or 'Unknown error.'}",
color=0xE02B2B,
).set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed = discord.Embed(
title="Error",
description=f"Failed to generate speech. {error or 'Unknown error.'}",
color=0xE02B2B,
).set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await send_embed(context, embed, ephemeral=True)
if interaction is not None and sent_initial_interaction_response:
@@ -213,7 +232,9 @@ def tts_command():
description=f"**Input:** {text}",
color=0x7289DA,
)
.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
.set_footer(
text=f"Requested by {context.author.display_name}",
icon_url=getattr(context.author.display_avatar, "url", None),
@@ -232,4 +253,4 @@ def tts_command():
await context.channel.send(embed=embed)
await context.channel.send(file=audio_file)
return tts
return tts

View File

@@ -16,22 +16,25 @@ import asyncio
def break_long_words(text: str, max_word_length: int = 50) -> str:
words = text.split(' ')
words = text.split(" ")
result = []
for word in words:
if len(word) > max_word_length:
chunks = [word[i:i+max_word_length] for i in range(0, len(word), max_word_length)]
result.append(' '.join(chunks))
chunks = [
word[i : i + max_word_length]
for i in range(0, len(word), max_word_length)
]
result.append(" ".join(chunks))
else:
result.append(word)
return ' '.join(result)
return " ".join(result)
class TweetyHelpView(discord.ui.View):
"""This is the help view for the slash command cuz only the pinging and prefix versions can use this command"""
def __init__(self, user_id: int, bot):
super().__init__(timeout=180)
self.user_id = user_id
@@ -41,22 +44,30 @@ class TweetyHelpView(discord.ui.View):
{
"title": "Tweety (Method 1)",
"description": "Use the prefix command `.media tweety` while replying to a message.",
"gif_url": "https://yes.nighty.works/raw/VrKX1L.gif",
"gif_url": "https://yes.nighty.works/raw/VrKX1L.gif",
"fields": [
{"name": "How to use", "value": "1. Reply to any message\n2. Type `.media tweety`\n3. Use the buttons to customize!", "inline": False},
]
{
"name": "How to use",
"value": "1. Reply to any message\n2. Type `.media tweety`\n3. Use the buttons to customize!",
"inline": False,
},
],
},
{
"title": "Tweety (Method 2)",
"description": f"Mention <@{bot.user.id}> with `tweety` while replying to a message.",
"gif_url": "https://yes.nighty.works/raw/9XEe9j.gif",
"gif_url": "https://yes.nighty.works/raw/9XEe9j.gif",
"fields": [
{"name": "How to use", "value": f"1. Reply to any message\n2. Type `<@{bot.user.id}> tweety`\n3. Use the buttons to customize!", "inline": False},
]
}
{
"name": "How to use",
"value": f"1. Reply to any message\n2. Type `<@{bot.user.id}> tweety`\n3. Use the buttons to customize!",
"inline": False,
},
],
},
]
self.update_buttons()
def create_embed(self):
page = self.pages[self.current_page]
embed = discord.Embed(
@@ -64,68 +75,87 @@ class TweetyHelpView(discord.ui.View):
description=page["description"],
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
for field in page["fields"]:
embed.add_field(name=field["name"], value=field["value"], inline=field["inline"])
embed.add_field(
name=field["name"], value=field["value"], inline=field["inline"]
)
embed.set_image(url=page["gif_url"])
embed.set_footer(text=f"Page {self.current_page + 1}/{len(self.pages)}")
return embed
def update_buttons(self):
self.clear_items()
prev_button = discord.ui.Button(
label="Prev",
style=discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji(name="left", id=1420240344926126090),
disabled=self.current_page == 0
disabled=self.current_page == 0,
)
prev_button.callback = self.previous_page
self.add_item(prev_button)
next_button = discord.ui.Button(
label="Next",
style=discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji(name="right", id=1420240334100627456),
disabled=self.current_page == len(self.pages) - 1
disabled=self.current_page == len(self.pages) - 1,
)
next_button.callback = self.next_page
self.add_item(next_button)
async def previous_page(self, interaction: discord.Interaction):
if interaction.user.id != self.user_id:
await interaction.response.send_message("You can't control someone else's help menu!", ephemeral=True)
await interaction.response.send_message(
"You can't control someone else's help menu!", ephemeral=True
)
return
if self.current_page > 0:
self.current_page -= 1
self.update_buttons()
await interaction.response.edit_message(embed=self.create_embed(), view=self)
await interaction.response.edit_message(
embed=self.create_embed(), view=self
)
else:
await interaction.response.defer()
async def next_page(self, interaction: discord.Interaction):
if interaction.user.id != self.user_id:
await interaction.response.send_message("You can't control someone else's help menu!", ephemeral=True)
await interaction.response.send_message(
"You can't control someone else's help menu!", ephemeral=True
)
return
if self.current_page < len(self.pages) - 1:
self.current_page += 1
self.update_buttons()
await interaction.response.edit_message(embed=self.create_embed(), view=self)
await interaction.response.edit_message(
embed=self.create_embed(), view=self
)
else:
await interaction.response.defer()
async def on_timeout(self):
for item in self.children:
item.disabled = True
class TweetyView(discord.ui.View):
def __init__(self, author_id: int, original_message, tweet_data: dict, api_url: str, image_message: Optional[discord.Message] = None):
def __init__(
self,
author_id: int,
original_message,
tweet_data: dict,
api_url: str,
image_message: Optional[discord.Message] = None,
):
super().__init__(timeout=300)
self.author_id = author_id
self.original_message = original_message
@@ -138,28 +168,34 @@ class TweetyView(discord.ui.View):
self.click_count = 0
self.update_button_styles()
def update_button_styles(self):
self.clear_items()
dark_button = discord.ui.Button(
label="Dark Mode" if self.is_dark else "Light Mode",
style=discord.ButtonStyle.primary if self.is_dark else discord.ButtonStyle.secondary,
style=discord.ButtonStyle.primary
if self.is_dark
else discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji(name="darkmode", id=1425165393751965884),
custom_id="toggle_dark"
custom_id="toggle_dark",
)
dark_button.callback = self.toggle_dark_callback
self.add_item(dark_button)
verified_button = discord.ui.Button(
label="Verified",
style=discord.ButtonStyle.primary if self.is_verified else discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji(name="TwitterVerifiedBadge", id=1425165432142172392),
custom_id="toggle_verified"
style=discord.ButtonStyle.primary
if self.is_verified
else discord.ButtonStyle.secondary,
emoji=discord.PartialEmoji(
name="TwitterVerifiedBadge", id=1425165432142172392
),
custom_id="toggle_verified",
)
verified_button.callback = self.toggle_verified_callback
self.add_item(verified_button)
async def regenerate_tweet(self, interaction: discord.Interaction):
"""Regenerate only the image message with current settings"""
if self.click_count >= 10:
@@ -168,7 +204,9 @@ class TweetyView(discord.ui.View):
description="Stop spamming! You've reached the maximum number of changes for this tweet.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await interaction.response.send_message(embed=embed, ephemeral=True)
return
@@ -176,7 +214,7 @@ class TweetyView(discord.ui.View):
current_time = time.time()
time_since_last = current_time - self.last_regenerate_time
if time_since_last < 3 and self.last_regenerate_time > 0:
wait_time = 3 - time_since_last
await asyncio.sleep(wait_time)
@@ -189,9 +227,8 @@ class TweetyView(discord.ui.View):
async with session.post(
f"{self.api_url}/api/render",
json=self.tweet_data,
headers={"Content-Type": "application/json"}
headers={"Content-Type": "application/json"},
) as response:
if response.status != 200:
error_text = await response.text()
embed = discord.Embed(
@@ -199,23 +236,27 @@ class TweetyView(discord.ui.View):
description=f"API Error ({response.status}): {error_text}",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
await interaction.followup.send(embed=embed, ephemeral=True)
return
image_data = await response.read()
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
with tempfile.NamedTemporaryFile(
delete=False, suffix=".png"
) as temp_file:
temp_file.write(image_data)
temp_file_path = temp_file.name
with open(temp_file_path, 'rb') as f:
with open(temp_file_path, "rb") as f:
author_name = self.original_message.author.name
filename = f"tweet_{author_name}_{int(datetime.now().timestamp())}.png"
file = discord.File(
f,
filename=filename
filename = (
f"tweet_{author_name}_{int(datetime.now().timestamp())}.png"
)
file = discord.File(f, filename=filename)
self.update_button_styles()
@@ -223,18 +264,20 @@ class TweetyView(discord.ui.View):
await self.image_message.edit(attachments=[file], view=self)
else:
await interaction.followup.send(file=file, view=self)
os.remove(temp_file_path)
except Exception as e:
embed = discord.Embed(
title="Error",
description="Error regenerating tweet image",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await interaction.followup.send(embed=embed, ephemeral=True)
async def _check_author(self, interaction: discord.Interaction) -> bool:
"""Check if user is authorized to modify the tweet"""
if interaction.user.id != self.author_id:
@@ -243,11 +286,13 @@ class TweetyView(discord.ui.View):
description="You can't modify someone else's tweet!",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await interaction.response.send_message(embed=embed, ephemeral=True)
return False
return True
async def toggle_dark_callback(self, interaction: discord.Interaction):
"""Handle dark mode toggle button click"""
if not await self._check_author(interaction):
@@ -255,7 +300,7 @@ class TweetyView(discord.ui.View):
self.is_dark = not self.is_dark
self.tweet_data["dark"] = self.is_dark
await self.regenerate_tweet(interaction)
async def toggle_verified_callback(self, interaction: discord.Interaction):
"""Handle verified toggle button click"""
if not await self._check_author(interaction):
@@ -263,16 +308,16 @@ class TweetyView(discord.ui.View):
self.is_verified = not self.is_verified
self.tweet_data["verified"] = self.is_verified
await self.regenerate_tweet(interaction)
async def on_timeout(self):
"""Disable buttons when view times out"""
for item in self.children:
item.disabled = True
def tweety_command():
@commands.hybrid_command(
name="tweety",
description="Convert a replied message to a tweet image."
name="tweety", description="Convert a replied message to a tweet image."
)
@commands.cooldown(1, 10, commands.BucketType.user)
async def tweety(self, context):
@@ -281,44 +326,60 @@ def tweety_command():
embed = view.create_embed()
await context.send(embed=embed, view=view, ephemeral=True)
return
if not context.message.reference or not context.message.reference.message_id:
embed = discord.Embed(
title="Error",
description="You must reply to a message to use this command!",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.send(embed=embed)
return
try:
original_message = await context.channel.fetch_message(context.message.reference.message_id)
original_message = await context.channel.fetch_message(
context.message.reference.message_id
)
processing_embed = discord.Embed(
title="Tweet Generator (Processing)",
description="<a:mariospin:1423677027013103709> Generating tweet... This may take a moment.",
color=0x7289DA,
)
processing_embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
processing_embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
processing_msg = await context.send(embed=processing_embed)
author = original_message.author
display_name = author.display_name or author.name
username = f"@{author.name}"
avatar_url = str(author.avatar.url) if author.avatar else str(author.default_avatar.url)
avatar_url = (
str(author.avatar.url)
if author.avatar
else str(author.default_avatar.url)
)
message_text = original_message.content
for mention in original_message.mentions:
message_text = message_text.replace(f'<@{mention.id}>', f'@{mention.name}')
message_text = message_text.replace(f'<@!{mention.id}>', f'@{mention.name}')
message_text = message_text.replace(
f"<@{mention.id}>", f"@{mention.name}"
)
message_text = message_text.replace(
f"<@!{mention.id}>", f"@{mention.name}"
)
for role in original_message.role_mentions:
message_text = message_text.replace(f'<@&{role.id}>', f'@{role.name}')
message_text = message_text.replace(f"<@&{role.id}>", f"@{role.name}")
for channel in original_message.channel_mentions:
message_text = message_text.replace(f'<#{channel.id}>', f'#{channel.name}')
message_text = message_text.replace(
f"<#{channel.id}>", f"#{channel.name}"
)
if not message_text.strip():
await processing_msg.delete()
embed = discord.Embed(
@@ -326,10 +387,12 @@ def tweety_command():
description="No text found! This command only works with text messages.",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.send(embed=embed)
return
if len(message_text) > 300:
await processing_msg.delete()
embed = discord.Embed(
@@ -337,13 +400,15 @@ def tweety_command():
description=f"Message is too long! Maximum 300 characters allowed.\nYour message: {len(message_text)} characters",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.send(embed=embed)
return
message_text = break_long_words(message_text, max_word_length=50)
ny_tz = pytz.timezone('America/New_York')
ny_tz = pytz.timezone("America/New_York")
msg_time_ny = original_message.created_at.astimezone(ny_tz)
timestamp = msg_time_ny.strftime("%I:%M %p · %b %d, %Y").replace(" 0", " ")
tweet_data = {
@@ -353,19 +418,18 @@ def tweety_command():
"avatar": avatar_url,
"timestamp": timestamp,
"verified": False,
"dark": False
"dark": False,
}
API_BASE_URL = "http://tweet.6969.pro"
async with aiohttp.ClientSession() as session:
try:
async with session.post(
f"{API_BASE_URL}/api/render",
json=tweet_data,
headers={"Content-Type": "application/json"}
headers={"Content-Type": "application/json"},
) as response:
if response.status != 200:
await processing_msg.delete()
error_text = await response.text()
@@ -374,26 +438,37 @@ def tweety_command():
description=f"API Error ({response.status}): {error_text}",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
await context.send(embed=embed)
return
image_data = await response.read()
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
with tempfile.NamedTemporaryFile(
delete=False, suffix=".png"
) as temp_file:
temp_file.write(image_data)
temp_file_path = temp_file.name
await processing_msg.delete()
with open(temp_file_path, 'rb') as f:
file = discord.File(f, filename=f"tweet_{author.name}_{int(datetime.now().timestamp())}.png")
with open(temp_file_path, "rb") as f:
file = discord.File(
f,
filename=f"tweet_{author.name}_{int(datetime.now().timestamp())}.png",
)
embed = discord.Embed(
title="Tweet Generated",
description=f"<:error:1424007141768822824> Tweet sometimes may look a bit broken, im gonna rewrite the API another time... (it wasnt made for Syntrel in the first place)",
color=0x7289DA,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
embed.set_footer(
text=f"Requested by {context.author.name}",
icon_url=context.author.display_avatar.url,
@@ -403,7 +478,7 @@ def tweety_command():
author_id=context.author.id,
original_message=original_message,
tweet_data=tweet_data,
api_url=API_BASE_URL
api_url=API_BASE_URL,
)
await context.send(embed=embed)
@@ -411,7 +486,7 @@ def tweety_command():
view.image_message = image_message
os.remove(temp_file_path)
except aiohttp.ClientError:
await processing_msg.delete()
embed = discord.Embed(
@@ -419,7 +494,10 @@ def tweety_command():
description=f"Connection error: Could not reach tweet API",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
await context.send(embed=embed)
except Exception:
await processing_msg.delete()
@@ -428,7 +506,10 @@ def tweety_command():
description="Error generating tweet image",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media",
icon_url="https://yes.nighty.works/raw/y5SEZ9.webp",
)
await context.send(embed=embed)
except Exception:
@@ -437,7 +518,9 @@ def tweety_command():
description="Error processing the message!",
color=0xE02B2B,
)
embed.set_author(name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp")
embed.set_author(
name="Media", icon_url="https://yes.nighty.works/raw/y5SEZ9.webp"
)
await context.send(embed=embed)
return tweety