首页 > 解决方案 > 带有一组工作音乐命令的小型不和谐机器人,大约 6 天前,播放功能完全停止运行(更多下文)

问题描述

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

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

以下是播放命令的代码:

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

标签: pythonffmpegdiscord.pyyoutube-dlpytube

解决方案


我知道这段代码不能解决你的特定问题,但我有一些用于音乐机器人的 python 代码,而不是在你的设备上下载 mp3 文件,只是流式传输你想要播放的 youtube 歌曲 - 有点像比如 Groovy 和 Rhythm 机器人。它要快得多,而且代码对我来说非常好。干得好:

import asyncio
import discord
import youtube_dl
from discord.ext import commands

# Suppress noise about console usage from errors
youtube_dl.utils.bug_reports_message = lambda: ''


ytdl_format_options = {
    'format': 'bestaudio/best',
    'outtmpl': '%(extractor)s-%(id)s-%(title)s.%(ext)s',
    'restrictfilenames': True,
    'noplaylist': True,
    'nocheckcertificate': True,
    'ignoreerrors': False,
    'logtostderr': False,
    'quiet': True,
    'no_warnings': True,
    'default_search': 'auto',
    'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}

ffmpeg_options = {
    "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5",
    'options': '-vn'
}

ytdl = youtube_dl.YoutubeDL(ytdl_format_options)


class YTDLSource(discord.PCMVolumeTransformer):
    def __init__(self, source, *, data, volume=0.5):
        super().__init__(source, volume)

        self.data = data

        self.title = data.get('title')
        self.url = data.get('url')

    @classmethod
    async def from_url(cls, url, *, loop=None, stream=False):
        loop = loop or asyncio.get_event_loop()
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))

        if 'entries' in data:
            # take first item from a playlist
            data = data['entries'][0]

        filename = data['url'] if stream else ytdl.prepare_filename(data)
        return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)


class Music(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(description="joins a voice channel")
    async def join(self, ctx):
        if ctx.author.voice is None or ctx.author.voice.channel is None:
            return await ctx.send('You need to be in a voice channel to use this command!')

        voice_channel = ctx.author.voice.channel
        if ctx.voice_client is None:
            vc = await voice_channel.connect()
        else:
            await ctx.voice_client.move_to(voice_channel)
            vc = ctx.voice_client

    @commands.command(description="streams music")
    async def play(self, ctx, *, url):
        async with ctx.typing():
            player = await YTDLSource.from_url(url, loop=self.client.loop, stream=True)
            ctx.voice_client.play(player, after=lambda e: print('Player error: %s' % e) if e else None)
        embed = discord.Embed(title="Now playing", description=f"[{player.title}]({player.url}) [{ctx.author.mention}]")
        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(Music(bot))

首先,确保您已下载ffmpegyoutube_dl更新到最新版本。另外,请注意,这段代码是写在一个cog文件中的,所以如果你决定像我一样格式化你的项目,然后cogs在你的项目目录中创建一个名为的文件夹,在文件夹中添加一个新文件(你可以随意命名,但它需要以.py) 结尾,并将上面的代码复制粘贴到此文件中。然后,在您的主.py文件中,添加以下代码以加载 cog:

# loading all cogs
for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        bot.load_extension(f'cogs.{filename[:-3]}')

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


推荐阅读