所以我试着用一个不和谐的机器人来做一个简单的API调用,来获取游戏的价格。我有API请求工作,但当我试图触发机器人检查价格为我什么都没有发生。我没有在控制台上看到错误,机器人也不会发布任何东西来聊天。在
我知道它很难看,我会回去把它清理干净的,等我可以把它贴出来。在
if message.content.startswith('!pricecheck'):
game = message.content[11:]
gres = requests.get('https://api.steampowered.com/ISteamApps/GetAppList/v2/')
gdata = gres.json()
for i in gdata["applist"]["apps"]:
if (i["name"] == game):
app = (i["appid"])
priceres = requests.get(f"https://store.steampowered.com/api/appdetails/?appids={app}")
priced = priceres.json()
price = (priced[f"{app}"]["data"]["price_overview"].get("final"))
msg = f"{game} is currently{price}".format(message)
await client.send_message(message.channel, msg)
假设命令由
!pricecheck <game>
调用,game = message.content[11:]
包含空格。请参阅下面的测试用例,其中空格被替换为_u,因此它很容易阅读。在因此,
if (i["name"] == game)
永远不会为真,因此await client.send_message
将永远不会执行。在将其更改为
message.content[12:]
将删除空间。在建议
添加一个检查是否找到了一个游戏将允许您查看所有的
if
的计算结果是False
。当命令不起作用时,它也会给用户反馈,可能是因为使用错误。在您还可以将} 库,这是异步版本。
requests
库更改为^{requests
可能是危险的,因为它是阻塞的,这意味着如果花费很长时间,它可能会导致代码崩溃。在下面是如何在代码中使用这些建议的示例。在
^{pr2}$相关问题 更多 >
编程相关推荐