-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
497 lines (401 loc) · 19.2 KB
/
client.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import re
import nextcord
import socket
from nextcord.ext import commands
from nextcord import Interaction, Embed
from rcon.source import Client as rcon_cl
from rcon.exceptions import *
from .ssh import SSHCl
from utils.logger import get_logger
from .commands import *
from functools import wraps
from .navigator import nav, By
from .pages import EmbededModsPageSource, menus
from pretty_help import PrettyHelp
import sys
sys.path.append("..")
from db.db import ModsDB, Document, Query
class DiscordBot(commands.Bot):
"""
A class that stores a discord client and bot info
"""
def __init__(self, config, *args, **kwargs):
super().__init__(*args, **kwargs)
self._config = config
self.logger = get_logger()
# SSH Channels to invoke server (keeps listening) and ping commands
self.ssh_cl = SSHCl(config)
self.ssh_server = SSHCl(config) # SSH Channel always open to keep server running
# Buffers of server invoke
self.server_stdio = None
self.server_stdout = None
self.server_stderr = None
# TinyDB Mods Database initialization
self.mods = ModsDB("db/mods/mods.json", indent=4)
# SSH common functions
@self.ssh_cl.ping_command(f"pgrep -u {config['ssh_user']} {config['start_file']}")
def ping_pgrep(streams, *args, **kwargs):
stdout, stderr = streams
self.logger.debug(stdout)
self.logger.debug(stderr)
message = None
proc = next(iter(stdout), None)
if len(stdout):
message = f"Server running on proccess {proc}"
elif stderr:
message = f"Errors found on command: {stderr}"
else:
message = f"Server is not running"
return message, proc
self.pgrep = ping_pgrep
async def on_ready(self):
self.logger.info(f"{self.user} has connected to Discord")
# async def on_message(self, message : nextcord.message.Message):
# self.logger.debug(message)
# if not message.author == self.user:
# await message.channel.send("Hmm... Coquinha Gelada")
async def on_command_error(self, context: commands.Context, exception: commands.errors.CommandError) -> None:
if isinstance(exception, commands.CommandNotFound):
await context.message.add_reaction("\N{CROSS MARK}")
await context.send("Command Not Found")
else:
self.logger.debug(exception)
def local_run(self):
self.run(self._config["bot_token"])
def get_mod_by_url(self, url):
id_regex = re.compile(r"id=(\d{9,11})")
workshop_regex = re.compile(r"Workshop ID: (\d{9,11})")
mod_regex = [re.compile(r"Mod ID: (\w+)"), re.compile(r"ModID: (\w+)")]
map_regex = re.compile(r"Map Folder: (\w+)")
mod_id = id_regex.findall(url)
if not mod_id:
self.logger.debug(f"Expected URL from a Steam Workshop Mod. {url}")
raise ValueError(f"Expected URL from a Steam Workshop Mod. {url}")
else:
mod_id = next(iter(mod_id))
self.logger.debug(f"Mod ID Found: {mod_id}")
nav.get(url)
result = nav.find_element(By.ID, 'highlightContent').text
title = nav.find_element(By.CLASS_NAME, 'workshopItemTitle').text
for reg in mod_regex:
text_mod_id = reg.findall(result)
if text_mod_id:
break
text_workshop_id = workshop_regex.findall(result)
map_folder = map_regex.findall(result)
self.logger.debug(f"Mod Title: {title}; Workshop ID: {mod_id}; Mod ID(s): {', '.join(text_mod_id)}")
return text_workshop_id, title, text_mod_id, map_folder
async def fail_response(ctx: commands.Context, message: str = None):
await ctx.message.clear_reaction("⏳")
await ctx.message.add_reaction("\N{CROSS MARK}")
if message:
await ctx.send(message)
async def success_response(ctx: commands.Context, message: str = None):
await ctx.message.clear_reaction("⏳")
await ctx.message.add_reaction("\N{WHITE HEAVY CHECK MARK}")
if message:
await ctx.send(message)
def get_bot(config, *args, **kwargs):
intent = nextcord.Intents.default()
intent.message_content = True
bot = DiscordBot(config,
intents=intent,
help_command=PrettyHelp(
show_index=False,
no_category="Commands"),
*args,
**kwargs)
@bot.slash_command(name="status", description="Returns Bot Status", guild_ids=[config["guild_id"]])
async def status(interaction: Interaction):
await interaction.response.send_message("Bot Loaded and Runnning")
@bot.slash_command(name="running", description="Returns if the PZ Server is Runnning", guild_ids=[config["guild_id"]])
async def running(intercation: Interaction):
await intercation.response.defer()
@bot.ssh_cl.ping_command(f"pgrep -u {config['ssh_user']} {config['start_file']}")
def ping_pgred(streams, *args, **kwargs):
stdout, stderr = streams
bot.logger.debug(stdout)
bot.logger.debug(stderr)
message = None
if len(stdout):
message = f"Server running on proccess {stdout[0]}"
elif stderr:
message = f"Errors found on command: {stderr}"
else:
message = f"Server is not running"
return message
try:
message = ping_pgred()
except:
message = f"Failed to communicate with server {config['server']}"
await intercation.followup.send(content= message)
@bot.slash_command(name="restart", description="Restarts the server on runner", guild_ids=[config["guild_id"]])
async def restart(interaction: Interaction):
await interaction.response.defer()
@bot.ssh_cl.ping_command(f"pgrep -u {config['ssh_user']} {config['start_file']}")
def ping_pgred(streams, *args, **kwargs):
stdout, _ = streams
bot.logger.debug(stdout)
return next(iter(stdout), None)
proc = ping_pgred()
@bot.ssh_cl.ping_command(f"kill -9 {proc}")
def kill_server(streams, *args, **kwargs):
stdout, _ = streams
bot.logger.debug(stdout)
return stdout
if proc:
await interaction.followup.send(content=f"Server Running on {proc}. Restarting...")
# await interaction.response.send_message(kill_server())
kill_server()
else:
await interaction.followup.send(content="No Server Running. Initializing Server")
bot.server_stdio, bot.server_stdout, bot.server_stderr = bot.ssh_server.attached_command(
"/".join([
config["start_path"],
config["start_file"]
])
)
bot.server_stdio.close()
bot.logger.info("Server Started")
@bot.slash_command(name="update", description="Update Server on runner", guild_ids=[config["guild_id"]])
async def update(interaction: Interaction):
await interaction.response.defer()
@bot.ssh_cl.ping_command(f"pgrep -u {config['ssh_user']} {config['start_file']}")
def ping_pgred(streams, *args, **kwargs):
stdout, _ = streams
bot.logger.debug(stdout)
return next(iter(stdout), None)
proc = ping_pgred()
@bot.ssh_cl.ping_command(f"kill -9 {proc}")
def kill_server(streams, *args, **kwargs):
stdout, _ = streams
bot.logger.debug(stdout)
return stdout
if proc:
kill_server()
await interaction.send("Updating Server:")
@bot.ssh_cl.ping_command(f"steamcmd +runscript {config['update_script']}")
def update_server(streams, *args, **kwargs):
stdout, _ = streams
bot.logger.debug(stdout)
return stdout
messages = "\n".join(update_server())
await interaction.send(f'```ansi\n{messages}\n```')
bot.server_stdio, bot.server_stdout, bot.server_stderr = bot.ssh_server.attached_command(
"/".join([
config["start_path"],
config["start_file"]
])
)
bot.server_stdio.close()
await interaction.followup.send(content="Server Updated")
bot.logger.info("Server Started")
# @bot.command(name="info")
# async def help(ctx: commands.Context, *args):
# command = next(iter(args), None)
# await ctx.message.add_reaction("⏳")
# if command and not command in zz_help:
# await fail_response(ctx, f"Command **{command}** Not Found")
# elif command and command in zz_help:
# message = f"Command **{command}**:\n{'-'*2} Usage: {zz_help[command].usage}\n{'-'*2} Description: {zz_help[command].description}"
# await success_response(ctx, message)
# else:
# message = ""
# for command in zz_help:
# message += f"Command **{command}**:\n{'-'*2} Usage: {zz_help[command].usage}\n{'-'*2} Description: {zz_help[command].description}\n\n"
# await success_response(ctx, message)
@bot.command(name="log",
usage=zz_help["log"].usage,
description=zz_help["log"].description,
brief=zz_help["log"].brief,
help=zz_help["log"].help)
async def log(ctx: commands.Context, *args):
count = 5
if len(args):
count = int(next(iter(args)))
await ctx.message.add_reaction("⏳")
if bot.server_stdout:
bot.server_stdout.channel.settimeout(config.pop("rcon_timeout", 10)) # sets interval to wait for new messages
last_lines = list()
try:
for line in iter(bot.server_stdout.readline, ""):
last_lines.append(line)
bot.logger.debug(line)
except:
pass
last_lines = last_lines[-count:]
# Checks Wheter maximum Embed description size is reached
while sum(len(line) for line in last_lines) >= 4096:
last_lines = last_lines[1:]
message = "".join(last_lines)
if not message:
message = "No new Logs Available"
await success_response(ctx, f"Retrieved {len(last_lines)} log lines")
if len(message) > 2000:
embed = Embed(description=message, colour = nextcord.Colour.purple())
await ctx.send(embed = embed)
else:
await ctx.send(f"```{message}```")
else:
await fail_response(ctx, "Server not Running.")
@bot.command(name="add_mod",
usage=zz_help["add_mod"].usage,
description=zz_help["add_mod"].description,
brief=zz_help["add_mod"].brief,
help=zz_help["add_mod"].help)
async def addmod(ctx: commands.Context, *args):
bot.logger.debug(f"add_mod. Command Arguments: {args}")
await ctx.message.add_reaction("⏳")
if len(args) != 1:
await fail_response(ctx, "Invalid number of arguments for command add_mod")
return
url = args[0]
try:
mod_id, title, text_mod_id, map_folder = bot.get_mod_by_url(url)
mod_id = next(iter(mod_id))
except Exception as e:
bot.logger.debug(e)
await fail_response(ctx, f"{e}. Cannot Navigate URL")
return
if map_folder:
await fail_response(ctx, "Cannot Handle Map Mods Yet")
return
bot.logger.debug(f"URL Data: {mod_id} {title} {text_mod_id} {map_folder}")
if not bot.mods.contains(doc_id=mod_id):
bot.mods.insert(Document(
{
'id': mod_id,
'title': title,
'url': url,
'mods': text_mod_id,
'map': None,
},
doc_id=mod_id))
else:
await fail_response(ctx, f"Mod {title} is already added to the server")
return
bot.logger.debug("Reading remote server file")
sconfig_lines = bot.ssh_cl.read_remote_file(config["server_file"])
mod_line = next(i for i, l in enumerate(sconfig_lines) if re.search(r"^Mods=", l))
id_line = next(i for i, l in enumerate(sconfig_lines) if re.search(r"^WorkshopItems", l))
bot.logger.debug("Filling remote server file with new mod data")
if ";" in sconfig_lines[mod_line]:
sconfig_lines[mod_line] = ";".join([sconfig_lines[mod_line].strip()] + text_mod_id) + "\n"
sconfig_lines[id_line] = ";".join([sconfig_lines[id_line].strip(), mod_id]) + "\n"
else:
sconfig_lines[mod_line] = sconfig_lines[mod_line].strip() + ";".join(text_mod_id) + "\n"
sconfig_lines[id_line] = sconfig_lines[id_line] + mod_id + "\n"
bot.ssh_cl.write_remote_file(config["server_file"], "".join(sconfig_lines))
await success_response(
ctx,
f"Mod Title: {title}\nWorkshop ID: {mod_id}\nMod ID(s): {', '.join(text_mod_id)}"
)
@bot.command(name="remove_mod",
usage=zz_help["remove_mod"].usage,
description=zz_help["remove_mod"].description,
brief=zz_help["remove_mod"].brief,
help=zz_help["remove_mod"].help
)
async def remove_mod(ctx: commands.Context, *args):
bot.logger.debug(f"remove_mod. Command Arguments: {args}")
await ctx.message.add_reaction("⏳")
if len(args) != 1:
await fail_response(ctx, "Wrong number of arguments for remove_mod")
await ctx.send("Check usage with zz help remove_mod")
return
arg = next(iter(args))
q = Query()
remove_entity = bot.mods.search(q.id == arg) or bot.mods.search(q.title == arg)
if not remove_entity:
await fail_response(ctx, f"Unable to find {arg} id or title")
return
remove_entity = next(iter(remove_entity))
bot.logger.debug(f"Removing Entity {remove_entity}")
bot.mods.remove(doc_ids=[int(remove_entity["id"])])
sconfig_lines = bot.ssh_cl.read_remote_file(config["server_file"])
mod_line = next(i for i, l in enumerate(sconfig_lines) if re.search(r"^Mods=", l))
id_line = next(i for i, l in enumerate(sconfig_lines) if re.search(r"^WorkshopItems", l))
ids = [a["id"] for a in bot.mods.all()]
mods = [name for workshop in bot.mods.all() for name in workshop["mods"]]
bot.logger.debug(f"Writing changes to server config file")
sconfig_lines[mod_line] = f"Mods={';'.join(mods)}\n"
sconfig_lines[id_line] = f"WorkshopItems={';'.join(ids)}\n"
bot.ssh_cl.write_remote_file(config["server_file"], "".join(sconfig_lines))
await success_response(ctx, f"Removed mod {remove_entity['title']} ({remove_entity['id']})")
@bot.command(name="list_mod",
usage=zz_help["list_mod"].usage,
description=zz_help["list_mod"].description,
brief=zz_help["list_mod"].brief,
help=zz_help["list_mod"].help)
async def list_mod(ctx: commands.Context, *args):
bot.logger.debug(f"list_mod. Command Arguments: {args}")
await ctx.message.add_reaction("⏳")
mods = bot.mods.all()
mods = ([f"{mod['id'] or ERROR}: {mod['title'] or ERROR}" for mod in mods])
if not mods:
await fail_response(ctx, "No mods found on server")
else:
#message = ""
await success_response(ctx, f"Total of {len(mods)} Mods on Server")
# for mod in mods:
# if len(message + mod) > 2000:
# await ctx.send(message)
# message = mod
# else:
# message = "\n".join([message, mod])
# await ctx.send(message)
pages = menus.ButtonMenuPages(
source = EmbededModsPageSource(mods),
)
await pages.start(ctx)
# RCON Command Factory
def function_builder(cmd):
@bot.command(name=cmd.name,
usage=zz_help[cmd.name].usage,
description=zz_help[cmd.name].description,
brief=zz_help[cmd.name].brief,
help=zz_help[cmd.name].help)
async def SendMessage(ctx: commands.Context, *args):
bot.logger.debug(ctx.message)
bot.logger.debug(f"Command Arguments: {args}")
bot.logger.debug(cmd)
await ctx.message.add_reaction("⏳")
# adds double quotes to arguments that contains space
args = [f'"{a}"' if " " in a else f"{a}" for a in args]
if len(args) not in cmd.args:
await fail_response(ctx, f"Invalid number of arguments for command {cmd.name}")
return
try:
with rcon_cl(config["server"],
config["rcon_port"],
passwd=config["rcon_pwd"],
timeout=config["rcon_timeout"]) as cl:
message = cl.run(cmd.name, *args)
bot.logger.debug(f"Command Response: {message}")
# specific behaviour from checkModsNeedUpdate that writes to server log
if cmd.name == "checkModsNeedUpdate":
if bot.server_stdout:
await ctx.send(message)
message = None
bot.server_stdout.channel.settimeout(3) # sets interval to wait for new messages
try:
logs = list()
for line in iter(bot.server_stdout.readline, ""):
if("CheckMods" in line):
# await ctx.send(line)
logs.append(line)
bot.logger.debug(line)
except Exception as e:
pass
message = f"```{''.join(logs)}```"
await success_response(ctx)
except Exception as e:
bot.logger.warning(e)
message = f"Error communicating with Project Zomboid Bilulu Server.\n{e}"
await fail_response(ctx)
if message:
await ctx.send(message)
return SendMessage
for cmd in zz_commands:
function_builder(cmd)
return bot