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
>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