Add async context manager to VoiceClient (#67)
Some checks failed
Deploy MkDocs / deploy (push) Has been cancelled
Some checks failed
Deploy MkDocs / deploy (push) Has been cancelled
This commit is contained in:
parent
def2ff0183
commit
8e48da3bee
@ -261,3 +261,18 @@ class VoiceClient:
|
|||||||
self._udp_receive_thread.join(timeout=1)
|
self._udp_receive_thread.join(timeout=1)
|
||||||
if self._sink:
|
if self._sink:
|
||||||
self._sink.close()
|
self._sink.close()
|
||||||
|
|
||||||
|
async def __aenter__(self) -> "VoiceClient":
|
||||||
|
"""Enter the context manager by connecting to the voice gateway."""
|
||||||
|
await self.connect()
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(
|
||||||
|
self,
|
||||||
|
exc_type: Optional[type],
|
||||||
|
exc: Optional[BaseException],
|
||||||
|
tb: Optional[BaseException],
|
||||||
|
) -> bool:
|
||||||
|
"""Exit the context manager and close the connection."""
|
||||||
|
await self.close()
|
||||||
|
return False
|
||||||
|
@ -9,15 +9,17 @@ import asyncio
|
|||||||
import os
|
import os
|
||||||
import disagreement
|
import disagreement
|
||||||
|
|
||||||
vc = disagreement.VoiceClient(
|
async def main():
|
||||||
os.environ["DISCORD_VOICE_ENDPOINT"],
|
async with disagreement.VoiceClient(
|
||||||
os.environ["DISCORD_SESSION_ID"],
|
os.environ["DISCORD_VOICE_ENDPOINT"],
|
||||||
os.environ["DISCORD_VOICE_TOKEN"],
|
os.environ["DISCORD_SESSION_ID"],
|
||||||
int(os.environ["DISCORD_GUILD_ID"]),
|
os.environ["DISCORD_VOICE_TOKEN"],
|
||||||
int(os.environ["DISCORD_USER_ID"]),
|
int(os.environ["DISCORD_GUILD_ID"]),
|
||||||
)
|
int(os.environ["DISCORD_USER_ID"]),
|
||||||
|
) as vc:
|
||||||
|
await vc.send_audio_frame(b"...")
|
||||||
|
|
||||||
asyncio.run(vc.connect())
|
asyncio.run(main())
|
||||||
```
|
```
|
||||||
|
|
||||||
After connecting you can send raw Opus frames:
|
After connecting you can send raw Opus frames:
|
||||||
@ -41,7 +43,7 @@ You can switch sources while connected:
|
|||||||
await vc.play(FFmpegAudioSource("other.mp3"))
|
await vc.play(FFmpegAudioSource("other.mp3"))
|
||||||
```
|
```
|
||||||
|
|
||||||
Call `await vc.close()` when finished.
|
The connection will be closed automatically when leaving the `async with` block.
|
||||||
|
|
||||||
## Fetching Available Voice Regions
|
## Fetching Available Voice Regions
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user