提问者:小点点

我如何让我的音乐机器人消息什么歌曲正在队列中播放discord.py


对于我的不和谐音乐机器人,我结合了一个队列和播放命令。所以每当我想排队时,我可以使用播放命令。我的问题是我的机器人只向我播放的第一首歌曲发送消息。之后,队列继续播放而不发送消息。

例如,第一次显示Now PLAYING:(歌曲名称),但之后不显示。我希望每次队列中的新歌播放时,它都继续显示此内容。

这是代码:

queue = {}
@commands.command(pass_context = True)
    async def play(self, ctx, *, info):

        def check_queue(ctx, id):
            if queue[id] != {}:
                voice_client = ctx.guild.voice_client
                source = queue[id].pop(0)
                voice_client.play(source, after=lambda x=0: check_queue(ctx, ctx.message.guild.id))

        try:
            voice_channel = ctx.author.voice.channel # checks if user is in voice channel
            channel = ctx.message.author.voice.channel
            VOICE_CHANNELS[channel.id] = ctx.channel
            guild_id = ctx.message.guild.id
        except AttributeError:
            return await ctx.send("Dush! Please Join a Channel.") # user is not in a voice channel

        voice_client = ctx.guild.voice_client
        if not voice_client:
            await voice_channel.connect()
            voice_client = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)

        loop = asyncio.get_event_loop()

        try:
            data = await loop.run_in_executor(None, lambda: ytdl.extract_info(info, download=False)) 
            title = data["title"] # get title
            song = data["url"] # get url
            if "entries" in data: # checks for playlist 
                    data = data["entries"][0] # if its a playlist, we get the first item
        except Exception as e:
            data = await loop.run_in_executor(None, lambda: ytdl.extract_info("ytsearch:" + info, download=False))
            song = data["formats"][0]["url"]
            title = data["formats"][0]["title"]
            if 'entries' in data:
                    data = data['entries'][0]
    
        try:
            source = discord.FFmpegPCMAudio(source=song,**ffmpeg_options, executable="ffmpeg") 

            if voice_client.is_playing():
                if guild_id in queue:
                    queue[guild_id].append(source)
                else:
                    queue[guild_id] = [source]
                if len(queue) >= 1:
                        await ctx.send(f"{title} added to queue.")
            else:
                voice_client.play(source, after=lambda x=0: check_queue(ctx, ctx.message.guild.id))
                await ctx.send(f'**Now playing:** {title}')
        except Exception as e:
            print(e)

共1个答案

匿名用户

您可以修改check_queue函数,以便在开始播放队列中的新歌时发送包含歌曲名称的消息。

queue = {}
@commands.command(pass_context=True)
async def play(self, ctx, *, info):

    def check_queue(ctx, id):
        if queue[id]:
            voice_client = ctx.guild.voice_client
            source, title = queue[id].pop(0)  # Get source and title
            voice_client.play(source, after=lambda x=0: check_queue(ctx, ctx.message.guild.id))
            loop.create_task(ctx.send(f"NOW PLAYING: {title}"))  # Send a message with the song title

    try:
        voice_channel = ctx.author.voice.channel  # checks if user is in voice channel
        channel = ctx.message.author.voice.channel
        VOICE_CHANNELS[channel.id] = ctx.channel
        guild_id = ctx.message.guild.id
    except AttributeError:
        return await ctx.send("Dush! Please Join a Channel.")  # user is not in a voice channel

    voice_client = ctx.guild.voice_client
    if not voice_client:
        await voice_channel.connect()
        voice_client = discord.utils.get(self.bot.voice_clients, guild=ctx.guild)

    loop = asyncio.get_event_loop()

    try:
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info(info, download=False))
        title = data["title"]  # get title
        song = data["url"]  # get url
        if "entries" in data:  # checks for playlist
            data = data["entries"][0]  # if its a playlist, we get the first item
    except Exception as e:
        data = await loop.run_in_executor(None, lambda: ytdl.extract_info("ytsearch:" + info, download=False))
        song = data["formats"][0]["url"]

    # Your existing code to play the song...

    if guild_id in queue:
        queue[guild_id].append((source, title))  # Store source and title in the queue
    else:
        queue[guild_id] = [(source, title)]  # Initialize the queue with the source and title

    # Your existing code to check the queue...