我想让当你输入 !tags 时,discord 机器人会通过 API 在随机流行的服装中搜索标签,但是当我输入
!tags
时,它不会发送任何错误,也不会发送任何不和谐的消息。有任何修复吗?我可以改进代码吗?
完整代码:
code REMOVED
如果可以的话,你能帮忙解释一下吗?如果你不能解释,请尝试让我的代码变得更好。
我尝试过,但它只是没有给我错误或机器人的任何消息。
问题出在
get_shirt_description
函数中的第二个 api 请求。像f"https://catalog.roblox.com/v1/catalog/items/details?id={item_id}"
这样的格式是无效的。所以,它会返回你的一些 json
{
"errors":[{
"code":0,
"message":"MethodNotAllowed"
}]
}
那么在这种情况下,
any(keyword in description for keyword in keywords)
中的contains_tags
将始终将False
返回到if语句。因此,没有任何错误,但也没有执行 if 语句下的任何行。json
发送到 https://catalog.roblox.com/v1/catalog/items/details
。类似的东西
item_json = {
"items": [{
"itemType": 1,
"id": 12222721460
}]
}
response = requests.post(
url="https://catalog.roblox.com/v1/catalog/items/details",
json=item_json,
headers={
"Content-Type": "application/json",
"X-CSRF-TOKEN": Your_Token
}
)
# print(response.json())
# do sth further
查看更多文档
另外,我强烈建议您将
requests
切换为 aiohttp
,因为 requests
模块被阻塞。