-
Notifications
You must be signed in to change notification settings - Fork 1
/
start.py
executable file
·135 lines (110 loc) · 4.14 KB
/
start.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import configparser
import os.path
import sys
import re
import time
from pydoc import locate
from quarry.net.client import ClientProtocol, ClientFactory
from quarry.mojang.profile import Profile
from devbot import chat
CONFIG_FILE = 'config.cfg'
CONFIG = configparser.ConfigParser()
COMMAND_FILE = 'commands.cfg'
COMMANDS = configparser.ConfigParser()
COMMANDS.read(COMMAND_FILE)
class DevotedBotClientProtocol(ClientProtocol):
def packet_keep_alive(self, buff):
identifier = buff.unpack_varint(signed=True)
self.send_packet('keep_alive', self.buff_type.pack_varint(identifier, signed=True))
global COMMANDS
COMMANDS = configparser.ConfigParser()
COMMANDS.read(COMMAND_FILE)
chat.process_chat(self)
def packet_update_health(self, buff):
health = buff.unpack('f')
food = buff.unpack_varint()
saturation = buff.unpack('f')
if health <= 0:
self.send_packet('client_status', self.buff_type.pack_varint(0))
chat.say('/g GlobalChat')
chat.say('Please do not kill DevotedBot.')
print('Died, respawning...')
def packet_disconnect(self, buff):
message = buff.unpack_chat()
print('Disconnected for reason:', message)
factory.stop()
def packet_chat_message(self, buff):
chat = buff.unpack_chat()
position = buff.unpack("b")
if position == 0:
print(':: ' + chat)
elif position == 1:
result = handle_chat(chat, self)
if not result:
print(':=: ' + chat)
elif position == 2:
print(':_: ' + chat)
else:
self.logger.warning('Unhandled chat position: ' + position + ' skipping.')
return
class DevotedBotClientFactory(ClientFactory):
protocol = DevotedBotClientProtocol
factory = DevotedBotClientFactory()
def generate_config_file(name, config):
config['auth'] = {'username': 'USERNAME',
'password': 'PASSWORD',
'auth': 'true'}
config['server'] = {'ip': 'play.devotedmc.com',
'port': '25565'}
with open(name, 'w') as configfile:
configfile.write('# Config automatically generated. Delete this file to reset.\n\n')
config.write(configfile)
print('Config file generated. Edit the username and password fields to set up an account.')
sys.exit()
def main():
if os.path.isfile(CONFIG_FILE):
CONFIG.read(CONFIG_FILE)
else:
generate_config_file(CONFIG_FILE, CONFIG)
profile = Profile()
if not CONFIG['auth'].getboolean('auth'):
profile.login_offline(CONFIG['auth']['username'])
factory.profile = profile
else:
def login_ok(data):
factory.connect(CONFIG['server']['ip'], int(CONFIG['server']['port']))
def login_failed(err):
print('login failed:', err.value)
factory.stop()
sys.exit(1)
factory.profile = profile
deferred = profile.login(CONFIG['auth']['username'], CONFIG['auth']['password'])
deferred.addCallbacks(login_ok, login_failed)
factory.run()
def handle_chat(message, protocol):
"""
:return: Weather or not the chat message was a valid command
"""
if message == 'You are already chatting in that group.':
return True
elif re.match(r'From Amelorate: ssh', message):
chat.say(message[20:])
return True
match = re.match(r'From .*:\s', message)
if match:
print(':c: ' + message[5:])
name = message[5:].split(': ')[0]
message = message[match.end():]
for command in COMMANDS['regex'].keys():
match = re.match(command + r'\s', message, re.IGNORECASE)
if match:
message = message[match.end():]
message = message[:-1]
locate('commands.' + COMMANDS['regex'][command] + '.call')(message, name, protocol, CONFIG, COMMANDS)
return True
chat.say('/msg ' + name + ' Sorry, that command was not recognized as valid.')
return False
else:
return False
if __name__ == '__main__':
main()