我正在尝试用Pyautogui为MMO创建一个叫做“Graal Online Classic”的机器人。它会读取发送消息的玩家的“PM”,然后回复相应的响应。它还会截取玩家名称的截图,并将其转换为带有tesseract的字符串,并将其存储在字典中(还存储特定玩家在机器人的响应层次结构中的位置),并使用它来确定玩家是否有消息他们之前。关键字检测
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
keys = players.keys()
image = pyautogui.screenshot('name_test.png', region = name_coords) # Names are screenshots
name_image = str(pytesseract.image_to_string(Image.open('name_test.png')))
print(name_image)
for name_image in keys:
print("User is previous user.")
if players[name_image] == None:
players[name_image] = option
else:
players[name_image] = players[name_image] + option
return players[name_image]
else:
print("User was not previously found, adding to dictionary.")
players[name_image] = None
print(players.keys())
print(players)
问题源于上述功能。当它发现之前有消息的人时,应该去if语句,但无论如何,else语句都会执行。我在播放器中打印值,当播放器第二次发送消息时,它的值相同,但是在键中的name_image:仍然返回false。
如果需要https://pastebin.com/haJRfqJD
这里是3个消息
None
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
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]
Replying...
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
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]
Replying...
(454, 222)
Found a waiting message
Found an open message
1003 424
Player messaged 1
1
I hate sand
User was not previously found, adding to dictionary.
dict_keys(['I hate sand'])
{'I hate sand': None}
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]
Replying...
如果您正在检查是否存在使用'if not not name_image in players:'的密钥。 – Darkstarone
'name_image'取自'players.keys()'。在什么情况下,它与值None相关联? – khelwood
http://stackoverflow.com/help/someone-answers – e4c5