made some comments regarding the Main file and the Command headers
This commit is contained in:
parent
bde2d35634
commit
2304f956b4
33
bot.py
33
bot.py
@ -1,3 +1,4 @@
|
||||
# Imports
|
||||
import os
|
||||
import requests
|
||||
import discord
|
||||
@ -5,22 +6,29 @@ from discord.ext import commands
|
||||
from discord import app_commands
|
||||
from dotenv import load_dotenv
|
||||
|
||||
opus_path = '/usr/lib/libopus.so.0.10.1'
|
||||
discord.opus.load_opus(opus_path)
|
||||
|
||||
# Environment vars
|
||||
load_dotenv()
|
||||
TOKEN = os.getenv("DISCORD_TOKEN")
|
||||
JELLYFIN_URL = os.getenv("JELLYFIN_URL")
|
||||
JELLYFIN_API_KEY = os.getenv("JELLYFIN_API_KEY")
|
||||
|
||||
# Setting the opus path, since Linux can't read it
|
||||
opus_path = '/usr/lib/libopus.so.0.10.1'
|
||||
discord.opus.load_opus(opus_path)
|
||||
|
||||
# Request headers
|
||||
headers= {
|
||||
"X-Emby-Token": JELLYFIN_API_KEY
|
||||
}
|
||||
|
||||
# discord bot setups
|
||||
intents = discord.Intents.default()
|
||||
intents.message_content = True
|
||||
intents.voice_states = True
|
||||
|
||||
|
||||
# ----- Classes ----- #
|
||||
# TODO: turn this in to an import in a separate file
|
||||
class JellyfinBot(commands.Bot):
|
||||
def __init__(self):
|
||||
super().__init__(command_prefix="/", intents=intents)
|
||||
@ -34,15 +42,28 @@ bot = JellyfinBot()
|
||||
async def on_ready():
|
||||
print(f"🪼 Logged in as {bot.user}")
|
||||
|
||||
|
||||
# ----- Commands ----- #
|
||||
|
||||
# Play a song
|
||||
|
||||
# Slash command descriptors
|
||||
@bot.tree.command(name="play", description="Play a song from Jellyfin")
|
||||
@app_commands.describe(song="Song title to play")
|
||||
async def play(interaction: discord.Interaction, song: str):
|
||||
await interaction.response.defer(ephemeral=False)
|
||||
|
||||
|
||||
# Play function
|
||||
async def play(interaction: discord.Interaction, song: str):
|
||||
|
||||
# Makes the reaction visible to everyone
|
||||
await interaction.response.defer()
|
||||
|
||||
# Checks if in a voice channel
|
||||
if not interaction.user.voice:
|
||||
await interaction.response.send_message("You must be in a voice channel.")
|
||||
return
|
||||
|
||||
# Builds the request header for the song
|
||||
url = f"{JELLYFIN_URL}/Items"
|
||||
params = {
|
||||
"searchTerm": song,
|
||||
@ -51,7 +72,9 @@ async def play(interaction: discord.Interaction, song: str):
|
||||
"Limit": 5
|
||||
}
|
||||
|
||||
# Attempts to get the file
|
||||
try:
|
||||
|
||||
response = requests.get(url, headers=headers, params=params)
|
||||
response.raise_for_status()
|
||||
data = response.json().get("Items", [])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user