如何让discord bot说出接下来播放的歌曲

时间:2021-06-23 03:04:19

标签: python discord.py

我正在尝试让我的不和谐机器人能够在播放队列中的新歌曲时说出一条消息。

例如

enter image description here

所以基本上当一首歌结束并播放下一首时,它会说类似上面的内容。我在想我可以在我的播放命令中写这个。我的播放命令的当前代码是

@commands.command()
    async def play(self, ctx, *, url):
        invc = ctx.author.voice
        botinvc = ctx.guild.me.voice
        if not invc:
            await ctx.send(f'{ctx.author.mention}, You are not in a VC!')
            return
        if invc:
            if not botinvc:
                await ctx.author.voice.channel.connect()
                player = music.get_player(guild_id=ctx.guild.id)
                if not player:
                    player =  music.create_player(ctx, ffmpeg_error_betterfix=True)
                if not ctx.voice_client.is_playing():
                    await player.queue(url, search=True)
                    song = await player.play()
                    await ctx.send(f'Now Playing: `{song.name}`')
            
            if botinvc:
                player = music.get_player(guild_id=ctx.guild.id)
                if not player:
                    player =  music.create_player(ctx, ffmpeg_error_betterfix=True)
                if not ctx.voice_client.is_playing():
                    await player.queue(url, search=True)
                    song = await player.play()
                    await ctx.send(f'Now Playing: `{song.name}`')
                else:
                    song = await player.queue(url, search=True)
                    embed=discord.Embed(title='Song Added to Queue!', description=f'**{song.name}** added!', color=0x00FFFF)
                    author = ctx.message.author
                    pfp = author.avatar_url
                    embed.set_author(name=f"{ctx.author.name}", icon_url=pfp)
                    embed.timestamp = datetime.datetime.utcnow()
                    embed.set_footer(text=f'Added by {ctx.author}')
                    await ctx.send(embed=embed)

我的进口是,

import discord
import datetime
import DiscordUtils
import asyncio
from discord.ext import commands

我怎样才能让它发挥作用?我为此使用了 DiscordUtils 库。

1 个答案:

答案 0 :(得分:3)

您好,您是否尝试过使用 after

song = await player.play(after=send_message_function)

您可以创建原始队列列表和已播放的流行歌曲,并获取返回列表中第一首歌曲的函数。 类似的东西

async def send_message_function():
    queue.pop(0)
    await ctx.send(embed=discord.Embed(title=f"Now Playing: {queue[0]}"))