Nef Discord机器人
可以尝试&看到这怎么了?就像每当我尝试运行代码时,我都会得到此味精,我尝试制作一个Discord Bot,以检查Hyphixel SkyBlock AH API&用拍卖的价格/名称/AH ID返回
/home/runner/nef-bot/venv/lib/python3.8/site-packages/grequests.py:22: Monkeypatchwarning:SSL之后的猴子斑SSL 进口可能导致错误,包括python 3.6上的recursionError。 这也可能导致Python 3.7的行为不正确。请 猴子较早。看 https://github.com/gevent/gevent/gevent/gevent/gevent/issues/1016 。直接的模块 导入(未修补):['aiohttp.connector (/home/runner/nef-bot/venv/lib/python3.8/site-packages/aiohttp/connector.py)', 'aiohttp.client (/home/runner/nef-bot/venv/lib/python3.8/site-packages/aiohttp/client.py)', 'aiohttp.client_exceptions (/home/runner/nef-bot/venv/lib/python3.8/site-packages/aiohttp/client_exceptions.py)', 'aiohttp.client_reqrep (/home/runner/nef-bot/venv/lib/python3.8/site-packages/aiohttp/client_reqrep.py)']。 curious_george.patch_all(thread = false,select = false)我们已登录 作为notenoughflips#8101
这是代码:
import discord
import grequests
import json
from pprint import pprint
import time
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$verify'):
await message.channel.send('I am online!')
client.run('xxxx-xxxx-xxxx-xxxx-xxxx')
auc_requirements = {"Baby_Yeti_pet": {"item_name": "] Baby Yeti", "tier": "EPIC", "category": "misc", "price": 999999999},
"Bat_pet": {"item_name": "] Bat", "tier": "MYTHIC", "category": "misc", "price": 999999999},
"Black_Cat_Pet": {"item_name": "] Black Cat", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Blaze_Pet": {"item_name": "] Blaze", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Blue_Whale_Pet": {"item_name": "] Blue Whale", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Dolphin_Pet": {"item_name": "] Dolphin", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Elephant_Pet": {"item_name": "] Elephant", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Ender_Dragon_Pet": {"item_name": "] Ender Dragon", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Enderman_Pet": {"item_name": "] Enderman", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Tiger_Pet": {"item_name": "] Tiger", "tier": "LEGENDARY", "category": "misc", "price": 999999999}
}
API_KEY = "xxxx-xxxx-xxxx-xxxx-xxxx"
data = {}
auction_final = []
auction_final_cheapest = {}
auction_final_cheapest_sorted = []
start_time = time.time()
url_base = f"https://api.hypixel.net/skyblock/auctions?key={API_KEY}"
def checkAuctionItem(auction_item):
# Check if item is BIN
if("bin" not in auction_item):
return (False, "Not BIN")
# Check if item is already claimed
if(auction_item["claimed"] == True):
return (False, "Already claimed")
# For unique obj ruleset
for id in auc_requirements:
# Start as valid item
valid = True
# For every rule in obj ruleset
for req in auc_requirements[id]:
# Make sure rule isn't price
if(req != "price"):
# Make sure it follows the rule
if(auc_requirements[id][req] not in auction_item[req]):
# No longer valid
valid = False
break
# Found a potential match with a filter!
if(valid):
# Found a match with a filter AND price! (Success)
if(auction_item["starting_bid"] < auc_requirements[id]["price"]):
dashed_uuid = auction_item['uuid'][:8] + '-' + auction_item['uuid'][8:12] + '-' + auction_item['uuid'][12:16]
dashed_uuid += '-' + auction_item['uuid'][16:20] + '-' + auction_item['uuid'][20:]
return (True, (id, f"/viewauction {dashed_uuid}", auction_item['starting_bid'], auction_item["item_name"]))
# Broke on one of the requirements
return (False, "Not all requirements met")
# Async to first page
resp = grequests.get(url_base)
# Get page 0 (and pages count)
for res in grequests.map([resp]):
data = json.loads(res.content)
total_pages = data['totalPages']
print(f"Total Pages found: {data['totalPages']}")
# Verify success
if(data["success"]):
# Get items from page 0
for auction_item in data["auctions"]:
try:
item_ans = checkAuctionItem(auction_item)
# Passed filter
if(item_ans[0]):
auction_final.append(item_ans[1])
# Failed filter
else:
pass
except:
pprint(data)
# Unsuccessful GET request
else:
print(f"Failed GET request: {data['cause']}")
first_page_time = time.time()
# Get all page urls
urls = []
for page_count in range(1, total_pages+1):
urls.append(f"{url_base}&page={page_count}")
# Async to remaining pages
resp = (grequests.get(url) for url in urls)
made_requests_time = time.time()
# Get items from remaining pages
for res in grequests.map(resp):
data = json.loads(res.content)
# Verify success
if(data["success"]):
# Get items from pages 1 -> n
for auction_item in data["auctions"]:
try:
item_ans = checkAuctionItem(auction_item)
# Passed filter
if(item_ans[0]):
auction_final.append(item_ans[1])
# Failed filter
else:
pass
except:
pprint(data)
# Unsuccessful GET request
else:
print(f"Failed GET request: {data['cause']}")
# Debug for amount of items found
print(f"{len(auction_final)} items found")
# Sort out the results
auction_final = sorted(auction_final, key=lambda x: (x[0], x[2]))
# Get only the cheapest of every time
for auc in auction_final:
# If not in new dict yet
if(auc[0] not in auction_final_cheapest):
auction_final_cheapest[auc[0]] = auc
# If already in new dict, compare against previous
else:
if(auc[2] < auction_final_cheapest[auc[0]][2]):
auction_final_cheapest[auc[0]] = auc
# Add all items from cheapest to cheapest_sorted
for auc in auction_final_cheapest:
auction_final_cheapest_sorted.append(auction_final_cheapest[auc])
# Sort cheapest_sorted
auction_final_cheapest_sorted = sorted(auction_final_cheapest_sorted, key=lambda x: x[2])
pprint(auction_final_cheapest_sorted)
end_time = time.time()
print(f"Time Taken: {end_time-start_time}\n\tFirst Page: {first_page_time-start_time}\n\tFinished Requests: {made_requests_time-start_time}")
Could some1 try & see whats wrong with this? as whenever i try to run the code i get this msg, im trying to make a discord bot that will check hypixel skyblock AH api & return with the prices/name/ah id of the auction
/home/runner/NEF-Bot/venv/lib/python3.8/site-packages/grequests.py:22:
MonkeyPatchWarning: Monkey-patching ssl after ssl has already been
imported may lead to errors, including RecursionError on Python 3.6.
It may also silently lead to incorrect behaviour on Python 3.7. Please
monkey-patch earlier. See
https://github.com/gevent/gevent/issues/1016. Modules that had direct
imports (NOT patched): ['aiohttp.connector
(/home/runner/NEF-Bot/venv/lib/python3.8/site-packages/aiohttp/connector.py)',
'aiohttp.client
(/home/runner/NEF-Bot/venv/lib/python3.8/site-packages/aiohttp/client.py)',
'aiohttp.client_exceptions
(/home/runner/NEF-Bot/venv/lib/python3.8/site-packages/aiohttp/client_exceptions.py)',
'aiohttp.client_reqrep
(/home/runner/NEF-Bot/venv/lib/python3.8/site-packages/aiohttp/client_reqrep.py)'].
curious_george.patch_all(thread=False, select=False) We have logged in
as NotEnoughFlips#8101
Here's the code:
import discord
import grequests
import json
from pprint import pprint
import time
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$verify'):
await message.channel.send('I am online!')
client.run('xxxx-xxxx-xxxx-xxxx-xxxx')
auc_requirements = {"Baby_Yeti_pet": {"item_name": "] Baby Yeti", "tier": "EPIC", "category": "misc", "price": 999999999},
"Bat_pet": {"item_name": "] Bat", "tier": "MYTHIC", "category": "misc", "price": 999999999},
"Black_Cat_Pet": {"item_name": "] Black Cat", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Blaze_Pet": {"item_name": "] Blaze", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Blue_Whale_Pet": {"item_name": "] Blue Whale", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Dolphin_Pet": {"item_name": "] Dolphin", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Elephant_Pet": {"item_name": "] Elephant", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Ender_Dragon_Pet": {"item_name": "] Ender Dragon", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Enderman_Pet": {"item_name": "] Enderman", "tier": "LEGENDARY", "category": "misc", "price": 999999999},
"Tiger_Pet": {"item_name": "] Tiger", "tier": "LEGENDARY", "category": "misc", "price": 999999999}
}
API_KEY = "xxxx-xxxx-xxxx-xxxx-xxxx"
data = {}
auction_final = []
auction_final_cheapest = {}
auction_final_cheapest_sorted = []
start_time = time.time()
url_base = f"https://api.hypixel.net/skyblock/auctions?key={API_KEY}"
def checkAuctionItem(auction_item):
# Check if item is BIN
if("bin" not in auction_item):
return (False, "Not BIN")
# Check if item is already claimed
if(auction_item["claimed"] == True):
return (False, "Already claimed")
# For unique obj ruleset
for id in auc_requirements:
# Start as valid item
valid = True
# For every rule in obj ruleset
for req in auc_requirements[id]:
# Make sure rule isn't price
if(req != "price"):
# Make sure it follows the rule
if(auc_requirements[id][req] not in auction_item[req]):
# No longer valid
valid = False
break
# Found a potential match with a filter!
if(valid):
# Found a match with a filter AND price! (Success)
if(auction_item["starting_bid"] < auc_requirements[id]["price"]):
dashed_uuid = auction_item['uuid'][:8] + '-' + auction_item['uuid'][8:12] + '-' + auction_item['uuid'][12:16]
dashed_uuid += '-' + auction_item['uuid'][16:20] + '-' + auction_item['uuid'][20:]
return (True, (id, f"/viewauction {dashed_uuid}", auction_item['starting_bid'], auction_item["item_name"]))
# Broke on one of the requirements
return (False, "Not all requirements met")
# Async to first page
resp = grequests.get(url_base)
# Get page 0 (and pages count)
for res in grequests.map([resp]):
data = json.loads(res.content)
total_pages = data['totalPages']
print(f"Total Pages found: {data['totalPages']}")
# Verify success
if(data["success"]):
# Get items from page 0
for auction_item in data["auctions"]:
try:
item_ans = checkAuctionItem(auction_item)
# Passed filter
if(item_ans[0]):
auction_final.append(item_ans[1])
# Failed filter
else:
pass
except:
pprint(data)
# Unsuccessful GET request
else:
print(f"Failed GET request: {data['cause']}")
first_page_time = time.time()
# Get all page urls
urls = []
for page_count in range(1, total_pages+1):
urls.append(f"{url_base}&page={page_count}")
# Async to remaining pages
resp = (grequests.get(url) for url in urls)
made_requests_time = time.time()
# Get items from remaining pages
for res in grequests.map(resp):
data = json.loads(res.content)
# Verify success
if(data["success"]):
# Get items from pages 1 -> n
for auction_item in data["auctions"]:
try:
item_ans = checkAuctionItem(auction_item)
# Passed filter
if(item_ans[0]):
auction_final.append(item_ans[1])
# Failed filter
else:
pass
except:
pprint(data)
# Unsuccessful GET request
else:
print(f"Failed GET request: {data['cause']}")
# Debug for amount of items found
print(f"{len(auction_final)} items found")
# Sort out the results
auction_final = sorted(auction_final, key=lambda x: (x[0], x[2]))
# Get only the cheapest of every time
for auc in auction_final:
# If not in new dict yet
if(auc[0] not in auction_final_cheapest):
auction_final_cheapest[auc[0]] = auc
# If already in new dict, compare against previous
else:
if(auc[2] < auction_final_cheapest[auc[0]][2]):
auction_final_cheapest[auc[0]] = auc
# Add all items from cheapest to cheapest_sorted
for auc in auction_final_cheapest:
auction_final_cheapest_sorted.append(auction_final_cheapest[auc])
# Sort cheapest_sorted
auction_final_cheapest_sorted = sorted(auction_final_cheapest_sorted, key=lambda x: x[2])
pprint(auction_final_cheapest_sorted)
end_time = time.time()
print(f"Time Taken: {end_time-start_time}\n\tFirst Page: {first_page_time-start_time}\n\tFinished Requests: {made_requests_time-start_time}")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论