python - 我正在用 discord.py 制作一个音乐机器人,但我在播放命令时遇到了一些问题
问题描述
机器人有播放命令,如果它已经不在语音频道中,它会加入语音频道,它会播放歌曲(显然),如果机器人已经在播放歌曲,它会将歌曲存储在名为 queues 的字典中然后在歌曲结束时播放。要执行此类操作,您需要after
voiceClientsplay
命令上的声明,它的行为似乎很奇怪。我已经放了一些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 的经典错误之一。看看这一行:
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))
推荐阅读
- amazon-s3 - Apache Flink - s3 文件夹监控 - 许多文件丢失
- spyder - spyder(python)不打开(实际上没有给出任何错误)
- deep-learning - 连接来自不同来源的编码器隐藏状态/单元/输出以进行注意力计算 - 问题?
- java - 为什么这个 Java 代理服务器很慢?
- sql - postgresql 查询找不到带有外来字母的结果
- java - 如何在 Android 中处理 MVVM 中的回调和状态
- node.js - 如何在 get() 函数中实现多个集合?
- python - For循环替代函数中的多个列(熊猫)
- c++ - 如何将 QBytearray BCD 转换为十进制 QString 表示?
- html - Angular 5材质数据网格自动高度