Skip to content

Commit

Permalink
5.4 Release touch ups and tweaks.
Browse files Browse the repository at this point in the history
Moved code portion out of backend_functions.server_log() and in to slime_bot.banlist().

Removed unnecessary if stop_at_checker is True statement in server_command().
Updated/added some docstrings.
Added help button on bot startup.
  • Loading branch information
0n1udra committed Apr 10, 2022
1 parent 15de68b commit 7b4a682
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 73 deletions.
47 changes: 20 additions & 27 deletions source/backend_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def lprint(arg1=None, arg2=None):

lprint("Server selected: " + slime_vars.server_selected[0])

# Parse/get data.
# ===== Parse/format/read data.
def format_args(args, return_no_reason=False):
"""
Formats passed in *args from Discord command functions.
Expand Down Expand Up @@ -89,24 +89,26 @@ def get_public_ip():
slime_vars.server_ip = server_ip
return server_ip

# Discord
# ===== Discord related functions.
def channel_set(channel):
"""Sets discord_channel global variable."""
global discord_channel
discord_channel = channel

async def channel_send(msg):
"""Send message to discord_channel."""

if discord_channel: await discord_channel.send(msg)

# ========== Server Commands: start, send command, read log, etc
async def server_command(command, stop_at_checker=True, skip_check=False, discord_msg=True):
async def server_command(command, skip_check=False, discord_msg=True):
"""
Sends command to Minecraft server. Depending on whether server is a subprocess or in Tmux session or using RCON.
Sends command to server, then reads from latest.log file for output.
If using RCON, will only return RCON returned data, can't read from server log.
Args:
command str: Command to send.
stop_at_checker bool(True): Only returns log output under sent status_checker
skip_check bool(False): Skips server_active boolean check.
discord_msg bool(True): Send message indicating if server is inactive.
Expand Down Expand Up @@ -156,10 +158,8 @@ async def inactive_msg():
return False

await asyncio.sleep(slime_vars.command_buffer_time)
# Checks server log if command went through.
return_data = [server_log(command), None]
if stop_at_checker is True:
return_data[1] = random_number
# Returns log line that matches command.
return_data = [server_log(command), random_number]
return return_data

def server_log(match=None, file_path=None, lines=15, normal_read=False, log_mode=False, filter_mode=False, stopgap_str=None, return_reversed=False):
Expand All @@ -180,41 +180,33 @@ def server_log(match=None, file_path=None, lines=15, normal_read=False, log_mode
log_data (str): Returns found match log line or multiple lines from log.
"""

# Parameter setups.
if match is None: match = 'placeholder_match'
match = match.lower()
if stopgap_str is None: stopgap_str = 'placeholder_stopgap'

if filter_mode is True: lines = slime_vars.log_lines_limit
# Defaults file to server log.
if file_path is None: file_path = f"{slime_vars.server_path}/logs/latest.log"
if not os.path.isfile(file_path): return False
#os.path.getsize(file_path) > 0:

if filter_mode is True: lines = slime_vars.log_lines_limit

log_data = ''

# Read log file top down.
if normal_read:
with open(file_path, 'r') as file:
for line in file:
if match in line: return line
else:
else: # Read log file bottom up, latest log outputs first.
with FileReadBackwards(file_path) as file:
for i in range(lines):
line = file.readline()
if not line.strip(): continue # Skip blank/newlines.

# Minecraft log data parsing.
if 'banlist' in match: # How ugly :(
if 'was banned by' in line: # finds log lines that shows banned players.
log_data += line
elif ']: There are' in line: # finds the end so it doesn't return everything from log other then banned users.
log_data += line
break

elif log_mode: log_data += line

elif log_mode:
log_data += line
elif match in line.lower():
log_data += line
if not filter_mode: break

if stopgap_str.lower() in line.lower(): break

if log_data:
Expand Down Expand Up @@ -261,7 +253,7 @@ async def server_status(discord_msg=False):
lprint("Checking Minecraft server status...")

# server_command() will send random number, server is online if match is found in log.
response = await server_command(' ', skip_check=True, stop_at_checker=True, discord_msg=discord_msg)
response = await server_command(' ', skip_check=True, discord_msg=discord_msg)
if response:
if discord_msg: await channel_send("**Server ACTIVE** :green_circle:")
lprint("Server Status: Active")
Expand Down Expand Up @@ -335,7 +327,6 @@ def ping_server():
Returns:
dict: Dictionary containing 'version', and 'description' (motd).
"""

global server_active
Expand Down Expand Up @@ -427,6 +418,8 @@ def get_data(find, url=''): return json.loads(requests.get(f'{base_url}{url}').t
return False

async def get_player_list():
"""Extracts wanted data from output of 'list' command."""

response = await server_command("list")
if not response: return False

Expand All @@ -446,7 +439,7 @@ async def get_player_list():
else:
player_names = [f"{i.strip()[:-4]}\n" if slime_vars.use_rcon else f"{i.strip()}" for i in (log_data[-1]).split(',')]
# Outputs player names in special discord format. If using RCON, need to clip off 4 trailing unreadable characters.
#player_names_discord = [f"`{i.strip()[:-4]}`\n" if use_rcon else f"`{i.strip()}`\n" for i in (log_data[-1]).split(',')]
# player_names_discord = [f"`{i.strip()[:-4]}`\n" if use_rcon else f"`{i.strip()}`\n" for i in (log_data[-1]).split(',')]

return player_names, text

Expand Down
Loading

0 comments on commit 7b4a682

Please sign in to comment.