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

feature: Allow passing an awaitable after function to AudioPlayer #448

Merged
merged 4 commits into from
Mar 11, 2022

Conversation

anoadragon453
Copy link
Contributor

Summary

This enables awaitable (async) functions to be called after an audio file has finished playing in voice calls.

I have been long frustrated with needing to provide a non-awaitable function to VoiceClient.play, only to have that function need to use asyncio.run_coroutine_threadsafe to further call async code.

By checking whether we receive an awaitable when calling self.after, and awaiting on it, we add support for awaitable functions.

I have lightly tested this with success in my own Discord bot. Providing both async and non-async functions as the after arugment to VoiceClient.play has resulted in them both being called successfully.

Checklist

  • If code changes were made then they have been tested.
    • I have updated the documentation to reflect the changes.
  • This PR fixes an issue.
  • This PR adds something new (e.g. new method or parameters).
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

This enables awaitable functions to be called after an audio file has
finished playing in voice calls.
@netlify
Copy link

netlify bot commented Feb 12, 2022

✔️ Deploy Preview for nextcord-gh-action ready!

🔨 Explore the source changes: b48e423

🔍 Inspect the deploy log: https://app.netlify.com/sites/nextcord-gh-action/deploys/62093273b5c1c00008f78969

😎 Browse the preview: https://deploy-preview-448--nextcord-gh-action.netlify.app

@@ -36,7 +36,19 @@
import re
import io

from typing import Any, Callable, Generic, IO, Optional, TYPE_CHECKING, Tuple, Type, TypeVar, Union
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not entirely obvious from the diff, but all that was changed here was Awaitable being added.

@anoadragon453
Copy link
Contributor Author

CI failure appears unrelated to this code change.

Copy link
Collaborator

@Skelmis Skelmis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm, I haven't tested.

*Edit, you should probably update the docs

@anoadragon453
Copy link
Contributor Author

anoadragon453 commented Feb 13, 2022

After digging into asyncio some more, I found that asyncio.run_coroutine_threadsafe will only accept a coroutine, not any Awaitable type (Tasks, Futures...). Therefore, I've reduced the scope of what we'll run to only coroutines in e995433.

@Skelmis thank you for the suggestion! I've updated the documentation in fbfab79 to note that coroutines may be passed.

nextcord/player.py Show resolved Hide resolved
@ooliver1 ooliver1 merged commit 87d7866 into nextcord:master Mar 11, 2022
ooliver1 pushed a commit that referenced this pull request Apr 9, 2022
…#448)

* Allow passing an awaitable after function to AudioPlayer

This enables awaitable functions to be called after an audio file has
finished playing in voice calls.

* Scope acceptable functions to coroutines, not any awaitable

* Update documentation to note after may be a coroutine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants