college-bot-v2/src/cogs/ffmeta.py

51 lines
1.5 KiB
Python
Raw Normal View History

2024-02-06 23:03:49 +00:00
import asyncio
import logging
import discord
from discord.ext import commands
class FFMeta(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.log = logging.getLogger("jimmy.cogs.ffmeta")
@commands.slash_command()
async def ffprobe(self, ctx: discord.ApplicationContext, url: str = None, attachment: discord.Attachment = None):
"""Runs ffprobe on a given URL or attachment"""
if url is None:
if attachment is None:
return await ctx.respond("No URL or attachment provided")
url = attachment.url
await ctx.defer()
process = await asyncio.create_subprocess_exec(
"ffprobe",
"-hide_banner",
url,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
stdout, stderr = await process.communicate()
2024-02-06 23:07:30 +00:00
stdout = stdout.decode("utf-8", "replace")
stderr = stderr.decode("utf-8", "replace")
2024-02-06 23:03:49 +00:00
paginator = commands.Paginator(prefix="```", suffix="```")
2024-02-06 23:08:03 +00:00
for line in stdout.splitlines():
2024-02-06 23:03:49 +00:00
if stderr:
paginator.add_line(f"[OUT] {line}"[:2000])
else:
paginator.add_line(line[:2000])
2024-02-06 23:08:03 +00:00
for line in stderr.splitlines():
2024-02-06 23:03:49 +00:00
paginator.add_line(f"[ERR] {line}"[:2000])
for page in paginator.pages:
await ctx.respond(page)
def setup(bot: commands.Bot):
bot.add_cog(FFMeta(bot))