Files
discord-plex-recommender/bot/main.py
2025-06-03 13:12:58 -04:00

68 lines
2.3 KiB
Python

# bot/main.py
import discord
from discord.ext import commands
from discord import app_commands
from tautulli_client import TautulliClient
from recommender import Recommender
from config import DISCORD_BOT_TOKEN
intents = discord.Intents.default()
client = commands.Bot(command_prefix="!", intents=intents)
tree = client.tree
tautulli = TautulliClient()
recommender = Recommender()
@client.event
async def on_ready():
print(f"🤖 Logged in as {client.user} (ID: {client.user.id})")
await tree.sync()
print("✅ Slash commands synced.")
@tree.command(name="recommend", description="Recommend movies or TV shows for a Plex user")
@app_commands.describe(
username="Plex username (case-sensitive)",
media_type="Choose 'movie' or 'tv'"
)
async def recommend(interaction: discord.Interaction, username: str, media_type: str):
await interaction.response.defer() # Acknowledge the request
try:
history = tautulli.get_user_watch_history(username=username, media_type=media_type.lower())
if not history:
await interaction.followup.send(f"⚠️ No recent {media_type} history found for `{username}`.")
return
watched_titles = [entry["title"] for entry in history]
# Print cached Plex titles for this media_type
print(f"🗂️ Cached Plex {media_type}s:")
for item in recommender.cache.data:
if item["type"] == media_type.lower():
print(f"- {item['title']}")
results = recommender.recommend(watched_titles, media_type=media_type.lower())
available = results["available"]
requestable = results["requestable"]
def format_recs(title_list):
return "\n".join(f"{title}" for title in title_list)
# Final Discord message
response = (
f"🎬 **Recommendations for `{username}` ({media_type}s)**\n\n"
f"**✅ Available on Plex:**\n"
f"{format_recs(available) or 'None found.'}\n\n"
f"**🛒 Requestable:**\n"
f"{format_recs(requestable) or 'None found.'}"
)
await interaction.followup.send(response)
except Exception as e:
await interaction.followup.send(f"❌ An error occurred while generating recommendations: {e}")
if __name__ == "__main__":
client.run(DISCORD_BOT_TOKEN)