Discord.py Bot How to play audio from local files

拟墨画扇 提交于 2021-01-04 06:48:41

问题


Basically the title. I installed ffmpeg and discord.py[audio] already. I just need to learn how it works. couldn't find any tutorial for local audio files. and I cant understand anything from documentations :/


回答1:


That's a function that plays a local audio file. I had problems with FFmpeg, so I hardcoded the .exe path. Also, I had a problem with the file path to the local file, so I put the absolute path in. This function also deletes the command that called it after the audio is done playing, keeping things tidy.

 @bot.command(name="<command_name>")
    async def <function_name>(ctx):
        # Gets voice channel of message author
        voice_channel = ctx.author.channel
        channel = None
        if voice_channel != None:
            channel = voice_channel.name
            vc = await voice_channel.connect()
            vc.play(discord.FFmpegPCMAudio(executable="C:/ffmpeg/bin/ffmpeg.exe", source="C:<path_to_file>"))
            # Sleep while audio is playing.
            while vc.is_playing():
                sleep(.1)
            await vc.disconnect()
        else:
            await ctx.send(str(ctx.author.name) + "is not in a channel.")
        # Delete command after the audio is done playing.
        await ctx.message.delete()


来源:https://stackoverflow.com/questions/63036753/discord-py-bot-how-to-play-audio-from-local-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!