Skip to content

Commit

Permalink
Improved example docs for audio_recording (#1409)
Browse files Browse the repository at this point in the history
Co-authored-by: Lala Sabathil <[email protected]>
Co-authored-by: Dorukyum <[email protected]>
  • Loading branch information
3 people authored Jun 28, 2022
1 parent 08aa4a2 commit 525d953
Showing 1 changed file with 35 additions and 47 deletions.
82 changes: 35 additions & 47 deletions examples/audio_recording.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,63 @@
from enum import Enum

import discord

bot = discord.Bot(debug_guilds=[...])
bot.connections = {}
connections = {}


@bot.command()
@discord.option(
"encoding",
choices=[
"mp3",
"wav",
"pcm",
"ogg",
"mka",
"mkv",
"mp4",
"m4a",
],
)
async def start(ctx: discord.ApplicationContext, encoding: str):
"""Record your voice!"""
class Sinks(Enum):
mp3 = discord.sinks.MP3Sink()
wav = discord.sinks.WaveSink()
pcm = discord.sinks.PCMSink()
ogg = discord.sinks.OGGSink()
mka = discord.sinks.MKASink()
mkv = discord.sinks.MKVSink()
mp4 = discord.sinks.MP4Sink()
m4a = discord.sinks.M4ASink()


async def finished_callback(sink, channel: discord.TextChannel, *args):
recorded_users = [f"<@{user_id}>" for user_id, audio in sink.audio_data.items()]
await sink.vc.disconnect()
files = [
discord.File(audio.file, f"{user_id}.{sink.encoding}")
for user_id, audio in sink.audio_data.items()
]
await channel.send(
f"Finished! Recorded audio for {', '.join(recorded_users)}.", files=files
)


@bot.command()
async def start(ctx: discord.ApplicationContext, sink: Sinks):
"""
Record your voice!
"""
voice = ctx.author.voice

if not voice:
return await ctx.respond("You're not in a vc right now")

vc = await voice.channel.connect()
bot.connections.update({ctx.guild.id: vc})

encodings = {
"mp3": discord.sinks.MP3Sink,
"wav": discord.sinks.WaveSink,
"pcm": discord.sinks.PCMSink,
"ogg": discord.sinks.OGGSink,
"mka": discord.sinks.MKASink,
"mkv": discord.sinks.MKVSink,
"mp4": discord.sinks.MP4Sink,
"m4a": discord.sinks.M4ASink
}

possible_sink = encodings.get(encoding)

if possible_sink is None:
return await ctx.respond("Invalid encoding.")

sink = possible_sink()
connections.update({ctx.guild.id: vc})

vc.start_recording(
sink,
sink.value,
finished_callback,
ctx.channel,
)

await ctx.respond("The recording has started!")


async def finished_callback(sink, channel: discord.TextChannel, *args):
recorded_users = [f"<@{user_id}>" for user_id, audio in sink.audio_data.items()]
await sink.vc.disconnect()
files = [discord.File(audio.file, f"{user_id}.{sink.encoding}") for user_id, audio in sink.audio_data.items()]
await channel.send(f"Finished! Recorded audio for {', '.join(recorded_users)}.", files=files)


@bot.command()
async def stop(ctx: discord.ApplicationContext):
"""Stop recording."""
if ctx.guild.id in bot.connections:
vc = bot.connections[ctx.guild.id]
if ctx.guild.id in connections:
vc = connections[ctx.guild.id]
vc.stop_recording()
del bot.connections[ctx.guild.id]
del connections[ctx.guild.id]
await ctx.delete()
else:
await ctx.respond("Not recording in this guild.")
Expand Down

0 comments on commit 525d953

Please sign in to comment.