Programmieren & Softwareentwicklung

Wenn Du nicht nur Anwender sein willst, sondern auch aktiv programmierst oder mit dem Gedanken spielst, dann warten hier die passenden Fragen und Antworten auf Dich.

26.800 Beiträge

Ask Me Anything: Themenspecials

Discord.py, ffmpeg und pytube um audio wiederzugeben?

ich habe versucht einen discord,py bot zum audio abspielen zu programieren, jedoch wird das audio immer wieder mitten drin abbrubt abgebrochen.

@bot.command()
async def play(ctx, url: str):
    global ffmpeg_executable
    if discord.utils.get(ctx.guild.roles, name='music'):
        role = discord.utils.get(ctx.guild.roles, name='music')
        if role in ctx.author.roles:
            voice_client = ctx.voice_client


            if voice_client is None:
                channel = ctx.author.voice.channel
                voice_client = await channel.connect()
                print("Bot joined voice channel successfully.")


            try:
                # Fetch the audio stream URL using pytube
                yt = YouTube(url)
                audio_stream = yt.streams.filter(only_audio=True).first()


                # Play the audio stream using the VoiceProtocol
                if voice_client.is_playing():
                    voice_client.stop()
                voice_client.play(discord.FFmpegPCMAudio(audio_stream.url, executable=ffmpeg_executable))
            except Exception as e:
                await ctx.reply(f"An error occurred: {str(e)}")
        else:
            await ctx.reply('You don\'t have the permission for this command!')
    else:
        await ctx.reply('This Feature is not available on this guild, ask an Admin to add it!')