【发布时间】: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