Compare commits

..

No commits in common. "080895421d6466714cea1e16aaed65ad9d8c0524" and "ad6c412802a22ea6a21d0533f778ae1d0520da04" have entirely different histories.

3 changed files with 55 additions and 32 deletions

View File

@ -2,9 +2,9 @@
A general-purpose Minecraft 1.16 bot written in Python. A general-purpose Minecraft 1.16 bot written in Python.
Mosfet is able to farm wood by cutting trees, farm crops, gather sand, farm Mosfet is able to farm wood by cutting trees, gather sand, gather netherwart,
netherwart, and trade with villagers to get emeralds. He can eat, sleep, and and trade with villagers to get emeralds. He can eat, sleep, and flee from
flee from threats. threats.
## Requirements ## Requirements
- Python >= 3.6 - Python >= 3.6
@ -168,4 +168,4 @@ put to use.
## Acknowledgements ## Acknowledgements
Thanks to Isaia, sose, and the devs behind pyCraft. Thanks to Isaia and the devs behind pyCraft.

View File

@ -21,12 +21,11 @@ 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, sender, text = message source, text = message
reply = None reply = None
private = False private = False
for_me = False for_me = False
authed = sender == '0c123cfa-1697-4427-9413-4b645dee7ec0' authed = False
bot_num = self.g.name[-1]
if source == 'SYSTEM': if source == 'SYSTEM':
self.g.command_lock = False self.g.command_lock = False
@ -36,20 +35,40 @@ class Commands:
elif text == 'You are no longer AFK.': elif text == 'You are no longer AFK.':
self.g.afk = False self.g.afk = False
text = text.replace('zzz', '!zzz') match1 = re.match(r'.*<(\w+)> (.*)', text)
match2 = re.match(r'\[(\w+) -> me] (.*)', text)
match = re.match(r'(.*\W+)\s+(['+bot_num+'|!])(\w+) ?(.*)', text) if match1:
if match: sender, text = match1.groups()
meta, prefix, command, data = match.groups() elif match2:
sender, text = match2.groups()
private = True
else: else:
return return
if '-> me' in meta: if sender == 'tanner6':
private = True authed = True
if prefix == bot_num: 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:
@ -372,9 +391,14 @@ 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: if p.player_uuid == sender_uuid:
player = p player = p
break break
else: # for else: # for
@ -501,7 +525,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('/r ' + reply) self.g.chat.send('/m ' + sender + ' ' + reply)
else: else:
self.g.chat.send(reply) self.g.chat.send(reply)

View File

@ -161,25 +161,24 @@ 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:
result += ''.join([self.translate_chat(x) for x in data['extra']]) return ''.join([self.translate_chat(x) for x in data['extra']])
elif 'text' in data:
return result 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 '?'
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:
@ -187,7 +186,7 @@ class ChatManager:
return return
if self.handler: if self.handler:
self.handler((source, sender, text)) self.handler((source, text))
def set_handler(self, func): def set_handler(self, func):
self.handler = func self.handler = func