G
G
Georgy Kharitonov2022-02-11 20:29:37
Python
Georgy Kharitonov, 2022-02-11 20:29:37

Discord.py and youtube_dl music not playing? What's wrong?

Music does not play. It only prints to the console: [youtube] yry1293OM_w: Downloading webpage, but nothing is done. I've been waiting for an hour...Here is the code:

@bot.command()
async def play(ctx, url:str):

    if not ctx.guild:
        e = discord.Embed(
                title = 'Ошибка!',
                description = 'Эта команда не работает в личных сообщениях',
                timestamp = ctx.message.created_at,
                color = discord.Colour.from_rgb(255, 0, 0)
            )
        await ctx.reply(embed = e)
        return

    voice_channel = ctx.author.voice.channel

    if voice_channel is not None:
        if ctx.voice_client is None:
            await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)

        ctx.voice_client.stop()
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5','options': '-vn'}
        YDL_OPTIONS = {'format': 'bestaudio'}
        vc = ctx.voice_client
        print(0)

        with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
            print(1)
            info = ydl.extract_info(str(url), download = False)
        url2 = info['formats'][0]['url']
        source =await discord.FFmpegOpusAudio.from_probe(source = url2, **FFMPEG_OPTIONS)
        print(2)
        vc.play(source)
    else:
        await ctx.reply("Ты должен находиться в каком-то голосовом канале!")

Maybe someone can help...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JiMoon, 2022-02-22
@Jimoon

try my code, it might work:

@bot.command()
  async def play(self, ctx, url: str):
    song_there = os.path.isfile('song.mp3')
    try:
      if song_there:
        os.remove('song.mp3')
        print('[Voice] Удаляю старый файл...')
    except PermissionError:
      print('[Voice] Не удалось удалить старый файл')

    await ctx.send('Пожалуйста, ожидайте...')

    voice = discord.utils.get(self.client.voice_clients, guild = ctx.guild)


    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
      print('[Voice] Загружаю музыку...')
      ydl.download([url])

    for file in os.listdir('./'):
      if file.endswith('.mp3'):
        name = file
        print(f'[Voice] Переименовываю файл: {name}')
        os.rename(file, 'song.mp3')

    voice.play(discord.FFmpegPCMAudio('song.mp3'), after = lambda e: print(f'[Voice] {name} закончила свое проигрывание'))
    voice.source = discord.PCMVolumeTransformer(voice.source)
    voice.source.volume = 0.07

    await ctx.send(f'Сейчас играет: {url}')

and by the way, if you are not writing in notepad, then select all my code except for @bot.command () and press Shift + Tab

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question