forked from ammaraskar/pyCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AstaraBot.py
280 lines (232 loc) · 10.4 KB
/
AstaraBot.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
from __future__ import print_function
import getpass
import sys
import re
import json
import threading
import time
from optparse import OptionParser
from minecraft import authentication
from minecraft.exceptions import YggdrasilError
from minecraft.networking.connection import Connection
from minecraft.networking.packets import Packet, clientbound, serverbound
from minecraft.compat import input
def get_options():
parser = OptionParser()
parser.add_option("-u", "--username", dest="username", default=None,
help="username to log in with")
parser.add_option("-p", "--password", dest="password", default=None,
help="password to log in with")
parser.add_option("-s", "--server", dest="server", default=None,
help="server host or host:port "
"(enclose IPv6 addresses in square brackets)")
parser.add_option("-o", "--offline", dest="offline", action="store_true",
help="connect to a server in offline mode "
"(no password required)")
parser.add_option("-d", "--dump-packets", dest="dump_packets",
action="store_true",
help="print sent and received packets to standard error")
(options, args) = parser.parse_args()
if not options.username:
options.username = input("Enter your username: ")
if not options.password and not options.offline:
options.password = getpass.getpass("Enter your password (leave "
"blank for offline mode): ")
options.offline = options.offline or (options.password == "")
if not options.server:
options.server = input("Enter server host or host:port "
"(enclose IPv6 addresses in square brackets): ")
# Try to split out port and address
match = re.match(r"((?P<host>[^\[\]:]+)|\[(?P<addr>[^\[\]]+)\])"
r"(:(?P<port>\d+))?$", options.server)
if match is None:
raise ValueError("Invalid server address: '%s'." % options.server)
options.address = match.group("host") or match.group("addr")
options.port = int(match.group("port") or 25565)
return options
def main():
options = get_options()
if options.offline:
print("Connecting in offline mode...")
connection = Connection(
options.address, options.port, username=options.username)
else:
auth_token = authentication.AuthenticationToken()
try:
auth_token.authenticate(options.username, options.password)
except YggdrasilError as e:
print(e)
sys.exit()
print("Logged in as %s..." % auth_token.username)
connection = Connection(
options.address, options.port, auth_token=auth_token)
if options.dump_packets:
def print_incoming(packet):
if type(packet) is Packet:
# This is a direct instance of the base Packet type, meaning
# that it is a packet of unknown type, so we do not print it.
return
print('--> %s' % packet, file=sys.stderr)
def print_outgoing(packet):
print('<-- %s' % packet, file=sys.stderr)
connection.register_packet_listener(
print_incoming, Packet, early=True)
connection.register_packet_listener(
print_outgoing, Packet, outgoing=True)
def handle_join_game(join_game_packet):
print('Connected.')
connection.register_packet_listener(
handle_join_game, clientbound.play.JoinGamePacket)
# debug chat callback
def print_chat(chat_packet):
print("Message (%s): %s" % (
chat_packet.field_string('position'), chat_packet.json_data))
# lists serves as output message queues
outQueue = []
# boolean to tell if we are running
running = True
# console input callback
def input_thread():
while running:
text = input()
outQueue.append(text)
time.sleep(0.1)
# helper function to insert into message queue
def insert_into_queue(message, player, globalFlag):
if globalFlag:
outQueue.append(message)
else:
outQueue.append('/msg ' + player + ' ' + message)
# helper function to handle commands
def process_message(message, player, playerId, globalFlag):
if message == '$whoami':
insert_into_queue('You are ' + player + '!', player, globalFlag)
if message == '$selling':
try:
dealFile = open('deals.json', 'r')
deals = json.loads(dealFile.read())
dealFile.close()
items = []
for item in deals['selling']:
items.append(item['item'])
insert_into_queue('Selling these items: ' + str(items), player, globalFlag)
except:
insert_into_queue('Sorry! Deals are not available at this time.', player, globalFlag)
if message == '$buying':
try:
dealFile = open('deals.json', 'r')
deals = json.loads(dealFile.read())
dealFile.close()
insert_into_queue('Buying these items: ' + str(deals['buying']), player, globalFlag)
except:
insert_into_queue('Sorry! Deals are not available at this time.', player, globalFlag)
if message == '$reps':
try:
dealFile = open('deals.json', 'r')
deals = json.loads(dealFile.read())
dealFile.close()
outQueue.append('Representatives of Astara: ' + str(deals['representatives']))
except:
insert_into_queue('Sorry! Database is not available at this time.', player, globalFlag)
if message == '$help':
insert_into_queue('Hi! I\'m the Astaran Trade Bot. Minimum trade value is 1db.' + \
' Here are some commands you can use: $selling, $buying, $price <item>, ' + \
'$whoami, $reps, $help', player, globalFlag)
if message.startswith('$price '):
try:
query = message[7:]
dealFile = open('deals.json', 'r')
deals = json.loads(dealFile.read())
dealFile.close()
name = ''
price = ''
for item in deals['selling']:
for alias in item['alias']:
if query.lower() == alias.lower():
name = item['item']
price = item['price']
break;
if name != '':
break;
if name == '' or price == '':
insert_into_queue('Sorry! No price listed for that item.', player, globalFlag)
else:
insert_into_queue('Astara sells ' + name + ' for ' + price, player, globalFlag)
except:
insert_into_queue('Sorry! No price listed for that item.', player, globalFlag)
# chat processing callback
def process_chat(chat_packet):
position = chat_packet.field_string('position')
if position == 'CHAT' or position == 'SYSTEM':
data = json.loads(chat_packet.json_data)
# Global Chat
if data['translate'] == 'chat.type.text':
# grab useful data from json
message = data['with'][1]
player = data['with'][0]['insertion']
hoverStr = data['with'][0]['hoverEvent']['value']['text']
start = hoverStr.index('id:\"') + 4
end = hoverStr.index('\",type:')
playerId = hoverStr[start:end]
# print chat message
outStr = playerId + ' (' + player + '): ' + message
print(outStr)
# log message
log = open('log.txt', 'a')
log.write(outStr + '\n')
log.close()
# process message
process_message(message, player, playerId, True)
# Private Chat
if data['translate'] == 'commands.message.display.incoming':
# grab useful data from json
message = data['with'][1]['text']
player = data['with'][0]['insertion']
hoverStr = data['with'][0]['hoverEvent']['value']['text']
start = hoverStr.index('id:\"') + 4
end = hoverStr.index('\",type:')
playerId = hoverStr[start:end]
# print chat message
outStr = playerId + ' (' + player + ') PRIVATE: ' + message
print(outStr)
# log message
log = open('log.txt', 'a')
log.write(outStr + '\n')
log.close()
# process message
process_message(message, player, playerId, False)
# Register Debug callback
#connection.register_packet_listener(
# print_chat, clientbound.play.ChatMessagePacket)
# Register our chatbot logic
connection.register_packet_listener(
process_chat, clientbound.play.ChatMessagePacket)
# start network thread
connection.connect()
# start console thread
inThread = threading.Thread(target=input_thread)
inThread.start()
# Main bot console loop
while running:
try:
time.sleep(0.05)
if len(outQueue) != 0:
msg = outQueue.pop()
if msg == '/respawn':
print('Respawning...')
packet = serverbound.play.ClientStatusPacket()
packet.action_id = serverbound.play.ClientStatusPacket.RESPAWN
connection.write_packet(packet)
elif msg == '/exit':
print('Disconnecting')
running = False
else:
print('Sent Message: ' + msg)
packet = serverbound.play.ChatPacket()
packet.message = msg
connection.write_packet(packet)
except KeyboardInterrupt:
outQueue.append('/exit')
if __name__ == "__main__":
main()