1

Here's the entire code:

import discord
from discord.ext import commands
import yt_dlp

class musicTest(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    async def play(self, ctx, *url):
        FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
        YDL_OPTIONS = {"format":"bestaudio"}

        print('@@ Command \'play\' was called upon!')

        #If author is not in a voice channel
        if ctx.author.voice is None:
            print('@@ Author is not in a voice channel. Warning them!')
            await ctx.send("You're not in a channel")
            return

        print('@@ Author is in a channel, proceeding!')

        #Store the voice channel
        voice_channel = ctx.author.voice.channel
        print('@@ Channel name has been identified!')

        #If bot is not in a voice channel, wait until connected
        #else move to the correct voice channel (switching channels)
        if ctx.voice_client is None:
            print('@@ Bot is not in a voice channel!')
            await voice_channel.connect()
            print('@@ Bot has entered the voice channel!')
        else:
            print('@@ Bot is connected in the wrong voice channel!')
            await ctx.voice_client.move_to(voice_channel)
            print('@@ Bot is switching to the correct voice channel!')


        if ".com" in url:
            print('@@ A youtube LINK has been requested!')
            with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
                info = ydl.extract_info(url, download=False)
                url2 = info['url']
                source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS, executable=r"C:\XXXX\ffmpeg.exe")
                await ctx.channel.send("Playing requested song")
                try:
                    print('@@ Trying to stream the music!')
                    ctx.voice_client.play(source)
                except Exception as e:
                    print('@@ Streaming FAILED!')
                    await ctx.send(f'Error (.com): {e}')

        else:
            with yt_dlp.YoutubeDL(YDL_OPTIONS) as ydl:
                info = ydl.extract_info(f"ytsearch:{url}", download=False)
                urlx = info['entries'][0]
                url2 = urlx['url']
                print(url2)
                source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS,executable=r"C:\Users\XXXX\ffmpeg.exe")
                await ctx.channel.send(f"Playing: {urlx['title']}")
                try:
                    print('@@ Trying to stream the music!')
                    ctx.voice_client.play(source)
                except Exception as e:
                    print('@@ Streaming FAILED!')
                    await ctx.send(f'Error (raw query): {e}')


###################################################################################

def setup(client):
    client.add_cog(musicTest(client))

Here's the weird behavior happening:

Let's say my command is: "!play dua lipa love again"...

  1. The bot will join the channel and stop there (in the python console, it will say that the bot is not in a channel, despite being there)
  2. I have to type !play dua lipa love again a 2ND time in order for it to initiate the youtube searching/streaming part of the code
  3. It fails to try playing audio

From the print statements in my code, here's what happens after the FIRST !play dua lipa love again:

Bot is ready.
@@ Command 'play' was called upon!
@@ Author is in a channel, proceeding!
@@ Channel name has been identified!
@@ Bot is not in a voice channel!

It stops there (the bot thinks that it's not in a channel)....then I type, for a SECOND time !play dua lipa love again and here's what happens:

@@ Command 'play' was called upon!
@@ Author is in a channel, proceeding!
@@ Channel name has been identified!
@@ Bot is connected in the wrong voice channel!
@@ Bot is switching to the correct voice channel!
[youtube:search] Extracting URL: ytsearch:('dua', 'lipa', 'love', 'again')
[download] Downloading playlist: ('dua', 'lipa', 'love', 'again')
[youtube:search] query "('dua', 'lipa', 'love', 'again')": Downloading web client config
[youtube:search] query "('dua', 'lipa', 'love', 'again')" page 1: Downloading API JSON
[youtube:search] Playlist ('dua', 'lipa', 'love', 'again'): Downloading 1 items of 1
[download] Downloading item 1 of 1
[youtube] Extracting URL: https://www.youtube.com/watch?v=BC19kwABFwc
[youtube] BC19kwABFwc: Downloading webpage
[youtube] BC19kwABFwc: Downloading android player API JSON
[download] Finished downloading playlist: ('dua', 'lipa', 'love', 'again')
https://XXXXXX....
@@ Trying to stream the music!
@@ Streaming FAILED!

And here's the error it shows in the discord chat:

MyBot:

Playing: Dua Lipa - Love Again (Official Music Video)

Error (raw query): Not connected to voice.

I'm guessing that having to type the command twice before it actually tries to use yt-dlp is a symptom of a larger problem which in turn causes the bot to not play the audio.

I'm lost as to what's going on. This is practically the same code I used with youtube_dl (old outdated version of yt-dlp) before it broke.

Yen
  • 59
  • 5

0 Answers0