对于我的不和谐音乐机器人,我结合了队列和播放命令。所以每当我想排队的东西,我可以只使用播放命令。我的问题是,我的机器人只消息我播放的第一首歌。之后,队列继续播放没有消息。
例如,第一次显示 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条答案
按热度按时间egdjgwm81#
您可以修改
check_queue
函数,使其在开始播放队列中的新歌时发送包含歌曲名称的消息。