made some changes to certain functions

This commit is contained in:
Jellyfishsh 2025-04-19 03:33:44 -07:00
parent 80b47c6d68
commit d55f473c69

71
bot.py
View File

@ -1,9 +1,10 @@
# Imports # Imports
import os import os
from discord.webhook.async_ import interaction_message_response_params
import requests import requests
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import DiscordException, NotFound, app_commands, guild, voice_client from discord import DiscordException, NotFound, app_commands, guild, user, voice_client
from dotenv import load_dotenv from dotenv import load_dotenv
@ -99,7 +100,7 @@ async def playTrack(interaction: discord.Interaction):
# on the play command, we add that song to the list # on the play command, we add that song to the list
headers_str = f"-headers \"X-Emby-Token: {JELLYFIN_API_KEY}\"" headers_str = f"-headers \"X-Emby-Token: {JELLYFIN_API_KEY}\""
try: try:
while guild_queue_dict[interaction.guild_id].count() > 0: while len(guild_queue_dict[interaction.guild_id]) > 0:
# Get song information # Get song information
song = guild_queue_dict[interaction.guild_id][0] song = guild_queue_dict[interaction.guild_id][0]
@ -158,6 +159,27 @@ async def connect(interaction: discord.Interaction):
# Makes the reaction visible to everyone # Makes the reaction visible to everyone
await interaction.response.defer() await interaction.response.defer()
try:
voice_status = await interaction.user.fetch_voice()
user_channel = voice_status.channel
voice_client = interaction.guild.voice_client
if voice_client is None:
voice_client = await user_channel.connect()
elif voice_client.channel != user_channel:
await voice_client.disconnect()
voice_client = await user_channel.connect()
except discord.errors.NotFound as e:
print(f"Error: {e}")
await interaction.followup.send("You are not in a voice channel!")
return
except discord.errors.Forbidden as e:
print(f"Error: {e}")
await interaction.followup.send(f"I am not allowed in that voice channel!")
return
await interaction.followup.send(f"Connected!")
return return
@ -198,6 +220,9 @@ async def play(interaction: discord.Interaction):
# Makes the reaction visible to everyone # Makes the reaction visible to everyone
await interaction.response.defer() await interaction.response.defer()
if guild_queue_dict.get(interaction.guild_id) == None:
guild_queue_dict[interaction.guild_id] = []
# Some basic checks # Some basic checks
try: try:
# Check if the bot is connected to a channel # Check if the bot is connected to a channel
@ -205,19 +230,17 @@ async def play(interaction: discord.Interaction):
await interaction.followup.send(f"Not connected to your voice channel!") await interaction.followup.send(f"Not connected to your voice channel!")
return # After this point, we know that it is in a channel return # After this point, we know that it is in a channel
# Check if the queue is empty # Check if the queue is empty
if guild_queue_dict[interaction.guild_id].count() == 0: if len(guild_queue_dict[interaction.guild_id]) == 0:
await interaction.followup.send(f"Queue is empty!") await interaction.followup.send(f"Queue is empty!")
return # After this point, we know the queue has something in it return # After this point, we know the queue has something in it
except: except discord.errors.NotFound:
await interaction.followup.send(f"You're not connected to a voice channel!") await interaction.followup.send(f"You're not connected to a voice channel!")
return
# Play all items in the queue # Play all items in the queue
playTrack(interaction) await playTrack(interaction)
return return
@ -267,13 +290,16 @@ async def search(interaction: discord.Interaction, title: str):
if not data: if not data:
await interaction.followup.send(f"No song found matching `{title}`.") await interaction.followup.send(f"No song found matching `{title}`.")
return return
result_list = [] result_list = []
for song in data: for song in data:
result_list.append(f"**{song.get("Name")}** by *{song.get("AlbumArtist", ["Unknown Artist"])}*") result_list.append(f"**{song.get("Name")}** by *{song.get("AlbumArtist", ["Unknown Artist"])}*")
await interaction.followup.send("\n".join(result_list)) await interaction.followup.send("\n".join(result_list))
# end search ----- return
# Skip # Skip
@ -368,13 +394,23 @@ async def add(interaction: discord.Interaction, title: str):
# query the item # query the item
data = await make_request(title, interaction) data = await make_request(title, interaction)
# Check if data is bogus
if not data:
await interaction.followup.send(f"Couldn't find a song matching {title}")
return
# Gets song information # Gets song information
query_song = data[0] query_song = data[0]
query_song_id = query_song.get('Id') query_song_id = query_song.get('Id')
query_song_title = query_song.get('Name') query_song_title = query_song.get('Name')
query_song_artist = query_song.get('AlbumArtist', ['Unknown Artist']) query_song_artist = query_song.get('AlbumArtist', ['Unknown Artist'])
print(f"Found: {query_song_id}, with the title {query_song_title} and artist {query_song_artist}")
# Add song information as a tuple # Add song information as a tuple
if guild_queue_dict.get(interaction.guild_id) == None:
guild_queue_dict[interaction.guild_id] = []
guild_queue_dict[interaction.guild_id].append((query_song_id, query_song_title, query_song_artist)) guild_queue_dict[interaction.guild_id].append((query_song_id, query_song_title, query_song_artist))
@ -382,11 +418,11 @@ async def add(interaction: discord.Interaction, title: str):
# Sends informational message # Sends informational message
await interaction.followup.send(f"Added **{query_song_title}** by *{query_song_artist}*!") await interaction.followup.send(f"Added **{query_song_title}** by *{query_song_artist}*!")
tracks_queued = guild_queue_dict[interaction.guild_id].count() tracks_queued = len(guild_queue_dict[interaction.guild_id])
# Helpfully tells the user how many tracks until their track is played # Helpfully tells the user how many tracks until their track is played
if tracks_queued > 0: if tracks_queued > 1:
await interaction.followup.send(f"Plays after {tracks_queued} more tracks.") await interaction.followup.send(f"Plays after **{tracks_queued}** more tracks.")
return return
@ -411,7 +447,16 @@ async def queue(interaction: discord.Interaction):
# Makes the reaction visible to everyone # Makes the reaction visible to everyone
await interaction.response.defer() await interaction.response.defer()
# Check if queue is uninitialized
if guild_queue_dict.get(interaction.guild_id) == None:
guild_queue_dict[interaction.guild_id] = []
# Add all the items in a queue to a list # Add all the items in a queue to a list
if len(guild_queue_dict[interaction.guild_id]) == 0:
await interaction.followup.send(f"The queue is empty!")
return
# The queue has something in it
result_list = [] result_list = []
for song in guild_queue_dict[interaction.guild_id]: for song in guild_queue_dict[interaction.guild_id]:
result_list.append(f"**{song[1]}** by *{song[2]}*") result_list.append(f"**{song[1]}** by *{song[2]}*")