我怎样才能修复我收到的头像命令中的这个错误?
How can I fix this error in my avatar command that I'm getting?
我需要修复这个错误,我对 API 不是很好。我很想知道如何修复它,因为我正在努力学习 Python。
代码:
@client.command()
async def avatarimg1(ctx, username):
user = await roblox.get_user_by_username(username)
embed = Embed(title=f"Avatar of {user.name}")
response = requests.get(f'https://thumbnails.roblox.com/v1/users/avatar?userIds={user.id}&size=420x420&format=Png&isCircular=false')
json_data = json.loads(response.text)
imagesj = json_data["imageUrl"]
embed.set_thumbnail(imagesj)
await ctx.send(embed=embed)
错误:
> Ignoring exception in command avatarimg1: Traceback (most recent call
> last): File
> "/home/runner/dasdasdasd/venv/lib/python3.8/site-packages/discord/ext/commands/core.py",
> line 85, in wrapped
> ret = await coro(*args, **kwargs) File "main.py", line 40, in avatarimg1
> imagesj = json_data["imageUrl"] KeyError: 'imageUrl'
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last): File
> "/home/runner/dasdasdasd/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py",
> line 939, in invoke
> await ctx.command.invoke(ctx) File "/home/runner/dasdasdasd/venv/lib/python3.8/site-packages/discord/ext/commands/core.py",
> line 863, in invoke
> await injected(*ctx.args, **ctx.kwargs) File "/home/runner/dasdasdasd/venv/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: KeyError: 'imageUrl'
接下来我可以尝试什么?
响应的第一层只有一个键 data
,它是一个列表,因此您需要获取第一个元素并访问其中的 imageUrl
键。
imagesj = json_data['data'][0]['imageUrl']
我需要修复这个错误,我对 API 不是很好。我很想知道如何修复它,因为我正在努力学习 Python。
代码:
@client.command()
async def avatarimg1(ctx, username):
user = await roblox.get_user_by_username(username)
embed = Embed(title=f"Avatar of {user.name}")
response = requests.get(f'https://thumbnails.roblox.com/v1/users/avatar?userIds={user.id}&size=420x420&format=Png&isCircular=false')
json_data = json.loads(response.text)
imagesj = json_data["imageUrl"]
embed.set_thumbnail(imagesj)
await ctx.send(embed=embed)
错误:
> Ignoring exception in command avatarimg1: Traceback (most recent call
> last): File
> "/home/runner/dasdasdasd/venv/lib/python3.8/site-packages/discord/ext/commands/core.py",
> line 85, in wrapped
> ret = await coro(*args, **kwargs) File "main.py", line 40, in avatarimg1
> imagesj = json_data["imageUrl"] KeyError: 'imageUrl'
>
> The above exception was the direct cause of the following exception:
>
> Traceback (most recent call last): File
> "/home/runner/dasdasdasd/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py",
> line 939, in invoke
> await ctx.command.invoke(ctx) File "/home/runner/dasdasdasd/venv/lib/python3.8/site-packages/discord/ext/commands/core.py",
> line 863, in invoke
> await injected(*ctx.args, **ctx.kwargs) File "/home/runner/dasdasdasd/venv/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: KeyError: 'imageUrl'
接下来我可以尝试什么?
响应的第一层只有一个键 data
,它是一个列表,因此您需要获取第一个元素并访问其中的 imageUrl
键。
imagesj = json_data['data'][0]['imageUrl']