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

@@ -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