Improve parsing chat commands
This commit is contained in:
parent
e4ea9aeaa0
commit
080895421d
|
@ -21,11 +21,12 @@ class Commands:
|
|||
self.g.chat.set_handler(self.handle_chat)
|
||||
|
||||
def handle_chat(self, message):
|
||||
source, text = message
|
||||
source, sender, text = message
|
||||
reply = None
|
||||
private = False
|
||||
for_me = False
|
||||
authed = False
|
||||
authed = sender == '0c123cfa-1697-4427-9413-4b645dee7ec0'
|
||||
bot_num = self.g.name[-1]
|
||||
|
||||
if source == 'SYSTEM':
|
||||
self.g.command_lock = False
|
||||
|
@ -35,40 +36,20 @@ class Commands:
|
|||
elif text == 'You are no longer AFK.':
|
||||
self.g.afk = False
|
||||
|
||||
match1 = re.match(r'.*<(\w+)> (.*)', text)
|
||||
match2 = re.match(r'\[(\w+) -> me] (.*)', text)
|
||||
if match1:
|
||||
sender, text = match1.groups()
|
||||
elif match2:
|
||||
sender, text = match2.groups()
|
||||
text = text.replace('zzz', '!zzz')
|
||||
|
||||
match = re.match(r'(.*\W+)\s+(['+bot_num+'|!])(\w+) ?(.*)', text)
|
||||
if match:
|
||||
meta, prefix, command, data = match.groups()
|
||||
else:
|
||||
return
|
||||
|
||||
if '-> me' in meta:
|
||||
private = True
|
||||
else:
|
||||
return
|
||||
|
||||
if sender == 'tanner6':
|
||||
authed = True
|
||||
|
||||
if text.startswith('zzz'):
|
||||
text = '!zzz'
|
||||
|
||||
bot_num = self.g.name[-1]
|
||||
|
||||
if text.startswith(bot_num):
|
||||
text = text[1:]
|
||||
if prefix == bot_num:
|
||||
for_me = True
|
||||
elif text.startswith('! '):
|
||||
text = text[2:]
|
||||
elif text.startswith('!'):
|
||||
text = text[1:]
|
||||
else:
|
||||
return
|
||||
|
||||
if ' ' in text:
|
||||
command = text.split(' ', 1)[0]
|
||||
data = text.split(' ', 1)[1]
|
||||
else:
|
||||
command = text
|
||||
data = None
|
||||
|
||||
try:
|
||||
|
||||
|
@ -391,14 +372,9 @@ class Commands:
|
|||
|
||||
## 1here - bot comes to your location
|
||||
if command == 'here':
|
||||
try:
|
||||
sender_uuid = self.g.player_names[sender]
|
||||
except KeyError:
|
||||
reply = 'can\'t find your uuid'
|
||||
|
||||
if not reply:
|
||||
for p in self.g.players.values():
|
||||
if p.player_uuid == sender_uuid:
|
||||
if p.player_uuid == sender:
|
||||
player = p
|
||||
break
|
||||
else: # for
|
||||
|
@ -525,7 +501,7 @@ class Commands:
|
|||
reply = 'reply too long, check console'
|
||||
|
||||
if private and not reply.startswith('/'):
|
||||
self.g.chat.send('/m ' + sender + ' ' + reply)
|
||||
self.g.chat.send('/r ' + reply)
|
||||
else:
|
||||
self.g.chat.send(reply)
|
||||
|
||||
|
|
|
@ -161,24 +161,25 @@ class ChatManager:
|
|||
def translate_chat(self, data):
|
||||
if isinstance(data, str):
|
||||
return data
|
||||
|
||||
result = data.get('text', '')
|
||||
result += data.get('translate', '')
|
||||
|
||||
if 'with' in data:
|
||||
result += ' ' + ' '.join([self.translate_chat(x) for x in data['with']])
|
||||
elif 'extra' in data:
|
||||
return ''.join([self.translate_chat(x) for x in data['extra']])
|
||||
elif 'text' in data:
|
||||
return data['text']
|
||||
elif 'with' in data:
|
||||
if len(data['with']) >= 2:
|
||||
return '<{}> {}'.format(*[self.translate_chat(x) for x in data['with']])
|
||||
else:
|
||||
return self.translate_chat(data['with'][0])
|
||||
elif 'translate' in data:
|
||||
return data['translate']
|
||||
else:
|
||||
print(data)
|
||||
return '?'
|
||||
result += ''.join([self.translate_chat(x) for x in data['extra']])
|
||||
|
||||
return result
|
||||
|
||||
def print_chat(self, chat_packet):
|
||||
#print(chat_packet)
|
||||
#print(chat_packet.json_data)
|
||||
#import json
|
||||
#print(json.dumps(json.loads(chat_packet.json_data), indent=4))
|
||||
try:
|
||||
source = chat_packet.field_string('position')
|
||||
sender = chat_packet.sender
|
||||
text = self.translate_chat(json.loads(chat_packet.json_data))
|
||||
print('[%s] %s'%(source, text))
|
||||
except Exception as ex:
|
||||
|
@ -186,7 +187,7 @@ class ChatManager:
|
|||
return
|
||||
|
||||
if self.handler:
|
||||
self.handler((source, text))
|
||||
self.handler((source, sender, text))
|
||||
|
||||
def set_handler(self, func):
|
||||
self.handler = func
|
||||
|
|
Loading…
Reference in New Issue
Block a user