我正在使用 discord.py 制作音乐机器人,但在播放命令时遇到了一些问题

时间:2021-07-22 22:54:06

标签: python discord.py

机器人有播放命令,可以做 3 件事,如果它不在语音频道中,它会加入语音频道,播放歌曲(显然),如果机器人已经在播放,它会将歌曲存储在一个名为 queues 的字典中一首歌曲,然后在歌曲结束时播放它。要执行此类操作,您需要在 voiceClients after 命令上使用 play 语句,并且它的退出行为似乎很奇怪。我已经放了一些 print 命令来测试机器人,似乎我放在 after 语句上的功能是立即触发而不是在歌曲结束时触发,我不知道这是错误还是如果我做错了什么......无论如何这里是代码:

<块引用>

这是播放命令后播放的函数

def continue(ctx):

   queues[ctx.guild.id].pop(0)

   print(f'function continue: {queues}')
    
   if len(queues[ctx.guild.id]) == 0:
       return

   Songs.play(ctx=ctx ,song=queues[ctx.guild.id][0])
<块引用>

这是播放命令

class Songs(commands.Cog):

   def __init__(self, bot):
       self.client = client


   @commands.command()
   async def play(self, ctx, *, song):

       if ctx.voice_client is None:
           await ctx.message.author.voice.channel.connect()
       else:
           if ctx.voice_client.channel != ctx.message.author.voice.channel:
               await ctx.send('You are not in the same channel as the bot!')
               return
    
       if ctx.guild.id in queues:
           queues[ctx.guild.id].append(song)
       else: 
           queues[ctx.guild.id] = [song]
    
       print(f'function play: {queues}')

       if ctx.voice_client.is_playing():
           await ctx.send('Song added to queue! :thumbsup:')
           return

       ydl_opts = {'format': 'bestaudio'}
       with youtube_dl.YoutubeDL(ydl_opts) as ydl:
           if musica.startswith('https'):
               info = ydl.extract_info(song, download=False)
               URL = info['formats'][0]['url']
           else:
               info = ydl.extract_info(f'ytsearch:{song}', download=False)
               URL = info['entries'][0]['formats'][0]['url']
    

       FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
       ctx.message.guild.voice_client.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after=continue(ctx))

2 个答案:

答案 0 :(得分:1)

啊,你犯了一个经典的 Python 错误。看看这一行:

       ctx.message.guild.voice_client.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after=continue(ctx))

当该行运行时,它要做的第一件事就是调用您的函数 continue。然后它将该函数调用的结果传递给 play 函数。你不想调用函数,你想传递函数对象:

       ctx.message.guild.voice_client.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after=continue)

如果您确实需要那里的上下文,则必须使用 lambda:

       ctx.message.guild.voice_client.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after=lambda: continue(ctx))

答案 1 :(得分:0)

在代码片段的最后一行:

ctx.message.guild.voice_client.play(discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS), after=continue(ctx))

after 关键字参数需要一个可调用的。你没有给它。您正在调用函数 continue(顺便说一句,这不是一个很好的函数名称,因为您隐藏了内置关键字),并将返回值绑定到 after。由于您的可调用对象必须接受参数 ctx,因此快速解决方法是传入一个 lambda:

after=lambda: continue(ctx)