discord.py voice recording bot in Discord problem

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?

New contributor

Alexander AI1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật