From c365cd1ff2cdadd9567fbead896db467a693bcc6 Mon Sep 17 00:00:00 2001 From: Michelle Date: Thu, 30 Apr 2026 16:17:39 +0200 Subject: [PATCH] try adding youtube player support Co-authored-by: Copilot --- main.py | 2 ++ requirements.txt | 5 +++-- yt_player.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 yt_player.py diff --git a/main.py b/main.py index 8c13e5b..6e0f051 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ import os import re import logging import random +import yt_player # this is a bot which posts the latest image post from ich_iel # the code probably sucks, but it works, so I don't care @@ -17,6 +18,7 @@ import random load_dotenv() prefix = os.getenv("COMMAND_PREFIX", "/") bot = fluxer.Bot(command_prefix=prefix, intents=fluxer.Intents.GUILD_MESSAGES | fluxer.Intents.GUILDS | fluxer.Intents.MESSAGE_CONTENT) +yt_player.setup(bot) task = None diff --git a/requirements.txt b/requirements.txt index 8935d93..fe455f2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ -fluxer.py +fluxer.py[voice] requests asyncio dotenv -aiohttp \ No newline at end of file +aiohttp +yt-dlp \ No newline at end of file diff --git a/yt_player.py b/yt_player.py new file mode 100644 index 0000000..199a9e0 --- /dev/null +++ b/yt_player.py @@ -0,0 +1,38 @@ +# at this point this bot isn't just a reddit bot anymore, maybe i should start renaming it lol +import yt_dlp +import logging +import os + +AUDIO_DIR = "data/audio" + +_bot = None + +def setup(bot): + global _bot + _bot = bot + os.makedirs(AUDIO_DIR, exist_ok=True) + bot.command()(play) + +async def play(ctx, channel_id: int, *, path: str): + channel = await _bot.fetch_channel(str(channel_id)) + url = ctx.content[len("!play "):].strip() + logging.info(f"Playing {url}") + ydl_opts = { + 'format': 'm4a/bestaudio/best', + 'outtmpl': f'{AUDIO_DIR}/%(id)s.%(ext)s', + 'postprocessors': [{ # Extract audio using ffmpeg + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'm4a', + }] + } + + with yt_dlp.YoutubeDL(ydl_opts) as ydl: + info = ydl.extract_info(url, download=True) + filename = ydl.prepare_filename(info).rsplit('.', 1)[0] + '.m4a' + title = info.get('title', 'Unknown Title') + logging.info(f"Downloaded to {filename}") + + ctx.send(f"Playing {title} in {channel.mention}") + + async with await channel.connect(_bot) as vc: + await vc.play_file(filename)