我正在嘗試使用TGRCode API 創建一個 Discord 命令,該命令回傳有關 Super Mario Maker 2 中關卡的資訊。但是,我遇到了問題。
如果該級別沒有,例如,清除條件,那么它會拋出一個鍵錯誤并停止命令。我知道它是做什么的,因為它只是不存在于 API 回傳的 JSON 中,而不是空字串或任何東西。
但是,問題是我不確定如何處理它。
(我正在嘗試使用AIOHTTP接收資訊)
我試過使用
if clear_condition == None:
clear_condition = "None"
那只是回傳一個 KeyError 我也嘗試過
if clear_condition_name not in response:
clear_condition_name = "None"
那只是回傳一個錯誤,說ClientResponse不可迭代。
我試過(帶和不帶引號)
if "clear_condition_name" not in response.json():
clear_condition_name = "None"
這只是吐出另一個關鍵錯誤。
我還查看了 Career Karma 的這篇文章,但這并沒有幫助,因為我不是定義字典的人。
這是我當前的代碼:
@commands.command()
async def smm2_lookup_level(self, ctx, code):
await ctx.reply(f"Getting level information of level with code `{code}`")
print(f"{ctx.author.name} has requested to get all information for {code}.")
MAIN = f"https://tgrcode.com/mm2/level_info/{code}"
async with request("GET", MAIN, headers={}) as response:
if response.status == 200:
print("The TGRCode API returned OK; getting info...")
data = await response.json()
title = data["name"]
description = data["description"]
upload_date = data["uploaded_pretty"]
course_id = data["course_id"]
style = data["game_style_name"]
theme = data["theme_name"]
difficulty = data["difficulty_name"]
tags = data["tags_name"]
upload_time = data["upload_time_pretty"]
number_of_comments = data["num_comments"]
clear_condition_name = data["clear_condition_name"]
clear_condition_magnitude = data["clear_condition_magnitude"]
clears = data["clears"]
attempts = data["attempts"]
clear_rate = data["clear_rate"]
plays = data["plays"]
likes = data["likes"]
boos = data["boos"]
thumbnail = f"https://tgrcode.com/mm2/level_thumbnail/{code}"
map = f"https://tgrcode.com/mm2/level_entire_thumbnail/{code}"
if clear_condition_name not in response.json():
clear_condition_name = "None"
clear_condition_magnitude = ""
if "clear_condition_magnitude" not in response.json():
clear_condition_name = "None"
clear_condition_magnitude = ""
if "description" not in response.json():
description = ""
if "tags" not in response.json():
tags = "None"
embed = discord.Embed(title="SMM2 Level Information.")
embed.add_field(name="Level Name", value=title, inline=True)
embed.add_field(name="Level Description", value=description, inline=True)
embed.add_field(name="Level Code", value=course_id, inline=True)
embed.add_field(name="Level Style", value=style, inline=True)
embed.add_field(name="Level Theme", value=theme, inline=True)
embed.add_field(name="Level Difficulty", value=difficulty, inline=True)
embed.add_field(name="Tags", value=tags, inline=True)
embed.add_field(name="Upload Date", value=upload_date, inline=True)
embed.add_field(name="Time taken to Clearcheck Course", value=upload_time, inline=True)
embed.add_field(name="Clear Condition Information", value=f"{clear_condition_name} {clear_condition_magnitude}.", inline=True)
embed.add_field(name="Clear Information", value=f"Clears: {clears}\nAttemps: {attempts}\nPlays: {plays}\nClear Rate: {clear_rate}.")
embed.add_field(name="Number of Comments", value=number_of_comments, inline=True)
embed.set_thumbnail(url=thumbnail)
embed.set_image(url=map)
embed.add_field(name="Like:Boo Ratio", value=f"{likes}:{boos}")
await ctx.send(f"{ctx.author.mention} Here's the information for level with code `{code}`:", embed=embed)
else:
await ctx.send(f"I was unable to retrieve the information. Try again, if not the TGRCode API may be down, or there is an error on my end.\n*Response Status: {response.status}*")
print(f"I was unable to retrieve the information from TGRCode.\n*Response Status: {response.status}*")
因此,盡管我嘗試過的所有方法都沒有幫助,但我希望我能在這里找到一些。謝謝。
uj5u.com熱心網友回復:
問題是您在宣告clear_condition_name變數時試圖讀取該鍵。您應該檢查回應資料字典是否有這樣的鍵,如下所示:
clear_condition_name = data["clear_condition_name"] if "clear_condition_name" in data.keys() else "None"
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/470236.html
上一篇:如何獲取陣列中存在的物件中的所有物件并將結果添加到另一個陣列中
下一篇:你能幫我用烏龜圖形畫房子嗎?
