mirror of
https://github.com/opus-tango/minecraft-server-discord-bot.git
synced 2026-03-20 03:55:20 +00:00
clean up unused lines, comments, and output strings
This commit is contained in:
45
bot.py
45
bot.py
@@ -39,13 +39,7 @@ active_server_pipe_task = None # To hold the asyncio task that reads from the pi
|
|||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# --- Named Pipe Functions ---
|
# --- Named Pipe Functions ---
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def _get_pipe_path(servername):
|
def _get_pipe_path(servername):
|
||||||
@@ -78,8 +72,6 @@ async def _delete_named_pipe(servername):
|
|||||||
except OSError as e:
|
except OSError as e:
|
||||||
print(f"Error deleting named pipe {pipe_path}: {e}")
|
print(f"Error deleting named pipe {pipe_path}: {e}")
|
||||||
|
|
||||||
# --- Pipe Reading and Parsing Task ---
|
|
||||||
|
|
||||||
async def _read_from_pipe(servername, channel_id, bot_instance):
|
async def _read_from_pipe(servername, channel_id, bot_instance):
|
||||||
"""
|
"""
|
||||||
Asynchronously reads from the named pipe, parses Minecraft output,
|
Asynchronously reads from the named pipe, parses Minecraft output,
|
||||||
@@ -199,7 +191,7 @@ async def _stop_server_internal(ctx, servername, method):
|
|||||||
await ctx.send(f"No server '{servername}' is currently active, or a different server is running.")
|
await ctx.send(f"No server '{servername}' is currently active, or a different server is running.")
|
||||||
return
|
return
|
||||||
|
|
||||||
await ctx.send(f"Attempting to {method} server '{servername}'...")
|
await ctx.send(f"Attempting to {method} server '{servername}'. Please wait to send any other commands...")
|
||||||
|
|
||||||
if method == "stop":
|
if method == "stop":
|
||||||
command_to_send = 'stop\n'
|
command_to_send = 'stop\n'
|
||||||
@@ -249,16 +241,12 @@ async def _stop_server_internal(ctx, servername, method):
|
|||||||
active_server = None # Clear active server state
|
active_server = None # Clear active server state
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Handle events (messages mostly)
|
Handle events (messages mostly)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
@@ -269,15 +257,19 @@ async def on_message(message):
|
|||||||
print(f'Message from {message.author}: {message.content}')
|
print(f'Message from {message.author}: {message.content}')
|
||||||
if message.author == bot.user:
|
if message.author == bot.user:
|
||||||
return
|
return
|
||||||
|
# If the message is in the chat channel and not a command, send it to Minecraft
|
||||||
if message.channel.name == chat_channel and not message.content.startswith(command_prefix):
|
if message.channel.name == chat_channel and not message.content.startswith(command_prefix):
|
||||||
if not active_server:
|
if not active_server:
|
||||||
await message.channel.send("No server is currently active.")
|
await message.channel.send("No server is currently active.")
|
||||||
return
|
return
|
||||||
await send_message(message)
|
await send_message(message)
|
||||||
|
|
||||||
|
# If the message is in the command channel and is a command, process it as a command
|
||||||
if message.channel.name == command_channel and message.content.startswith(command_prefix):
|
if message.channel.name == command_channel and message.content.startswith(command_prefix):
|
||||||
await bot.process_commands(message)
|
await bot.process_commands(message)
|
||||||
|
|
||||||
async def send_message(message):
|
async def send_message(message):
|
||||||
|
''' Send a message to Minecraft by sending it to the screen session. '''
|
||||||
global active_server
|
global active_server
|
||||||
if not active_server:
|
if not active_server:
|
||||||
await message.channel.send("No server is currently active.")
|
await message.channel.send("No server is currently active.")
|
||||||
@@ -290,16 +282,9 @@ async def send_message(message):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Handle commands
|
Handle commands
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def startserver(ctx, *args):
|
async def startserver(ctx, *args):
|
||||||
@@ -355,8 +340,6 @@ async def startserver(ctx, *args):
|
|||||||
f"screen -dmS {servername} bash -c \"{server['startcommand']} | tee {pipe_path}\""
|
f"screen -dmS {servername} bash -c \"{server['startcommand']} | tee {pipe_path}\""
|
||||||
)
|
)
|
||||||
|
|
||||||
#command = f"cd {path} && screen -dmS {servername} {server['startcommand']}"
|
|
||||||
#print(f"Running command: {command}")
|
|
||||||
print(f"Running screen command: {screen_cmd}")
|
print(f"Running screen command: {screen_cmd}")
|
||||||
try:
|
try:
|
||||||
subprocess.Popen(screen_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
subprocess.Popen(screen_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
|
||||||
@@ -395,7 +378,7 @@ async def stopserver(ctx):
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def command(ctx, *args):
|
async def command(ctx, *args):
|
||||||
''' [command] : Send a command to the server '''
|
''' [command] : Send a command to the server (admin only) '''
|
||||||
global active_server
|
global active_server
|
||||||
# Check if the user is an admin
|
# Check if the user is an admin
|
||||||
if not ctx.author.guild_permissions.administrator:
|
if not ctx.author.guild_permissions.administrator:
|
||||||
@@ -446,7 +429,7 @@ async def list(ctx):
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def forcestopserver(ctx):
|
async def forcestopserver(ctx):
|
||||||
''' Force stop the server by sending keyboard interrupt '''
|
''' Force stop the server by sending keyboard interrupt (admin only) '''
|
||||||
global active_server
|
global active_server
|
||||||
if not ctx.author.guild_permissions.administrator:
|
if not ctx.author.guild_permissions.administrator:
|
||||||
await ctx.send("You don't have permission to run this command.")
|
await ctx.send("You don't have permission to run this command.")
|
||||||
@@ -458,7 +441,7 @@ async def forcestopserver(ctx):
|
|||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def reloadregex(ctx):
|
async def reloadregex(ctx):
|
||||||
''' Reload the regex for the server '''
|
''' Reload the regex for the server output (admin only) '''
|
||||||
global regex_list, fprint_list
|
global regex_list, fprint_list
|
||||||
if not ctx.author.guild_permissions.administrator:
|
if not ctx.author.guild_permissions.administrator:
|
||||||
await ctx.send("You don't have permission to run this command.")
|
await ctx.send("You don't have permission to run this command.")
|
||||||
@@ -475,7 +458,7 @@ async def reloadregex(ctx):
|
|||||||
global processline
|
global processline
|
||||||
processline = reload(processline)
|
processline = reload(processline)
|
||||||
testresults = processline.testreload()
|
testresults = processline.testreload()
|
||||||
await ctx.send("Regex reloaded, test result: " + testresults)
|
await ctx.send("Regex reloaded, test function returned: " + testresults)
|
||||||
|
|
||||||
@bot.command()
|
@bot.command()
|
||||||
async def ping(ctx):
|
async def ping(ctx):
|
||||||
|
|||||||
@@ -3,11 +3,10 @@ import re
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
async def processline(line, regex_list, fprint_list, bot_instance, channel):
|
async def processline(line, regex_list, fprint_list, bot_instance, channel):
|
||||||
|
''' Process a line of output from the server and send it to Discord if it matches a regex. '''
|
||||||
for i in range(len(regex_list)):
|
for i in range(len(regex_list)):
|
||||||
# print(f"Processing line: {line}")
|
|
||||||
match = regex_list[i].match(line)
|
match = regex_list[i].match(line)
|
||||||
if match:
|
if match:
|
||||||
# print(f"Matched line: {line}")
|
|
||||||
groups = match.groups()
|
groups = match.groups()
|
||||||
discord_message = fprint_list[i].format(groups=groups)
|
discord_message = fprint_list[i].format(groups=groups)
|
||||||
# Limit message length for Discord if necessary
|
# Limit message length for Discord if necessary
|
||||||
@@ -17,8 +16,7 @@ async def processline(line, regex_list, fprint_list, bot_instance, channel):
|
|||||||
await bot_instance.loop.create_task(channel.send(discord_message))
|
await bot_instance.loop.create_task(channel.send(discord_message))
|
||||||
# else:
|
# else:
|
||||||
# # Optionally, you can log other server output to a debug channel or console
|
# # Optionally, you can log other server output to a debug channel or console
|
||||||
# if random.random() < 0.01:
|
|
||||||
# print(f"Non-chat output: {line}")
|
# print(f"Non-chat output: {line}")
|
||||||
|
|
||||||
def testreload():
|
def testreload():
|
||||||
return "some new string"
|
return "reloaded successfully"
|
||||||
Reference in New Issue
Block a user