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

Small discord bot with a set of working music commands, about 6 days ago the play function completely stopped functioning (more below)

这是我的服务器的一个小型 discord python 机器人,具有多种功能,其中包括音乐命令,直到它们突然停止而没有显示任何问题错误。 它使用 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 和 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 主文件中,添加此代码以加载齿轮:

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

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