Python Discord music bot queue命令,但vc.is#u以某种方式正在播放';你不在这里工作吗?
我现在正在编写一个discord机器人,在过去的两天里,我的discord.py编码技能有了很大的提高,这使我开始编写一个音乐机器人。我有一个预先编写的代码,并试图成功地理解它,现在已经编写了自己的代码。现在我几乎完成了(我想),显然有一个问题。我的代码包括vc.is\u多次播放,但不知何故它在play\u命令中不起作用。我的代码:Python Discord music bot queue命令,但vc.is#u以某种方式正在播放';你不在这里工作吗?,python,discord,discord.py,bots,Python,Discord,Discord.py,Bots,我现在正在编写一个discord机器人,在过去的两天里,我的discord.py编码技能有了很大的提高,这使我开始编写一个音乐机器人。我有一个预先编写的代码,并试图成功地理解它,现在已经编写了自己的代码。现在我几乎完成了(我想),显然有一个问题。我的代码包括vc.is\u多次播放,但不知何故它在play\u命令中不起作用。我的代码: @bot.command(name='play', aliases=['p']) async def _play(ctx, url : str): #just th
@bot.command(name='play', aliases=['p'])
async def _play(ctx, url : str): #just the command
queueurls = [] #the list where the incoming url's are stored
vc = ctx.voice_client
if not vc: #if the bot is not in the channel...
channel = ctx.author.voice.channel
await channel.connect() #...then connect the bot the channel the author of the command is in
url = ctx.message.content #the URL is just what the author of the command has written
url = ttourl(url) #ttourl(url) is just a def to turn for example "!p Hello" into "Hello" and then into an YouTube URL (https://www.youtube.com/watch?v=video id)
queueurls.append(url) #then apply that URL to the list above
if not vc.is_playing(): #(HERE'S THE PROBLEM) when the bot isn't already playing anything...
while queueurls != []: #go into the loop until the queueurls list is empty
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
url = queueurls[0] #the url is the first item in the queueurls list
queueurls.pop(0) #now erase that out of the list
with youtube_dl.YoutubeDL(ytdlopts) as ydl: #and now download the video
ydl.download([url])
audio = "songstemp/song.mp3"
voice.play(discord.FFmpegPCMAudio("songstemp/song.mp3")) #play that song
time.sleep(audiosec(audio)) #wait until the song is finished...
if os.path.exists("songstemp/song.mp3"): #...and then if the song is there...
path = "songstemp/song.mp3"
os.remove(path) #...delete it
else: #if the bot is already playing something, then just do nothing, but the url is added to the list so the while loop should play the song when the other song is finished
pass
@bot.command(name="stop")
async def stop_(ctx): #just the stop command
vc = ctx.voice_client
if vc.is_playing(): #here, is_playing works just fine.
vc.stop()
path = "songstemp/song.mp3"
os.remove(path)
正如你所看到的,我在代码的底部有“stop”命令,“vc.is_playing”在里面工作。但当我在“_play”中使用它时,它不起作用!此处显示错误消息:
Ignoring exception in command play:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 69, in _play
if not vc.is_playing():
AttributeError: 'NoneType' object has no attribute 'is_playing'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'is_playing'
这是第一个问题。第二个问题是,当while循环应该运行时,bot什么也不做。。。
[!]我正在repl.it/replit.com上运行这个机器人我还没有真正弄糟
discord.py
的音乐部分,但是修复应该很容易
在第一条if语句中,您要检查vc
是否为NoneType
,还应该在其中定义vc
(因为您没有退出函数)
如果不是vc:
频道=ctx.author.voice.channel
vc=等待通道。连接()
显然,我不得不做第二个vc=…,但我必须以不同的方式命名它
现在是:
@bot.command(name='play', aliases=['sing', 'p'])
async def _play(ctx, url : str):
queueurls = []
vc = ctx.voice_client #first definition of ctx.voice_client
if not vc: #use first definition
channel = ctx.author.voice.channel
await channel.connect()
url = ctx.message.content
url = ttourl(url)
queueurls.append(url)
voicec = ctx.voice_client #second definition of ctx.voice_client
if not voicec.is_playing(): #use second definition
while queueurls != []:
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
url = queueurls[0]
queueurls.pop(0)
with youtube_dl.YoutubeDL(ytdlopts) as ydl:
ydl.download([url])
voice.play(discord.FFmpegPCMAudio("songstemp/song.mp3"))
if os.path.exists("songstemp/song.mp3"):
path = "songstemp/song.mp3"
os.remove(path)
else:
pass
错误应该是不言自明的
NoneType
=某些内容不存在,或者您所需的函数返回None
。您可以执行vc=ctx.voice\u client
操作,然后检查vc
是否为None并处理该情况,但您永远不会将新内容分配给vc
,因此它仍然为None。