【发布时间】:2021-08-05 06:18:05
【问题描述】:
我一直在编写一个不和谐的音乐机器人,我的简单版本没有任何错误!现在我想添加队列功能,我设计了它以便 replit.com 可以处理它。但现在我有错误说:
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
vc = ctx.voice_client
AttributeError: 'str' object has no attribute 'voice_client'
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: 'str' object has no attribute 'voice_client'
一般来说,像“is_paused”或“is_playing”这样的不和谐播放变量都不再起作用(得到相同的AttributeError。)!
这是我的代码:
queue_urls = []
class Music(commands.Cog):
async def check_for_play(self, ctx):
if len(queue_urls) > 0: #if there still are more items in the queue...
await ctx.invoke(self.real_play) #...run the player again
async def real_play(self, ctx): #the player plays the song...
voice = discord.utils.get(bot.voice_clients, guild=ctx.guild)
url = queue_urls[0]
queue_urls.pop(0)
with youtube_dl.YoutubeDL(ytdlopts) as ydl:
info = ydl.extract_info(url, download=False)
URL_NEXT = info['formats'][0]['url']
voice.play(discord.FFmpegPCMAudio(URL_NEXT, **ffmpegopts), after=lambda e: ctx.invoke(self.check_for_play)) #...and when the song is finished, should run the function check_for_play
@bot.command(name='play', aliases=['sing', 'p'])
async def _play(self, ctx): #the command...
vc = ctx.voice_client
if not vc.is_connected:
channel = ctx.author.voice.channel
await channel.connect()
url = ctx.message.content
url = ttourl(url)
queue_urls.append(url) #...adds the item to the queue...
voicec = ctx.voice_client
if not voicec.is_playing: #...and starts the player if its not already playing
await ctx.invoke(self.real_play)
【问题讨论】:
标签: python discord discord.py bots