Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"No such file or directory" discord.py – FileNotFound is not working

I’m trying to make a Discord bot with sound functions (like a soundboard). The command should play a sound, which the user types in the command. If the Sound doesn’t exist, it should send a message, that it doesn’t exist. But if the Error lib/media/sounds/foo.mp3: No such file or directory occurs it doesn’t react and doesn’t send a message.

@bot.command()
@commands.cooldown(1,5,commands.BucketType.user)
async def sound(ctx, sound : str):
    await done(ctx)
    user = ctx.message.author
    if user.voice is None:
        await ctx.send("Du bist in keinem Sprachkanal")
        add_log('user is in no voice channel', cat=err)
    voiceChannel = user.voice.channel
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    if voice is None:
        await voiceChannel.connect()
        add_log('connected to: ' + str(voiceChannel), cat=i)
    voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
    try:
        voice.play(discord.FFmpegPCMAudio(f"lib/media/sounds/{sound}.mp3"))
    except FileNotFoundError:
        await ctx.send("Diesen Sound gibt es nicht!")

I hope thats enough information

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

FFmpegPCMAudio calls ffmpeg in a subprocess, that’s why Python doesn’t raise FileNotFound.

If you check the docs (linked) you will see the function raises ClientException. You must catch this exception for your code to work. EDIT: Apparently it doesn’t raise any exception.

You can check if the file exists using os.path.isfile

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading