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