Python 如何使我的机器人更快地播放音频?(discord.py重写)

Python 如何使我的机器人更快地播放音频?(discord.py重写),python,discord.py,Python,Discord.py,我的音乐大约需要30秒或更长时间才能开始播放音频,我想知道是否有任何方法可以让它像Groovy和许多其他机器人一样更快地播放音频。非常感谢您的帮助。我知道直接从yt流媒体是一件事,我的机器人目前下载并提取文件,然后播放。这是我的密码: @bot.command(pass_context=True, aliases=['p', 'pla']) async def play(ctx, url: str): global voice channel = ctx.message.auth

我的音乐大约需要30秒或更长时间才能开始播放音频,我想知道是否有任何方法可以让它像Groovy和许多其他机器人一样更快地播放音频。非常感谢您的帮助。我知道直接从yt流媒体是一件事,我的机器人目前下载并提取文件,然后播放。这是我的密码:

@bot.command(pass_context=True, aliases=['p', 'pla'])
async def play(ctx, url: str):
    global voice
    channel = ctx.message.author.voice.channel
    voice = get(bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

    await voice.disconnect()

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()
        print(f"The bot has connected to {channel}\n")

    def check_queue():
        Queue_infile = os.path.isdir("./Queue")
        if Queue_infile is True:
            DIR = os.path.abspath(os.path.realpath("Queue"))
            length = len(os.listdir(DIR))
            still_q = length - 1
            try:
                first_file = os.listdir(DIR)[0]
            except:
                print("No more queued song(s)\n")
                queues.clear()
                return
            main_location = os.path.dirname(os.path.realpath(__file__))
            song_path = os.path.abspath(os.path.realpath("Queue") + "\\" + first_file)
            if length != 0:
                print("Song done, playing next queued\n")
                print(f"Songs still in queue: {still_q}")
                song_there = os.path.isfile("song.mp3")
                if song_there:
                    os.remove("song.mp3")
                shutil.move(song_path, main_location)
                for file in os.listdir("./"):
                    if file.endswith(".mp3"):
                        os.rename(file, 'song.mp3')

                voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: check_queue())
                voice.source = discord.PCMVolumeTransformer(voice.source)
                voice.source.volume = 0.07

            else:
                queues.clear()
                return

        else:
            queues.clear()
            print("No songs were queued before the ending of the last song\n")

我不确定是什么原因导致它变慢,但我认为可能与您的pc/主机服务的互联网连接有关,我认为您应该尝试直接从youtube流媒体,以加快速度


队列=[]
YDL_选项=={
“格式”:“最佳音频”,
“后处理器”:[{
“键”:“FFmpegExtractAudio”,
“首选编解码器”:“mp3”,
“首选质量”:“192”,
}],“noplaylist”:“True”
}
FFMPEG_OPTIONS={'before_OPTIONS':'-重新连接1-重新连接流传输1-重新连接延迟最大5','OPTIONS':'-vn'}
def play_next():
queue.pop(0)
如果len(队列)>=1:
source=队列[0]['source']
voice.play(discord.FFmpegPCMAudio(队列[0]['source'],FFMPEG\u选项),after=lambda e:play\u next())
voice.source=discord.PCMVolumeTransformer(voice.source)
voice.source.volume=0.07
def lstr(列表:Iterable):
string=f“{list[0]}”
对于范围(1,len(列表))中的i:
string+=f“{list[i]}”
返回字符串
def搜索_yt(项目):
使用YoutubeDL(YDL_选项)作为YDL:
尝试:
info=ydl.extract_info(“ytsearch:%s”%item,download=False)['entries'][0]
除例外情况外:
返回错误
返回{'source':info['formats'][0]['url'],'title':info['title']}
@命令(pass_context=True,别名=['p','pla'])
异步def播放(ctx,*查询):
query=lstr(查询)
宋=搜索_yt(查询)
queue.append[歌曲]
全球之声
频道=ctx.message.author.voice.channel
voice=get(bot.voice\u客户端,guild=ctx.guild)
如果voice和voice.is_已连接():
等待语音。移动到(频道)
其他:
语音=等待频道。连接()
等待语音。断开连接()
如果voice和voice.is_已连接():
等待语音。移动到(频道)
其他:
语音=等待频道。连接()
打印(f“bot已连接到{channel}\n”)
voice.play(discord.FFmpegPCMAudio(队列[0]['source'],FFMPEG\u选项),after=lambda e:play\u next())
voice.source=discord.PCMVolumeTransformer(voice.source)
voice.source.volume=0.07

或者你可以找一台主机,这会让它快一点;)

也许是一个更好的地方。在你发布之前,请确保阅读,因为有些事情在那里做得不同-例如,问题标题应该简单地说明代码的作用,因为问题总是“我如何改进它?”。确保代码正常工作;如果可能,包括单元测试。您可能会得到一些建议,使其更高效、更易于阅读,并更好地测试。此流式代码不起作用,它表示未定义
Iterable