mirror of
https://github.com/neoarz/Syntrel.git
synced 2025-12-25 03:40:11 +01:00
fix(download): now works in dms and priv channels
This commit is contained in:
2
TODO.md
2
TODO.md
@@ -65,6 +65,8 @@
|
|||||||
|
|
||||||
- [ ] Add logs to channel
|
- [ ] Add logs to channel
|
||||||
|
|
||||||
|
- [ ] Add idevice livecontainer command
|
||||||
|
|
||||||
- [x] add [tts](https://developer.puter.com/tutorials/free-unlimited-text-to-speech-api/) command
|
- [x] add [tts](https://developer.puter.com/tutorials/free-unlimited-text-to-speech-api/) command
|
||||||
|
|
||||||
- [x] update botinfo command
|
- [x] update botinfo command
|
||||||
|
|||||||
@@ -10,6 +10,45 @@ import logging
|
|||||||
|
|
||||||
logger = logging.getLogger("discord_bot")
|
logger = logging.getLogger("discord_bot")
|
||||||
|
|
||||||
|
async def send_error_message(context, description: str):
|
||||||
|
embed = discord.Embed(
|
||||||
|
title="Error",
|
||||||
|
description=description,
|
||||||
|
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():
|
||||||
|
await interaction.response.send_message(embed=embed, ephemeral=True)
|
||||||
|
else:
|
||||||
|
await interaction.followup.send(embed=embed, ephemeral=True)
|
||||||
|
else:
|
||||||
|
await context.send(embed=embed, ephemeral=True)
|
||||||
|
|
||||||
|
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}")
|
||||||
|
form = aiohttp.FormData()
|
||||||
|
form.add_field('reqtype', 'fileupload')
|
||||||
|
with open(path, 'rb') as f:
|
||||||
|
form.add_field('fileToUpload', f, 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:
|
||||||
|
text = await resp.text()
|
||||||
|
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}")
|
||||||
|
return url_text
|
||||||
|
logger.error(f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}")
|
||||||
|
raise RuntimeError(f"Upload failed: {text.strip()}")
|
||||||
|
|
||||||
def download_command():
|
def download_command():
|
||||||
@commands.hybrid_command(
|
@commands.hybrid_command(
|
||||||
name="download",
|
name="download",
|
||||||
@@ -18,74 +57,29 @@ def download_command():
|
|||||||
@commands.cooldown(1, 30, commands.BucketType.user)
|
@commands.cooldown(1, 30, commands.BucketType.user)
|
||||||
async def download(self, context, *, url: str):
|
async def download(self, context, *, url: str):
|
||||||
if not url:
|
if not url:
|
||||||
embed = discord.Embed(
|
await send_error_message(context, "Please provide a valid URL to download.")
|
||||||
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")
|
|
||||||
|
|
||||||
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)
|
|
||||||
else:
|
|
||||||
await interaction.followup.send(embed=embed, ephemeral=True)
|
|
||||||
else:
|
|
||||||
await context.send(embed=embed, ephemeral=True)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
parsed_url = urlparse(url)
|
parsed_url = urlparse(url)
|
||||||
if not parsed_url.scheme or not parsed_url.netloc:
|
if not parsed_url.scheme or not parsed_url.netloc:
|
||||||
embed = discord.Embed(
|
await send_error_message(context, "Please provide a valid URL.")
|
||||||
title="Error",
|
|
||||||
description="Please provide a valid URL.",
|
|
||||||
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():
|
|
||||||
await interaction.response.send_message(embed=embed, ephemeral=True)
|
|
||||||
else:
|
|
||||||
await interaction.followup.send(embed=embed, ephemeral=True)
|
|
||||||
else:
|
|
||||||
await context.send(embed=embed, ephemeral=True)
|
|
||||||
return
|
return
|
||||||
except Exception:
|
except Exception:
|
||||||
embed = discord.Embed(
|
await send_error_message(context, "Please provide a valid URL.")
|
||||||
title="Error",
|
|
||||||
description="Please provide a valid URL.",
|
|
||||||
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():
|
|
||||||
await interaction.response.send_message(embed=embed, ephemeral=True)
|
|
||||||
else:
|
|
||||||
await interaction.followup.send(embed=embed, ephemeral=True)
|
|
||||||
else:
|
|
||||||
await context.send(embed=embed, ephemeral=True)
|
|
||||||
return
|
return
|
||||||
|
|
||||||
processing_embed = discord.Embed(
|
|
||||||
title="Download (Processing)",
|
|
||||||
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")
|
|
||||||
|
|
||||||
interaction = getattr(context, "interaction", None)
|
interaction = getattr(context, "interaction", None)
|
||||||
if interaction is not None:
|
if interaction is not None:
|
||||||
if not interaction.response.is_done():
|
if not interaction.response.is_done():
|
||||||
await interaction.response.send_message(embed=processing_embed, ephemeral=True)
|
await interaction.response.defer(ephemeral=False)
|
||||||
else:
|
|
||||||
await interaction.followup.send(embed=processing_embed, ephemeral=True)
|
|
||||||
else:
|
else:
|
||||||
|
processing_embed = discord.Embed(
|
||||||
|
title="Download (Processing)",
|
||||||
|
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_msg = await context.send(embed=processing_embed)
|
processing_msg = await context.send(embed=processing_embed)
|
||||||
|
|
||||||
temp_dir = tempfile.mkdtemp()
|
temp_dir = tempfile.mkdtemp()
|
||||||
@@ -122,7 +116,6 @@ def download_command():
|
|||||||
video_uploader = info.get('uploader', 'Unknown')
|
video_uploader = info.get('uploader', 'Unknown')
|
||||||
video_url = info.get('webpage_url') or info.get('original_url') or url
|
video_url = info.get('webpage_url') or info.get('original_url') or url
|
||||||
platform = info.get('extractor') or info.get('extractor_key') or 'Unknown'
|
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))]
|
files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))]
|
||||||
|
|
||||||
@@ -135,32 +128,11 @@ def download_command():
|
|||||||
|
|
||||||
if file_size > 24 * 1024 * 1024: # 24MB limit
|
if file_size > 24 * 1024 * 1024: # 24MB limit
|
||||||
logger.info("File is over 24MB, uploading to Catbox")
|
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}")
|
|
||||||
form = aiohttp.FormData()
|
|
||||||
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:
|
|
||||||
text = await resp.text()
|
|
||||||
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}")
|
|
||||||
return url_text
|
|
||||||
logger.error(f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}")
|
|
||||||
raise RuntimeError(f"Upload failed: {text.strip()}")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
link = await upload_to_catbox(video_file)
|
link = await upload_to_catbox(video_file)
|
||||||
minutes, seconds = divmod(video_duration_seconds, 60)
|
minutes, seconds = divmod(video_duration_seconds, 60)
|
||||||
duration_str = f"{minutes}:{seconds:02d}"
|
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})**\n\n{link}" if video_url else f"### **{video_title}**\n\n{link}"
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title="Download",
|
title="Download",
|
||||||
description=description_text,
|
description=description_text,
|
||||||
@@ -173,16 +145,10 @@ def download_command():
|
|||||||
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:
|
if interaction is not None:
|
||||||
await context.channel.send(embed=embed)
|
await interaction.followup.send(embed=embed)
|
||||||
await context.channel.send(link)
|
|
||||||
try:
|
|
||||||
await interaction.delete_original_response()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
await processing_msg.delete()
|
await processing_msg.delete()
|
||||||
await context.channel.send(embed=embed)
|
await context.send(embed=embed)
|
||||||
await context.channel.send(link)
|
|
||||||
return
|
return
|
||||||
except Exception as upload_error:
|
except Exception as upload_error:
|
||||||
logger.exception(f"Catbox upload exception: {upload_error}")
|
logger.exception(f"Catbox upload exception: {upload_error}")
|
||||||
@@ -200,10 +166,6 @@ def download_command():
|
|||||||
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:
|
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:
|
else:
|
||||||
await processing_msg.delete()
|
await processing_msg.delete()
|
||||||
@@ -230,45 +192,19 @@ def download_command():
|
|||||||
file = discord.File(f, filename=files[0])
|
file = discord.File(f, filename=files[0])
|
||||||
|
|
||||||
if interaction is not None:
|
if interaction is not None:
|
||||||
await context.channel.send(embed=embed)
|
await interaction.followup.send(embeds=[embed], files=[file])
|
||||||
await context.channel.send(file=file)
|
|
||||||
try:
|
|
||||||
await interaction.delete_original_response()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
await processing_msg.delete()
|
await processing_msg.delete()
|
||||||
await context.channel.send(embed=embed)
|
await context.send(embeds=[embed], files=[file])
|
||||||
await context.channel.send(file=file)
|
|
||||||
except discord.HTTPException as e:
|
except discord.HTTPException as e:
|
||||||
if e.status == 413:
|
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}")
|
|
||||||
form = aiohttp.FormData()
|
|
||||||
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:
|
|
||||||
text = await resp.text()
|
|
||||||
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}")
|
|
||||||
return url_text
|
|
||||||
logger.error(f"Catbox upload failed: status={resp.status} body={text.strip()[:500]}")
|
|
||||||
raise RuntimeError(f"Upload failed: {text.strip()}")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
link = await upload_to_catbox(video_file)
|
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}"
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
title="Download",
|
title="Download",
|
||||||
description=description_text,
|
description=description_text_with_link,
|
||||||
color=0x7289DA,
|
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")
|
||||||
@@ -278,16 +214,10 @@ def download_command():
|
|||||||
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:
|
if interaction is not None:
|
||||||
await context.channel.send(embed=embed)
|
await interaction.followup.send(embed=embed)
|
||||||
await context.channel.send(link)
|
|
||||||
try:
|
|
||||||
await interaction.delete_original_response()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
await processing_msg.delete()
|
await processing_msg.delete()
|
||||||
await context.channel.send(embed=embed)
|
await context.send(embed=embed)
|
||||||
await context.channel.send(link)
|
|
||||||
except Exception as upload_error:
|
except Exception as upload_error:
|
||||||
logger.exception(f"Catbox upload exception: {upload_error}")
|
logger.exception(f"Catbox upload exception: {upload_error}")
|
||||||
embed = discord.Embed(
|
embed = discord.Embed(
|
||||||
@@ -298,10 +228,6 @@ def download_command():
|
|||||||
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:
|
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:
|
else:
|
||||||
await processing_msg.delete()
|
await processing_msg.delete()
|
||||||
@@ -318,10 +244,6 @@ def download_command():
|
|||||||
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:
|
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:
|
else:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user