Код
from discord.ext import commands
from discord.utils import get
from youtube_dl import YoutubeDL
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
@bot.command()
async def play(ctx, *, query):
voice_channel = ctx.author.voice.channel
if voice_channel:
await voice_channel.connect()
voice = get(bot.voice_clients, guild=ctx.guild)
with YoutubeDL({'format': 'bestaudio', 'noplaylist':'True'}) as ydl:
info = ydl.extract_info(f"ytsearch:{query}", download=False)['entries'][0]
URL = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(URL)
voice.play(source)
else:
await ctx.send("You are not connected to a voice channel.")
@bot.command()
async def stop(ctx):
voice = get(bot.voice_clients, guild=ctx.guild)
if voice.is_playing():
voice.stop()
await ctx.send("Stopped the music.")
else:
await ctx.send("No music is playing.")
bot.run('TOKEN')
При выполнении команды !play (название музыки) происходит это:
[2023-03-03 20:23:54] [INFO ] discord.voice_client: Connecting to voice...
[2023-03-03 20:23:54] [INFO ] discord.voice_client: Starting voice handshake... (connection attempt 1)
[2023-03-03 20:23:55] [INFO ] discord.voice_client: Voice handshake complete. Endpoint found frankfurt5438.discord.media
[download] Downloading playlist: phonk
[youtube:search] query "phonk": Downloading page 1
[youtube:search] playlist phonk: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] zG6NcHtMBHc: Downloading webpage
[youtube] Downloading just video zG6NcHtMBHc because of --no-playlist
ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see https://yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.