Discord.py Bot如何从本地文件播放音频

时间:2020-07-22 14:35:05

标签: python audio bots discord.py

基本上是标题。我已经安装了ffmpeg和discord.py [audio]。我只需要学习它是如何工作的。找不到本地音频文件的任何教程。而且我无法从文档中了解任何内容:/

1 个答案:

答案 0 :(得分:2)

该功能可播放本地音频文件。我在FFmpeg上遇到问题,因此我对.exe路径进行了硬编码。另外,我在本地文件的文件路径上遇到了问题,因此我输入了绝对路径。在播放完音频后,此功能还删除了调用它的命令,使内容保持整洁。

 @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()