【问题标题】:I'm making a music bot with discord.py and I'm having some trouble with the play command我正在用 discord.py 制作一个音乐机器人,但我在播放命令时遇到了一些问题
【发布时间】:2021-09-30 04:11:47
【问题描述】:

机器人有播放命令,它执行 3 件事,如果它不在语音通道中,它会加入语音通道,它播放歌曲(显然),如果机器人已经在播放,它会将歌曲存储在一个名为 queues 的字典中一首歌曲,然后在歌曲结束时播放。要执行此类操作,您需要 voiceClients play 命令上的 after 语句,它的行为似乎很奇怪。我已经放了一些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))

【问题讨论】:

    标签: python discord.py


    【解决方案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))
    

    【讨论】:

      【解决方案2】:

      在你的 sn-p 的最后一行:

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

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

      after=lambda: continue(ctx)
      

      【讨论】:

        猜你喜欢
        • 2021-05-05
        • 2021-10-25
        • 1970-01-01
        • 2022-11-03
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 2019-05-05
        相关资源
        最近更新 更多