FEAT: Improved Spotify Controls adding Pause, Resume, Next, and Previous. Also Added a check for no devices
This commit is contained in:
parent
726ec91a79
commit
ed66d6438f
@ -5,7 +5,7 @@ from spotipy.oauth2 import SpotifyOAuth
|
|||||||
import config
|
import config
|
||||||
|
|
||||||
|
|
||||||
class SpotifyCog:
|
class SpotifyModule:
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
self.sp = spotipy.Spotify(
|
self.sp = spotipy.Spotify(
|
||||||
@ -26,41 +26,100 @@ class SpotifyCog:
|
|||||||
name="current_track", description="Get the currently playing song"
|
name="current_track", description="Get the currently playing song"
|
||||||
)
|
)
|
||||||
async def current_track(interaction: discord.Interaction):
|
async def current_track(interaction: discord.Interaction):
|
||||||
current = self.sp.currently_playing()
|
await interaction.response.defer()
|
||||||
if current is None or current["item"] is None:
|
try:
|
||||||
await interaction.response.send_message(
|
current = self.sp.currently_playing()
|
||||||
"No song is currently playing"
|
if current is None or current["item"] is None:
|
||||||
)
|
await interaction.followup.send("No song is currently playing")
|
||||||
else:
|
else:
|
||||||
track = current["item"]
|
track = current["item"]
|
||||||
artist = ", ".join([artist["name"] for artist in track[
|
artist = ", ".join([artist["name"] for artist in track["artists"]])
|
||||||
"artists"]])
|
await interaction.followup.send(
|
||||||
await interaction.response.send_message(
|
f"Currently playing: {track['name']} by {artist} "
|
||||||
f"Currently playing: {track['name']} by {artist} "
|
f"({track['album']['name']})"
|
||||||
f"({track['album']['name']})"
|
)
|
||||||
)
|
except Exception as e:
|
||||||
|
await interaction.followup.send(f"An error occurred: {e}")
|
||||||
|
|
||||||
@app_commands.command(
|
@app_commands.command(
|
||||||
name="play_track", description="Play a track by searching for it"
|
name="play_track", description="Play a track by searching for it"
|
||||||
)
|
)
|
||||||
async def play_track(interaction: discord.Interaction, query: str):
|
async def play_track(interaction: discord.Interaction, query: str):
|
||||||
results = self.sp.search(q=query, limit=1, type="track")
|
await interaction.response.defer()
|
||||||
if not results["tracks"]["items"]:
|
try:
|
||||||
await interaction.response.send_message("No results found")
|
results = self.sp.search(q=query, limit=1, type="track")
|
||||||
return
|
if not results["tracks"]["items"]:
|
||||||
|
await interaction.followup.send("No results found")
|
||||||
|
return
|
||||||
|
|
||||||
track = results["tracks"]["items"][0]
|
track = results["tracks"]["items"][0]
|
||||||
uri = track["uri"]
|
uri = track["uri"]
|
||||||
self.sp.start_playback(uris=[uri])
|
|
||||||
await interaction.response.send_message(
|
devices = self.sp.devices()
|
||||||
f"Now playing: {track['name']} by "
|
if not devices["devices"]:
|
||||||
f"{', '.join([artist['name'] for artist in track['artists']])}"
|
await interaction.followup.send("No active devices found. Please open Spotify on a device.")
|
||||||
f"({track['album']['name']})"
|
return
|
||||||
)
|
|
||||||
|
self.sp.start_playback(uris=[uri])
|
||||||
|
await interaction.followup.send(
|
||||||
|
f"Now playing: {track['name']} by "
|
||||||
|
f"{', '.join([artist['name'] for artist in track['artists']])} "
|
||||||
|
f"({track['album']['name']})"
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
await interaction.followup.send(f"An error occurred: {e}")
|
||||||
|
|
||||||
|
@app_commands.command(
|
||||||
|
name="pause", description="Pause the currently playing track"
|
||||||
|
)
|
||||||
|
async def pause(interaction: discord.Interaction):
|
||||||
|
await interaction.response.defer()
|
||||||
|
try:
|
||||||
|
self.sp.pause_playback()
|
||||||
|
await interaction.followup.send("Playback paused.")
|
||||||
|
except Exception as e:
|
||||||
|
await interaction.followup.send(f"An error occurred: {e}")
|
||||||
|
|
||||||
|
@app_commands.command(
|
||||||
|
name="resume", description="Resume the currently playing track"
|
||||||
|
)
|
||||||
|
async def resume(interaction: discord.Interaction):
|
||||||
|
await interaction.response.defer()
|
||||||
|
try:
|
||||||
|
self.sp.start_playback()
|
||||||
|
await interaction.followup.send("Playback resumed.")
|
||||||
|
except Exception as e:
|
||||||
|
await interaction.followup.send(f"An error occurred: {e}")
|
||||||
|
|
||||||
|
@app_commands.command(
|
||||||
|
name="next", description="Skip to the next track"
|
||||||
|
)
|
||||||
|
async def next_track(interaction: discord.Interaction):
|
||||||
|
await interaction.response.defer()
|
||||||
|
try:
|
||||||
|
self.sp.next_track()
|
||||||
|
await interaction.followup.send("Skipped to the next track.")
|
||||||
|
except Exception as e:
|
||||||
|
await interaction.followup.send(f"An error occurred: {e}")
|
||||||
|
|
||||||
|
@app_commands.command(
|
||||||
|
name="previous", description="Go back to the previous track"
|
||||||
|
)
|
||||||
|
async def previous_track(interaction: discord.Interaction):
|
||||||
|
await interaction.response.defer()
|
||||||
|
try:
|
||||||
|
self.sp.previous_track()
|
||||||
|
await interaction.followup.send("Returned to the previous track.")
|
||||||
|
except Exception as e:
|
||||||
|
await interaction.followup.send(f"An error occurred: {e}")
|
||||||
|
|
||||||
self.bot.tree.add_command(current_track)
|
self.bot.tree.add_command(current_track)
|
||||||
self.bot.tree.add_command(play_track)
|
self.bot.tree.add_command(play_track)
|
||||||
|
self.bot.tree.add_command(pause)
|
||||||
|
self.bot.tree.add_command(resume)
|
||||||
|
self.bot.tree.add_command(next_track)
|
||||||
|
self.bot.tree.add_command(previous_track)
|
||||||
|
|
||||||
|
|
||||||
async def setup(bot):
|
async def setup(bot):
|
||||||
SpotifyCog(bot)
|
SpotifyModule(bot)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user