我正在尝试使用 Pyautogui 为名为“Graal Online Classic”的 MMO 创建一个机器人。它会读取向其发送消息的玩家的“PM”(个人消息),然后回复适当的响应。它还会截取玩家姓名的屏幕截图,将其存储在字典中(还存储该特定玩家在机器人响应层次结构中的位置),并使用它来确定玩家之前是否曾向他们发送过消息。
import time
import pyautogui
#option = None
def main():
#find_notification()
name_coords, reply_coords, text_coords = set_message_coords()
option = read_message(text_coords)
player_id(name_coords, option)
answer = response()
reply(reply_coords, answer)
def find_notification(): #looks for a waiting PM and clicks it
while True:
image = pyautogui.locateCenterOnScreen('test.png', grayscale = False, confidence = .9)
print(image)
if image is not None:
print('Found a waiting message')
pyautogui.click(image)
break
def set_message_coords(): # Creates coords for message box and screenshots name
try:
imagex, imagey = pyautogui.locateCenterOnScreen('upper_right_message_corner.png', grayscale = True, confidence = .8)
except:
print('ERROR I SHOULD BE FINDING "upper_right_message_corner.PNG" EXITING PROGRAM')
exit()
name_coords = (imagex - 424), imagey, 378, 50 # The coords of where the players name would be
print('Found an open message')
print(imagex, imagey)
reply_coords = (imagex - 251), (imagey + 255 ) # Coords of the reply button
text_coords = (imagex - 461), (imagey + 45), 430, 45 # Coords of where a players possible response is
return name_coords, reply_coords, text_coords # Returns all coord values to be used by other functions
def player_id(name_coords, option): # Indentifies person who messaged and depending on if this person has messaged before changes response
players = {} # Used to store players names and where in the response is
name_image = pyautogui.screenshot('name_test.png', region = name_coords) # Names are screenshots
if name_image not in players:
print("User was not previously found, adding to dictionary.")
players[name_image] = None
else:
print("User is previous user.")
players[name_image] = players[name_image] + option
return players[name_image]
def reply(reply_coords, response): #Replies to PM
pyautogui.click(reply_coords)
pyautogui.typewrite(response)
pyautogui.press('enter')
def read_message(text_coords): # Reads PM for numbers and sets option the number
if pyautogui.locateCenterOnScreen('1.png',region = text_coords, confidence = .9, grayscale = True):
option = '1'
elif pyautogui.locateCenterOnScreen('2.png',region = text_coords, confidence = .9, grayscale = True):
option = '2'
elif pyautogui.locateCenterOnScreen('3.png',region = text_coords, confidence = .9, grayscale = True):
option = '3'
elif pyautogui.locateCenterOnScreen('4.png',region = text_coords, confidence = .9, grayscale = True):
option = '4'
elif pyautogui.locateCenterOnScreen('5.png',region = text_coords, confidence = .9, grayscale = True):
option = '5'
elif pyautogui.locateCenterOnScreen('6.png',region = text_coords, confidence = .9, grayscale = True):
option = '6'
elif pyautogui.locateCenterOnScreen('7.png',region = text_coords, confidence = .9, grayscale = True):
option = '7'
elif pyautogui.locateCenterOnScreen('8.png',region = text_coords, confidence = .9, grayscale = True):
option = '8'
elif pyautogui.locateCenterOnScreen('9.png',region = text_coords, confidence = .9, grayscale = True):
option = '9'
elif pyautogui.locateCenterOnScreen('0.png',region = text_coords, confidence = .9, grayscale = True):
option = '0'
else:
print('ERROR CANT FIND DIGIT ANSWER')
option = None
#reply(reply_coords,'ERROR PLEASE ENTER A NUMBER RESPONSE!')
#main()
print(option)
return option
def response(option): # All the possible responses the bot can give a player
if option == None:
return "Hello! I am bot made to answer your questions! PM the number for more options! 1: Bounties. 2: Bug Hunting. 3: Looting. 4: Farming."
elif option == '1':
return "The bounty quest allows you to hunt mobs for cash! Location: Castle, steward's room(to the right in the throne room) [PM 1 for specifics, PM 2 for TIPS, PM 3 for possible bounties]"
elif option == '12':
return '1. The bounty box will "drop"(stop following you, and will not pick up anything) after every kill/capture you get, and will require you to call it, or run over it to pick it up again. [PM 1 for more info, PM 9 to go back one level, PM 0 to reset choices]'
elif option == '13':
return '100 green blobs, 20 Lizardons, 75 Pyrats(as homage to the PQ I assume), 75 Rebel soldiers(regular green baddy), 60 dark blobs, 60 rats, 75 snakes, 75 bats, 75 bandits, 80 spiders, 50 archers, or 50 crabs. [PM 9 to go back one level, PM 0 to reset choices]'
main()
它会产生以下错误。
Found an open message
692 371
ERROR CANT FIND DIGIT ANSWER
None
Traceback (most recent call last):
File "C:\Users\Pablo\OneDrive\python_projects\chat_bot\main.py", line 104, in <module>
main()
File "C:\Users\Pablo\OneDrive\python_projects\chat_bot\main.py", line 10, in main
player_id(name_coords, option)
File "C:\Users\Pablo\OneDrive\python_projects\chat_bot\main.py", line 47, in player_id
if name_image not in players:
TypeError: unhashable type: 'Image'
[Finished in 1.9s]
Error happens in function player_id()
我对列表和字典没有太多经验,所以我不确定如何处理这个问题。我查看了类似的线程,并出现错误“TypeError:不可散列类型:”,但我并不真正理解这些响应。任何帮助将不胜感激,谢谢。
为了将项目添加到字典中,它必须是可散列的。在大多数情况下,这意味着它必须是不可变的。您不能直接对
PIL.Image.Image()
对象进行哈希处理。相反,您可以创建并存储一个 bytes
对象,然后检索该字节对象,如下所示:
# Store the image
name_image = pyautogui.screenshot('name_test.png', region = name_coords)
name_image_asbytes = name_image.tobytes()
players[name_image_asbytes] = None
# Retrieve data associated with the image
# In this case, I am using my_img as the image I want to get associated data from
data = players[my_img.tobytes()]
# Create image from bytes (in case you ever need to later on)
# You would also need to install the PIL library for this, but I suspect it is already installed because pyautogui needs PIL
# In this case, I am getting the image associated with the first player stored the "players" dictionary
from PIL.Image import Image
my_img = Image.frombytes(player.keys()[0])
# Do whatever you want with my_img