Discord.py Bot如何播放本地文件中的音频 [英] Discord.py Bot How to play audio from local files

本文介绍了Discord.py Bot如何播放本地文件中的音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!


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

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



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.

    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():
            await vc.disconnect()
            await ctx.send(str(ctx.author.name) + "is not in a channel.")
        # Delete command after the audio is done playing.
        await ctx.message.delete()

这篇关于Discord.py Bot如何播放本地文件中的音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
发送“验证码”获取 | 15天全站免登陆