I’ve been trying to build a bot that records audio from a voice channel on discord and then upload the file to Dropbox. However, none of the methods from the internet really worked (I’ve tried Interactions, discord-ext-voice-recv, discord-ext-audiorec,…). Has anyone really built an audio recording Discord bot in Python & can you guide me in the right direction? Thank you!
Here’s what I’ve got:
@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
await tree.sync()
@tree.command(name="join", description="Join a voice channel")
async def join(interaction: discord.Interaction):
if interaction.user.voice:
channel = interaction.user.voice.channel
if interaction.guild.voice_client is not None:
return await interaction.guild.voice_client.move_to(channel)
await channel.connect()
await interaction.response.send_message("Joined the voice channel")
else:
await interaction.response.send_message("You are not in a voice channel")
@tree.command(name="leave", description="Leave the voice channel")
async def leave(interaction: discord.Interaction):
if interaction.guild.voice_client:
await interaction.guild.voice_client.disconnect()
await interaction.response.send_message("Left the voice channel")
else:
await interaction.response.send_message("I'm not in a voice channel")
@tree.command(name="record", description="Start recording")
async def start_record(interaction: discord.Interaction):
if interaction.guild.voice_client:
interaction.guild.voice_client.start_recording(discord.sinks.MP3Sink(), finished_callback, interaction)
await interaction.response.send_message("Recording...")
else:
await interaction.response.send_message("I'm not in a voice channel")
async def finished_callback(sink, interaction):
recorded_users = [f"<@{user_id}>" for user_id in sink.audio_data]
files = [discord.File(audio.file, f"{user_id}.mp3") for user_id, audio in sink.audio_data.items()]
for file in files:
save_and_upload_recording(file.fp.read(), file.filename)
await interaction.channel.send(f"Finished! Recorded audio for {', '.join(recorded_users)}.", files=files)
@tree.command(name="stop", description="Stop recording and transcribe audio")
async def stop_recording(interaction: discord.Interaction):
if interaction.guild.voice_client:
interaction.guild.voice_client.stop_recording()
await interaction.response.send_message("Stopped!")
else:
await interaction.response.send_message("I'm not recording")
def save_and_upload_recording(wav_bytes, file_name):
with open(file_name, 'wb') as f:
f.write(wav_bytes)
upload_to_dropbox(file_name)
os.remove(file_name)
return file_name
def upload_to_dropbox(file_name):
with open(file_name, "rb") as f:
dbx.files_upload(f.read(), f'/{file_name}', mute=True)
print(f'Uploaded {file_name} to Dropbox')
def transcribe_audio(file_name):
recognizer = sr.Recognizer()
audio = AudioSegment.from_file(file_name)
audio.export(file_name, format="wav")
with sr.AudioFile(file_name) as source:
audio_data = recognizer.record(source)
try:
text = recognizer.recognize_google(audio_data)
except sr.UnknownValueError:
text = "[Could not understand audio]"
except sr.RequestError as e:
text = f"[Could not request results; {e}]"
return text
@tree.command(name="list_recordings", description="List all recordings")
async def list_recordings(interaction: discord.Interaction):
entries = dbx.files_list_folder('').entries
files = [entry.name for entry in entries]
if files:
await interaction.response.send_message('n'.join(files))
else:
await interaction.response.send_message('No recordings found.')
@tree.command(name="delete_recording", description="Delete a recording")
async def delete_recording(interaction: discord.Interaction, file_name: str):
dbx.files_delete(f'/{file_name}')
await interaction.response.send_message(f'Deleted {file_name} from Dropbox')
bot.run(TOKEN)
This did not work. Can anyone guide me in the right direction & tell me which library to use to record audio?
Alexander AI1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.