forked from silveryferret/AphroditeBot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBotCommands.py
More file actions
385 lines (343 loc) · 15.2 KB
/
BotCommands.py
File metadata and controls
385 lines (343 loc) · 15.2 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import discord
import asyncio
import struct
import ast
import urllib.parse
import config
def format_packet(msg):
return b"\x00\x83" + struct.pack(">H", len(msg) + 6) + \
b"\x00\x00\x00\x00\x00" + bytes(msg, "ascii") + b"\x00"
def handle_outgoing(payload, loop):
reader, writer = yield from asyncio.open_connection(config.host, config.gameport, loop=loop)
packet = format_packet(payload)
writer.write(packet)
headerReceived = yield from reader.read(2)
if headerReceived != b"\x00\x83":
print("Unexpected packet.")
packetLength = yield from reader.read(2)
packetLength = int.from_bytes(packetLength, "big")
received = yield from reader.read(packetLength)
received = received[1:-1]
received = received.decode("utf8")
print("handle_outgoing(): " + received + "\n")
writer.close()
return received
def get_command(messageObj):
commandstring = ""
parameter = ""
cmdMsg = ""
i = messageObj.content.strip(config.triggerString)
commandstring = i.split(" ")[0]
if len(i.split(" ")) >= 2:
parameter = i.split(" ")[1]
if len(i.split(" ", maxsplit=2)) >= 3:
cmdMsg = i.split(" ", maxsplit=2)[2]
command = (commandstring, parameter, cmdMsg)
return command
def has_perms(user):
for i in user.roles:
if str(i) in config.perm_roles:
return True
else:
perm = False
return perm
class Command(object):
def __init__(self, client, loop, message):
self.client = client
self.loop = loop
self.message = message
@asyncio.coroutine
def do_command(self):
pass
'''
class Ping(Command):
@asyncio.coroutine
def do_command(self):
try:
command = get_command(self.message)[0]
yield from handle_outgoing(command, self.loop)
yield from self.client.send_message(self.message.channel, "Server is online.")
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
'''
class Status(Command):
@asyncio.coroutine
def do_command(self):
try:
command = "status"
status = yield from handle_outgoing(command, self.loop)
status = urllib.parse.parse_qs(status)
version = status["version"][0]
mode = status["mode"][0]
respawn = status["respawn"][0]
admins = status["admins"][0]
playercount = status["players"][0]
roundduration = status["roundduration"][0]
stationtime = status["stationtime"][0]
playerList = []
for key in status:
if "player" in key and not "players" in key:
playerList.append(status[key][0])
statusMsg = "```Admins online: %s\r\n" % admins
statusMsg += "Game mode: %s\r\n" % mode
statusMsg += "Round duration: %s\r\n" % roundduration
statusMsg += "Station time: %s\r\n" % stationtime
statusMsg += "Players online: %s\r\n```" % playercount
yield from self.client.send_message(self.message.channel, statusMsg)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class Players(Command):
@asyncio.coroutine
def do_command(self):
try:
command = "status"
status = yield from handle_outgoing(command, self.loop)
status = urllib.parse.parse_qs(status)
playercount = status["players"][0]
playerList = []
for key in status:
if "player" in key and not "players" in key:
playerList.append(status[key][0])
playerList = sorted(playerList)
playerMsg = "```\r\n"
for player in playerList:
playerMsg = playerMsg + player + "\r\n"
playerMsg += "\r\nPlayers online: %s```" % playercount
yield from self.client.send_message(self.message.author, playerMsg)
except OSError:
yield from self.client.send_message(self.message.author, "Server is offline.")
'''
class Manifest(Command):
def fill_departments(self, manifest, departments, departmentName, manifestMsg):
manifestMsg += departmentName
for name in departments:
position = departments[name]
manifestMsg += name + " - " + position + "\r\n"
return manifestMsg
@asyncio.coroutine
def do_command(self):
try:
command = "manifest"
manifest = yield from handle_outgoing(command, self.loop)
manifest = str(manifest)
manifest = ast.literal_eval(manifest)
manifestMsg = "```"
if manifest == []:
manifestMsg += "No crew found."
else:
try:
manifestMsg = self.fill_departments(manifest, manifest["heads"], "Command:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["sec"], "Security:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["eng"], "Engineering:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["med"], "Medical:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["sci"], "Science:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["car"], "Cargo:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["civ"], "Civilian:\r\n", manifestMsg)
manifestMsg += "\r\n"
except KeyError:
pass
try:
manifestMsg = self.fill_departments(manifest, manifest["bot"], "Silicon:\r\n", manifestMsg)
manifestMsg += "```"
except KeyError:
pass
if manifestMsg == "``````":
manifestMsg = "No crew found."
manifestMsg += "```"
yield from self.client.send_message(self.message.author, manifestMsg)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
'''
class Revision(Command):
@asyncio.coroutine
def do_command(self):
try:
command = "revision"
revision = yield from handle_outgoing(command, self.loop)
revision = urllib.parse.parse_qs(revision)
revisionMsg = "```"
revisionMsg += "Date: " + revision['date'][0] + "\r\n"
revisionMsg += "Revision: " + revision['revision'][0] + "\r\n"
revisionMsg += "Game ID: " + revision['gameid'][0] + "\r\n"
revisionMsg += "Dream Daemon version: " + revision['dd_version'][0] + "\r\n"
revisionMsg += "Dream Maker version: " + revision['dm_version'][0] + "\r\n"
revisionMsg += "Branch: " + revision['branch'][0] + "\r\n"
revisionMsg += "```"
yield from self.client.send_message(self.message.channel, revisionMsg)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class Info(Command):
def parse_damage(self, damage):
dam = urllib.parse.parse_qs(damage)
if dam == {}:
damtup = ("Not living", "Not living", "Not living", "Not living", "Not living", "Not living")
else:
damtup = (dam["oxy"][0], dam["tox"][0], dam["fire"][0], dam["brute"][0], dam["clone"][0], dam["brain"][0])
return damtup
@asyncio.coroutine
def do_command(self):
try:
commandtup = get_command(self.message)
command = "?info=" + commandtup[1]
command += ";key=" + config.commskey
info = yield from handle_outgoing(command, self.loop)
if info == "No matches":
yield from self.client.send_message(self.message.channel, "No matches.")
else:
info = urllib.parse.parse_qs(info)
area = info["area"][0]
info["area"][0] = area.replace("%ff", "")
infoMsg = "```"
infoMsg += "Key: " + info["key"][0] + "\r\n"
infoMsg += "Name: " + info["name"][0] + "\r\n"
infoMsg += "Role: " + info["role"][0] + "\r\n"
infoMsg += "Location: " + info["loc"][0] + "\r\n"
infoMsg += "Turf: " + info["turf"][0] + "\r\n"
infoMsg += "Area: " + info["area"][0] + "\r\n"
infoMsg += "Antag: " + info["antag"][0] + "\r\n"
infoMsg += "Has been rev?: "
if int(info["hasbeenrev"][0]):
infoMsg += "Yes" + "\r\n"
else:
infoMsg += "No" + "\r\n"
infoMsg += "Status: " + info["stat"][0] + "\r\n"
infoMsg += "Mob type: " + info["type"][0] + "\r\n"
damages = self.parse_damage(info["damage"][0])
infoMsg += "Damage: " + "\r\n"
infoMsg += "--Oxy: " + damages[0] + "\r\n"
infoMsg += "--Tox: " + damages[1] + "\r\n"
infoMsg += "--Fire: " + damages[2] + "\r\n"
infoMsg += "--Brute: " + damages[3] + "\r\n"
infoMsg += "--Clone: " + damages[4] + "\r\n"
infoMsg += "--Brain: " + damages[5]
infoMsg += "```"
yield from self.client.send_message(self.message.channel, infoMsg)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class AdminMsg(Command):
@asyncio.coroutine
def do_command(self):
try:
commandtup = get_command(self.message)
author = self.message.author
command = "?adminmsg=" + commandtup[1]
command += ";msg=" + commandtup[2]
command += ";key=" + config.commskey
command += ";sender=" + author.name
confirmation = yield from handle_outgoing(command, self.loop)
yield from self.client.send_message(self.message.channel, confirmation)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class Notes(Command):
def parse(self, qs):
qs = qs.replace("%26%2334%3b", '"')
qs = qs.replace("%26%2339%3b", '"')
qs = qs.replace("%26amp%3b", "&")
qs = qs.replace("%26%2339;", "'")
qs = qs.replace("%0d", "\r")
qs = qs.replace("%0a", "\n")
qs = qs.replace("%28", "(")
qs = qs.replace("%29", ")")
qs = qs.replace("%2b", "+")
qs = qs.replace("%2c", ",")
qs = qs.replace("%2f", "/")
qs = qs.replace("%3a", ":")
qs = qs.replace("%3b", ";")
qs = qs.replace("%3f", "?")
qs = qs.replace("%5b", "[")
qs = qs.replace("%5d", "]")
qs = qs.replace("+", " ")
return qs
def format_for_sending(self, message):
message = "```" + message + "```"
return message
@asyncio.coroutine
def send(self, qs):
message = self.parse(qs)
if len(message) >= 1994:
fmtmessage = self.format_for_sending(message[:1994])
yield from self.client.send_message(self.message.channel, fmtmessage)
message = message[1994:]
yield from self.send(message)
else:
message = "```" + message + "```"
yield from self.client.send_message(self.message.channel, message)
@asyncio.coroutine
def do_command(self):
try:
commandtup = get_command(self.message)
command = "?notes=" + commandtup[1]
command += ";key=" + config.commskey
qs = yield from handle_outgoing(command, self.loop)
yield from self.send(qs)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class Age(Command):
@asyncio.coroutine
def do_command(self):
try:
commandtup = get_command(self.message)
command = "?age=" + commandtup[1]
command += ";key=" + config.commskey
age = yield from handle_outgoing(command, self.loop)
if age == "Ckey not found":
yield from self.client.send_message(self.message.channel, age)
else:
yield from self.client.send_message(self.message.channel, "Account is %s days old." % age)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class IP(Command):
@asyncio.coroutine
def do_command(self):
try:
commandtup = get_command(self.message)
command = "?ip=" + commandtup[1]
command += ";key=" + config.commskey
ip = yield from handle_outgoing(command, self.loop)
yield from self.client.send_message(self.message.channel, ip)
except OSError:
yield from self.client.send_message(self.message.channel, "Server is offline.")
class Help(Command):
@asyncio.coroutine
def do_command(self):
prepend = config.triggerString
helpMsg = ""
helpMsg += "```Aphrodite Bot Commands:\r\n"
# helpMsg += prepend + "ping - checks if server is up\r\n"
helpMsg += prepend + "status - status, including round duration, station time,\r\n"
helpMsg += "\tplayers online\r\n"
helpMsg += prepend + "players - PMs you a message of all players on server\r\n"
# helpMsg += prepend + "manifest - PMs you a message of the in round crew manifest\r\n"
# helpMsg += prepend + "revision - shows current server revision\r\n"
if self.message.channel.id == config.ahelpID:
helpMsg += prepend + "info <ckey> - shows detailed information about ckey\r\n"
helpMsg += prepend + "msg <ckey> <message> - adminhelps from discord to game\r\n"
helpMsg += prepend + "notes <ckey> - get player notes of ckey\r\n"
helpMsg += prepend + "age <ckey> - shows player age of ckey\r\n"
# helpMsg += prepend + "ip <ckey> - shows IP of ckey"
helpMsg += "```"
yield from self.client.send_message(self.message.channel, helpMsg)