Python 大约6天前,带有一组工作音乐命令的小型discord机器人的播放功能完全停止运行(详见下文)

Python 大约6天前,带有一组工作音乐命令的小型discord机器人的播放功能完全停止运行(详见下文),python,ffmpeg,discord.py,youtube-dl,pytube,Python,Ffmpeg,Discord.py,Youtube Dl,Pytube,这是一个用于我的服务器的小型discord python bot,具有各种功能,其中包括音乐命令,直到它们突然停止,并且没有显示任何错误。 它使用FFMpeg,youtubeDl和pytube收集歌曲并存储在本地播放,我让pip更新了所有这些,它们肯定在当前版本上,因为我已经在网上确认了这一点。 任何人提供的任何帮助或见解都将不胜感激。我很抱歉,如果代码的编写方式很复杂,我对编码还是相当陌生的,这是我第一个真正的大型项目之一 如果你需要任何信息,我很乐意尽我所能提供帮助 以下是播放命令的代码:

这是一个用于我的服务器的小型discord python bot,具有各种功能,其中包括音乐命令,直到它们突然停止,并且没有显示任何错误。 它使用FFMpeg,youtubeDl和pytube收集歌曲并存储在本地播放,我让pip更新了所有这些,它们肯定在当前版本上,因为我已经在网上确认了这一点。 任何人提供的任何帮助或见解都将不胜感激。我很抱歉,如果代码的编写方式很复杂,我对编码还是相当陌生的,这是我第一个真正的大型项目之一

如果你需要任何信息,我很乐意尽我所能提供帮助

以下是播放命令的代码:

@client.command()
async def play(ctx, *args):
    global queu
    #global autom
    if not args:
        voice = get(client.voice_clients, guild=ctx.guild)
        if not voice.is_playing():
            server = ctx.message.guild
            voice_channel = server.voice_client
            if queu:
                async with ctx.typing():
                    player = await YTDLSource.from_url(queu[0], loop=client.loop)
                    voice_channel.play(player, after=lambda e: print('Player error: %s' % e) if e else None)

                await ctx.send('**Now playing:** {}'.format(player.title))
                del(queu[0])
                # while autom == True:
                #     try:
                #         a = client.get_command('auto')
                #         await ctx.invoke(a)
                #     except:
                #         print('')
            elif not queu:
                await ctx.send("You can't play if there isn't anything in the queue\nIf auto mode was on it has now been disabled, to use it gain please add to the queue and run ``;auto on``")
                autom = False
    if args:
        global gueu
        search_keywords = ""
        print(args)
        for word in args:
            search_keywords += word
            search_keywords += '+'
        link = "https://www.youtube.com/results?search_query="
        link += search_keywords
        #print(link)
        html = urllib.request.urlopen(link)
        video_ids = re.findall(r"watch\?v=(\S{11})", html.read().decode())
        url = ("https://www.youtube.com/watch?v=" + video_ids[0])
        #print(url)
        queu.append(url)
        #print(queu)
        await ctx.send("``{}`` added to queue!\n If the song doesn't start please either let the current song end and run ``;play``/``;next`` again or run ``;next`` to play now".format(url))
        try:
            p = client.get_command('play')
            await ctx.invoke(p)
        except:
            print('failed')

我知道这段代码不会解决您的特定问题,但我有一些音乐机器人的python代码,它将不在您的设备上下载mp3文件,而是流式播放您想要播放的youtube歌曲,有点像Groovy和节奏机器人。这要快得多,而且代码对我来说非常好。给你:

导入异步IO
进口不和
导入youtube\u dl
从discord.ext导入命令
#抑制错误引起的有关控制台使用的噪音
youtube\u dl.utils.bug\u报告\u消息=lambda:“”
ytdl_格式_选项={
“格式”:“最佳音频/最佳”,
“outtmpl”:“%(提取器)s-%(id)s-%(标题)s.%(ext)s”,
“RestrictFileName”:True,
'noplaylist':True,
“nocheckcertificate”:正确,
“忽略错误”:False,
“logtostderr”:False,
“安静”:没错,
“无警告”:正确,
“默认搜索”:“自动”,
“源地址”:“0.0.0.0”绑定到ipv4,因为ipv6地址有时会导致问题
}
ffmpeg_选项={
“之前的选项”:“-重新连接1-重新连接流式1-重新连接延迟最多5”,
'选项':'-vn'
}
ytdl=youtube\u dl.YoutubeDL(ytdl\u格式\u选项)
YTDLSource类(discord.PCMVolumeTransformer):
def u u init _;(self,source,*,data,volume=0.5):
super().\uuuu init\uuuu(源、卷)
self.data=数据
self.title=data.get('title')
self.url=data.get('url')
@类方法
来自url的异步定义(cls,url,*,循环=无,流=假):
loop=循环或异步IO.get\u event\u loop()
数据=等待循环。在执行器中运行(无,lambda:ytdl。提取信息(url,下载=非流))
如果数据中有“条目”:
#从播放列表中选取第一项
数据=数据['entries'][0]
filename=data['url']如果流是ytdl。准备文件名(数据)
返回cls(discord.FFmpegPCMAudio(文件名,**ffmpeg_选项),data=data)
课堂音乐(commands.Cog):
定义初始化(自我,客户机):
self.client=client
@commands.command(description=“加入语音频道”)
异步def联接(自、ctx):
如果ctx.author.voice为None或ctx.author.voice.channel为None:
return wait ctx.send('您需要在语音频道中才能使用此命令!')
语音频道=ctx.author.voice.channel
如果ctx.voice_客户端为无:
vc=等待语音信道。连接()
其他:
等待ctx.voice\u客户端。移动到(voice\u频道)
vc=ctx.voice\u客户端
@commands.command(description=“streams music”)
异步def播放(self、ctx、*、url):
与ctx.typing()异步:
player=wait YTDLSource.from_url(url,loop=self.client.loop,stream=True)
ctx.voice_client.play(player,after=lambda e:print('player错误:%s'%e),如果没有其他错误)
embed=discord.embed(title=“Now playing”,description=f”[{player.title}]({player.url})[{ctx.author.title}]))
等待ctx.send(嵌入=嵌入)
def设置(机器人):
bot.add_cog(音乐(bot))
首先,确保下载并更新了
ffmpeg
youtube\u dl
的最新版本。另外,请注意,此代码是在
cog
文件中编写的,因此如果您决定按我的方式格式化您的项目,请在项目目录中创建一个名为
cogs
的文件夹,将一个新文件添加到该文件夹中(您可以随意命名它,但它需要以
.py
结尾),然后将上面的代码复制粘贴到此文件中。然后,在主
.py
文件中,添加以下代码以加载cog:

#加载所有齿轮
对于os.listdir('./cogs')中的文件名:
如果filename.endswith('.py'):
加载扩展名(f'cogs.{filename[:-3]}')

或者,您可以选择简单地将音乐代码添加到主
.py
文件中,而不是创建
cogs
文件夹,在这种情况下,您将不需要上述代码。

执行播放命令时是否有任何错误?您是否进行了任何可能影响it突然停止工作的更改?