Skip to content

Commit 5642422

Browse files
authored
Update voice receive example (#843)
* Use BytesIO for sink * Update voice receive example
1 parent 3e33987 commit 5642422

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

examples/audio_recording.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88

99
@bot.command()
10-
async def start(ctx: ApplicationContext, encoding: Option(str, choices=["mp3", "wav", "pcm", "ogg", "mka", "mkv", "mp4", "m4a",])):
10+
async def start(ctx: ApplicationContext,
11+
encoding: Option(str, choices=["mp3", "wav", "pcm", "ogg", "mka", "mkv", "mp4", "m4a", ])):
1112
"""
1213
Record your voice!
1314
"""
@@ -36,26 +37,28 @@ async def start(ctx: ApplicationContext, encoding: Option(str, choices=["mp3", "
3637
sink = discord.sinks.MP4Sink()
3738
elif encoding == "m4a":
3839
sink = discord.sinks.M4ASink()
40+
else:
41+
return await ctx.respond("Invalid encoding.")
3942

4043
vc.start_recording(
4144
sink,
4245
finished_callback,
4346
ctx.channel,
4447
)
45-
48+
4649
await ctx.respond("The recording has started!")
4750

4851

4952
async def finished_callback(sink, channel: discord.TextChannel, *args):
50-
5153
recorded_users = [
52-
f" <@{user_id}> ({os.path.split(audio.file)[1]}) "
54+
f"<@{user_id}>"
5355
for user_id, audio in sink.audio_data.items()
5456
]
5557
await sink.vc.disconnect()
56-
files = sink.get_all_audio()
58+
files = [discord.File(audio.file, f"{user_id}.{sink.encoding}") for user_id, audio in sink.audio_data.items()]
5759
await channel.send(f"Finished! Recorded audio for {', '.join(recorded_users)}.", files=files)
5860

61+
5962
@bot.command()
6063
async def stop(ctx):
6164
"""
@@ -70,4 +73,4 @@ async def stop(ctx):
7073
await ctx.respond("Not recording in this guild.")
7174

7275

73-
bot.run("TOKEN")
76+
bot.run("TOKEN")

0 commit comments

Comments
 (0)