API Reference¶
discord.ext.audiorec module¶
-
class
discord.ext.audiorec.
NativeVoiceClient
(client, channel)[source]¶ Bases:
discord.voice_client.VoiceProtocol
Represent a Discord voice connection
You do not create these , you typically get them from e.g.
connect()
Warning
Due to datagram transmission and reception, the opus library must be installed on your system.
Also, you need to add the location of the ffmpeg binary to the executable path because ffmpeg is used for the audio playback process.
- Parameters
client (
Client
) – The client (or its subclasses) that started the connection request.channel (
Connectable
) – The voice channel that is being connected to.
Examples
@commands.command() async def join(self, ctx: commands.Context): channel: discord.VoiceChannel = ctx.author.voice.channel if ctx.voice_client is not None: return await ctx.voice_client.move_to(channel) await channel.connect(cls=NativeVoiceClient)
-
await
connect
(*, reconnect, timeout)[source]¶ This function is a coroutine.
An abstract method called when the client initiates the connection request.
When a connection is requested initially, the library calls the constructor under
__init__
and then callsconnect()
. Ifconnect()
fails at some point thendisconnect()
is called.Within this method, to start the voice connection flow it is recommended to use
Guild.change_voice_state()
to start the flow. After which,on_voice_server_update()
andon_voice_state_update()
will be called. The order that these two are called is unspecified.
-
await
disconnect
(*, force=False)[source]¶ This function is a coroutine.
An abstract method called when the client terminates the connection.
See
cleanup()
.- Parameters
force (
bool
) – Whether the disconnection was forced.
-
latency
¶ Latency between a HEARTBEAT and a HEARTBEAT_ACK in seconds. This could be referred to as the Discord Voice WebSocket latency and is an analogue of user’s voice latencies as seen in the Discord client.
- Type
-
await
on_voice_server_update
(data)[source]¶ This function is a coroutine.
An abstract method that is called when initially connecting to voice. This corresponds to
VOICE_SERVER_UPDATE
.- Parameters
data (
dict
) –The raw voice server update payload.
-
await
on_voice_state_update
(data)[source]¶ This function is a coroutine.
An abstract method that is called when the client’s voice state has changed. This corresponds to
VOICE_STATE_UPDATE
.- Parameters
data (
dict
) –The raw voice state payload.
-
play
(input, *, after=<function NativeVoiceClient.<lambda>>)[source]¶ Plays Local audiofile
The finalizer,
after
is called after the source has been exhausted or an error occurred.
-
record
(after)[source]¶ Record discord voice stream
The finalizer,
after
is called after the record stopped or an error occurred.- Parameters
after (Callable[[
Exception
], Any]) – The finalizer that is called after voice record is stopped. This function must have a single parameter,error
, that denotes an optional exception that was raised during recording.
-
await
stop_record
(*, loop=None)[source]¶ This function is a coroutine.
Stop recording.
From the time record is called to the time this function is called, audio data in PCM format is stored in the audio buffer in memory.
It is recommended to call this function around 30 seconds after the start of record due to the limitation of voice data transmission capacity.
Otherwise, the memory may be exhausted or the data may not be sent correctly due to over capacity.
- Parameters
loop (
asyncio.AbstractEventLoop
) – The event loop that the voice client is running on.- Returns
PCM audio buffer
- Return type
Optional[bytes]
Examples
@commands.command() async def rec(self, ctx: commands.Context): ctx.voice_client.record(lambda e: print(f"Exception: {e}")) await ctx.send(f'Start Recording') await asyncio.sleep(30) await ctx.invoke(self.bot.get_command('stop')) @commands.command() async def stop(self, ctx: commands.Context): if not ctx.voice_client.is_recording(): return await ctx.send(f'Stop Recording') wav_bytes = await ctx.voice_client.stop_record() wav_file = discord.File(io.BytesIO(wav_bytes), filename="Recorded.wav") if wav_file: await ctx.send(file=wav_file)