首页 > 解决方案 > 我正在用 discord.py 制作一个音乐机器人,但我在播放命令时遇到了一些问题

问题描述

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

标签: pythondiscord.py

解决方案


啊,你犯了 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))

推荐阅读