我是python的新手,正在尝试制作一个不和谐的机器人,当机器人发送消息显示‘歌曲名称’而不是url时,我得到了这个TypeError: can only concatenate str (not "NoneType") to str
,并且不知道如何修复它,当我使用url时,它工作得很好
@client.command(pass_context = True)
async def play(ctx, *, url):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
else:
await ctx.send('`You are not in a voice channel`')
YDL_OPTIONS = {'format': 'bestaudio', 'noplaylist': 'True', 'default_search':"ytsearch"}
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
voice = get(client.voice_clients, guild=ctx.guild)
with YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
if 'entries' in info:
url = info["entries"][0]["formats"][0]['url']
elif 'formats' in info:
url = info["formats"][0]['url']
if not voice.is_playing():
voice.play(FFmpegPCMAudio(url, **FFMPEG_OPTIONS), after=lambda x=0: check_queue(ctx, ctx.message.guild.id))
embed_p = discord.Embed(colour = discord.Colour.magenta())
embed_p.add_field(name='Playing:『 ' + info.get('title')+' 』', value='『'+ctx.author.mention+'』' , inline=False)
await ctx.send(embed=embed_p)
发布于 2021-10-29 04:26:01
当您想要使用它们内部的数据时,您需要了解将来要尝试访问的数据结构。您记录的info变量如下所示:
"_type":"playlist",
"entries":[
{
"id":"dQw4w9WgXcQ",
"title":"Rick Astley - Never Gonna Give You Up (Official Music Video)",
"formats":[
{
"asr":48000,
"filesize":1232413,
"format_id":"249",
.....
并持续很长一段时间。
现在,如果您尝试从info
访问title,则会得到None,因为title在info
中不存在。但是,如果您更深入地研究该对象,您可以看到有一个
"title":"Rick Astley - Never Gonna Give You Up (Official Music Video)"
要访问它,首先要访问info
,然后访问entries
,这是一个可以在我上面提供的格式片段中看到的数组。然后,您可以访问包含标题的条目中的第一个对象。
用info['entries'][0]['title']
替换info.get('title')
https://stackoverflow.com/questions/69763616
复制相似问题