Jonathan Jara a1cb90c963 first push
2025-04-18 13:59:16 -07:00

71 lines
2.0 KiB
Python

import os
import requests
import discord
from discord.ext import commands
from discord import app_commands
from dotenv import load_dotenv
import io
import asyncio
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD_ID = discord.Object(id=1131317261202899034)
JELLYFIN_URL = os.getenv("JELLYFIN_URL")
JELLYFIN_API_KEY = os.getenv("JELLYFIN_API_KEY")
headers= {
"X-Emby-Token": JELLYFIN_API_KEY
}
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
class JellyfinBot(commands.Bot):
def __init__(self):
super().__init__(command_prefix="/", intents=intents)
async def setup_hook(self):
self.tree.copy_global_to(guild=GUILD_ID)
await self.tree.sync(guild=GUILD_ID)
bot = JellyfinBot()
@bot.event
async def on_ready():
print(f"🪼 Logged in as {bot.user}")
@bot.tree.command(name="search", description="Search for a song on Jellyfin")
@app_commands.describe(title="Song title to search for")
async def search(interaction: discord.Interaction, title: str):
await interaction.response.defer(ephemeral=True)
url = f"{JELLYFIN_URL}/Items"
params = {
"searchTerm": title,
"Recursive": True,
"IncludeItemTypes": "Audio",
"Limit": 5
}
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
data = response.json()
items = data.get("Items", [])
if not items:
await interaction.followup.send(f"❌ No song found matching `{title}`.")
return
lines = []
for item in items:
title = item.get("Name")
artist = item.get("AlbumArtist", ["Unknown Artist"])
lines.append(f"✅ **{title}** by *{artist}*")
await interaction.followup.send("\n".join(lines))
except requests.RequestException as e:
await interaction.followup.send("⚠️ Failed to contact Jellyfin server.")
print(f"Error: {e}")
bot.run(TOKEN)