0

My question is this: Why isn't my bot playing audio?

I want the bot to join, play audio from queue, then disconnect without downloading an mp3 file.

I tried using youtube-dl, but I switched to the yt-dlp library after getting errors I couldn't fix. I am running on Windows 10 locally. All my libraries are up to date.

Here are my ydl_opts and FFMPEG_OPTS:

ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'mp3',
        'preferredquality': '192',
    }],
}

FFMPEG_OPTIONS = {
    'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
    'options': '-vn'
} 

Here is where I believe the problem is.

async def play():
    print("Play Called")
    musicPlay()
    # Get message object from initial request
    message = ytLinkQue.get()
    print(f"Message object recieved: {message}")
    voiceChannel = message.author.voice.channel
    vc = await voiceChannel.connect()
    songsPlayed = 0
    
    while not ytLinkQue.empty():
        # Get current song
        currentSong = ytLinkQue.get()[0]
        print(f"Current song: {currentSong}")

        # Get song from Youtube
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            # song = ydl.download(currentSong)
            info = ydl.extract_info(currentSong, download=False)
            song = info['formats'][0]['url']

        # Play Song
        vc.play(discord.FFmpegPCMAudio(song, **FFMPEG_OPTIONS), after=lambda e: print('Song done'))

        # Wait until the song has finished playing
        while vc.is_playing():
            print("playing rn")
            await asyncio.sleep(1)
    
    await vc.disconnect()
    musicStop()

When play() is called, here is the output in terminal with my annotations as **** text ****:

>python main.py
2023-02-17 15:21:09 INFO     discord.client logging in using static token
2023-02-17 15:21:10 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: 60b9fce14faa5daa4aed9eb6db01a74d).
Max que: 50
Text Channel: 828698708123451434
Testing Bot#4591 is ready.
Passing message object
**** play() funciton is called ****
Play Called
Message object recieved: <Message id=1076267109782401186 channel=<TextChannel id=828698708123451434 name='bot-testing' position=7 nsfw=False news=False category_id=None> type=<MessageType.default: 0> author=<Member id=264225001492840448 name='Frank7?' discriminator='0199' bot=False nick='Fragnk7?' guild=<Guild id=261601676941721602 name='Wall Moment' shard_id=0 chunked=True member_count=57>> flags=<MessageFlags value=0>>
2023-02-17 15:21:16 INFO     discord.voice_client Connecting to voice...
2023-02-17 15:21:16 INFO     discord.voice_client Starting voice handshake... (connection attempt 1)
2023-02-17 15:21:17 INFO     discord.voice_client Voice handshake complete. Endpoint found seattle2004.discord.media
Current song: https://www.youtube.com/watch?v=vcAp4nmTZCA
[youtube] Extracting URL: https://www.youtube.com/watch?v=vcAp4nmTZCA 
[youtube] vcAp4nmTZCA: Downloading webpage 
[youtube] vcAp4nmTZCA: Downloading android player API JSON 
**** Does not play any audio ****
Playing rn
Song done
2023-02-17 15:21:18 INFO     discord.player ffmpeg process 20700 successfully terminated with return code of 1.
2023-02-17 15:21:19 INFO     discord.voice_client The voice handshake is being terminated for Channel ID 400178308467392513 (Guild ID 261601676941721602)
2023-02-17 15:21:19 INFO     discord.voice_client Disconnecting from voice normally, close code 1000.

On Discord's end, the bot successfully connects then disconnects after 2 second.

Note: I've only included code I think is relevant. Please let me know if I should add anything else to the post, otherwise, here is the github for the project. Code is in main.py. https://github.com/LukeLeimbach/wallMomentMusic

Thank you in advance!

I've applied the advice from these posts but it still will not play audio:

-https://stackoverflow.com/questions/45770016/how-do-i-make-my-discord-bot-play-audio-from-youtube

-https://stackoverflow.com/questions/66070749/how-to-fix-discord-music-bot-that-stops-playing-before-the-song-is-actually-over?newreg=c70dd786cf5844e490045494223c0381

-https://stackoverflow.com/questions/57688808/playing-music-with-a-bot-from-youtube-without-downloading-the-file

1 Answers1

0

There's A Problem With Your URL it doesn't return a streaming URL in yt_dlp they reworked the JSON data

so you would replace:

song = info['formats'][0]['url']

with:

song = info["url"]

Hope This Helps