i tried to make spotify bot in discord, but when trying to play playlist i have error, here’s fragment of it:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: SpotifyException: http status: 400, code:-1 – Unsupported URL / URI., reason: None
my discord bot code:
import discord
from discord.ext import commands
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import asyncio
import logging
logging.basicConfig(level=logging.ERROR, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", filename="app.log", filemode="w")
bot = commands.Bot(command_prefix="!", intents=discord.Intents().all())
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=' my id', client_secret=" my secret", redirect_uri='http://localhost:8888/callback', scope="playlist-read-private"))
async def play_spotify_playlist(ctx, playlist_link):
playlist_id = playlist_link.split("https://stackoverflow.com/")[-1]
playlist_info = sp.playlist(playlist_id)
tracks = playlist_info['tracks']['items']
if not tracks:
await ctx.send("Nie można znaleźć utworów w playlisty.")
return
channel = ctx.author.voice.channel
if not channel:
await ctx.send("Nie jesteś połączony z kanałem głosowym.")
return
vc = await channel.connect()
for track in tracks:
track_name = track['track']['name']
track_artist = track['track']['artists'][0]['name']
track_url = track['track']['external_urls']['spotify']
await ctx.send(f"Odtwarzam: {track_name} - {track_artist}")
vc.play(discord.FFmpegPCMAudio(track_url))
while vc.is_playing():
await asyncio.sleep(1)
await vc.disconnect()
@bot.command()
async def play(ctx, playlist_link):
await play_spotify_playlist(ctx, playlist_link)
@bot.command()
async def stop(ctx):
# Sprawdź, czy bot jest połączony z kanałem głosowym
if ctx.voice_client and ctx.voice_client.is_playing():
# Zatrzymaj odtwarzanie i rozłącz się z kanałem głosowym
ctx.voice_client.stop()
await ctx.send("Odtwarzanie zatrzymane.")
else:
await ctx.send("Aktualnie nic nie jest odtwarzane.")
bot.run('my discord bot token')
I’ve tried giving different playlists and even just the playlist id
Do you have your local host url as a redirect url in the Spotify developer dashboard?