Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VoiceClient.play() doesn't pass FFMPEG error to 'after' function. #9866

Open
3 tasks done
SpongeManiac opened this issue Jun 15, 2024 · 2 comments
Open
3 tasks done
Labels
unconfirmed bug A bug report that needs triaging

Comments

@SpongeManiac
Copy link

SpongeManiac commented Jun 15, 2024

Summary

VoiceClient.play() doesn't pass FFMPEG error to 'after' function.

Reproduction Steps

I know from #5131 that the session invalidation error is not a Discord.py error, however, the VoiceClient's play() function does not seem to be capturing and passing the FFMPEG error into my provided after function. I would like to notify when this error happens, so capturing this error would be very helpful. It also appears that Discord.py wrongly states that FFMPEG exited successfully, even when it exits with code 1, which by definition, is not successful.

Here is my current code and log results:
Code:

def sync_playback_error(error: Exception = None):
    async def playback_error(error: Exception = None):
        has_error: bool = error != None
        if has_error:
            print('Sending error to chat...')
            await ctx.message.channel.send('A playback error has caused the audio stream to stop.')
        else:
            print('No error in playback!')
    print('Starting async callback...')
    asyncio.run_coroutine_threadsafe(playback_error(error), self.bot.loop)

# Attempt to play ffmpeg source
vc.play(ffmpeg_src, after=sync_playback_error)

Logs:

[tls @ 0x5632a384eb80] Error in the pull function.
[matroska,webm @ 0x5632a384a600] Read error
[tls @ 0x5632a384eb80] The specified session has been invalidated for some reason.
    Last message repeated 1 times
Starting async callback...
DEBUG:asyncio:Using selector: EpollSelector
No error in playback!
2024-06-15 16:58:28 INFO     discord.player ffmpeg process 2801802 successfully terminated with return code of 1.

Minimal Reproducible Code

No response

Expected Results

I expected the FFMPEG error to be passed to the after function

Actual Results

The error was not captured and passed to the after function

Intents

discord.Intents.default()

System Information

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

@SpongeManiac SpongeManiac added the unconfirmed bug A bug report that needs triaging label Jun 15, 2024
@SpongeManiac
Copy link
Author

SpongeManiac commented Jun 16, 2024

Also, while I'm at it, Is there a way to put an async function in the after parameter, or do I just use the method that I've implemented?

@CptDarkrex
Copy link

import discord
import asyncio

# Define the async error handling function
async def playback_error(error: Exception):
    if error:
        print(f'Error in playback: {error}')
        await ctx.message.channel.send(f'A playback error has caused the audio stream to stop: {error}')
    else:
        print('No error in playback!')

# Wrapper to run the async function
def sync_playback_error(error: Exception):
    asyncio.run_coroutine_threadsafe(playback_error(error), bot.loop)

# Attempt to play ffmpeg source
vc.play(ffmpeg_src, after=sync_playback_error)

Explanation:

  1. playback_error Function: This async function logs the error and sends a message to the Discord channel if an error occurs.
  2. sync_playback_error Function: This function wraps the async playback_error function and ensures it runs in the bot's event loop.
  3. vc.play Call: Passes the sync_playback_error function to the after parameter of vc.play, which will be called when playback finishes or if an error occurs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unconfirmed bug A bug report that needs triaging
Projects
None yet
Development

No branches or pull requests

2 participants