finish readme and clean up json examples

This commit is contained in:
Nayan
2025-08-27 04:32:58 +00:00
parent 45e941bac3
commit 61a06a8b01
6 changed files with 36 additions and 59 deletions

View File

@@ -2,9 +2,17 @@
A simple discord bot written in python for managing multiple minecraft servers through the same discord server. A simple discord bot written in python for managing multiple minecraft servers through the same discord server.
The bot can start a server and stop a server. It can also force stop a server if the server hangs. The list of servers the bot can manage is stored in a json file. See servers.json.example for an example of the json file. I want to support passing the minecraft chat through to discord, but I'm running into parsing errors with python's regex library. Passing discord chat through to minecraft does work. ## Features
### Installation - Manages an arbitrary number of minecraft servers (and makes sure only one server is active at a time)
- Enables players to list, start, and stop servers from the discord server
- Can force stop a server if it hangs
- Listens to both discord and minecraft chat and forwards them back and forth so that the users in game and on discord can chat together
- Enables sending commands to the currently running server via discord
- Uses a simple json file to store the servers the bot can manage, including the path to the server folder, the server name, and the command to start the server (for instance a script file or directly calling a jar)
## Installation
1. Clone the repository 1. Clone the repository
2. Install the required packages with `pip install -r requirements.txt` 2. Install the required packages with `pip install -r requirements.txt`
@@ -15,7 +23,7 @@ The bot can start a server and stop a server. It can also force stop a server if
7. Run the bot with `screen -mS startbot python3 bot.py` 7. Run the bot with `screen -mS startbot python3 bot.py`
### Usage ## Usage
Commands: Commands:
- !help - Displays this help - !help - Displays this help
@@ -30,14 +38,34 @@ Commands:
**NOTE:** **NOTE:**
The bot allows running arbitrary scripts defined in the json file, and arbitrary commands if a discord user has the admin role and one of the startserver scripts opens a shell. This program is a proof of concept personal tool, and is not designed for robustness or security. Use it at your own risk, and make sure you understand the security implications if you decide to use it for more than basic testing. I do not accept any liablilty for any damage caused by the use of this program. The bot allows running arbitrary scripts defined in the json file, and arbitrary commands if a discord user has the admin role and one of the startserver scripts opens a shell. This program is a proof of concept personal tool, and is not designed for robustness or security. Use it at your own risk, and make sure you understand the security implications if you decide to use it for more than basic testing. I do not accept any liablilty for any damage caused by the use of this program.
## Configuration
### TODO #### The bot requires a .env file (or equivalent) to access the discord bot token.
`BOT_TOKEN=<token>`
#### The config.json file contains the following keys:
- chat_channel: The discord channel to send messages to
- command_channel: The discord channel to send commands to
- command_prefix: The prefix for commands
- regex: A list of regular expressions to match server output
- match: The regular expression to match
- fstring: The format string to use for the message ({groups} will be replaced with the groups from the regex match)
#### The servers.json file contains the following keys for each server:
- path: The path to the server folder
- servername: The name of the server (used to start the server via Discord)
- command: The command to start the server
## TODO
- [x] Add currently running server variable - [x] Add currently running server variable
- [x] Add running server check to prevent multiple servers running - [x] Add running server check to prevent multiple servers running
- [x] Add list command to see currently running servers - [x] Add list command to see currently running servers
- [x] Add server stop command - [x] Add server stop command
- [x] Add server command command - [x] Add server command command
- [ ] Add server message passthrough - [x] Add server message passthrough
- [x] Add help command - [x] Add help command
- [x] Add discord message passthrough - [x] Add discord message passthrough

4
bot.py
View File

@@ -27,7 +27,7 @@ with open('config.json', 'r') as f:
command_prefix = config['config']['command_prefix'] command_prefix = config['config']['command_prefix']
for regex in config['config']['regex']: for regex in config['config']['regex']:
regex_list.append(re.compile(regex['match'])) regex_list.append(re.compile(regex['match']))
fprint_list.append(regex['capture']) fprint_list.append(regex['fstring'])
intents = discord.Intents.default() intents = discord.Intents.default()
intents.message_content = True intents.message_content = True
@@ -452,7 +452,7 @@ async def reloadregex(ctx):
config = json.load(f) config = json.load(f)
for regex in config['config']['regex']: for regex in config['config']['regex']:
regex_list.append(re.compile(regex['match'])) regex_list.append(re.compile(regex['match']))
fprint_list.append(regex['capture']) fprint_list.append(regex['fstring'])
# Reload the processline module # Reload the processline module
global processline global processline

View File

@@ -5,9 +5,8 @@
"command_prefix": "!", "command_prefix": "!",
"regex": [ "regex": [
{ {
"type": "groups",
"match": ".*\\]: <(.*)> (.*)", "match": ".*\\]: <(.*)> (.*)",
"capture": "**[{groups[0]}]**: {groups[1]}" "fstring": "**[{groups[0]}]**: {groups[1]}"
} }
] ]
} }

View File

@@ -1,27 +0,0 @@
# Load data from testdata.txt
# Read each line and match it against the regex
# Print the match
import re
import json
REGEXS = []
CAPTURES = []
with open('config.json.example', 'r') as f:
config = json.load(f)
for regex in config['config']['regex']:
REGEXS.append(regex['match'])
CAPTURES.append(regex['capture'])
with open('testdata.txt', 'r') as f:
for line in f:
for i in range(len(REGEXS)):
REGEX = REGEXS[i]
CAPTURE = CAPTURES[i]
match = re.match(REGEX, line)
if match:
# print(match.group(1), match.group(2), match.group(3))
groups = match.groups()
ouput = CAPTURE.format(groups=groups)
print(ouput)

View File

@@ -1,13 +0,0 @@
[26Aug2025 00:10:14.772] [Server thread/INFO] [patchouli/]: Sending reload packet to clients
[26Aug2025 00:10:14.776] [Server thread/WARN] [ModernFix/]: Dedicated server took 125.256 seconds to load
[26Aug2025 00:10:14.867] [Server thread/WARN] [minecolonies/]: Starting Compat Discovery
[26Aug2025 00:10:15.307] [Server thread/INFO] [minecolonies/]: Finished discovering Ores 387 359
[26Aug2025 00:10:15.307] [Server thread/INFO] [minecolonies/]: Finished discovering saplings 312
[26Aug2025 00:25:15.983] [Server thread/INFO] [net.minecraft.server.MinecraftServer/]: <61616> hello
[26Aug2025 00:25:30.709] [Server thread/INFO] [net.minecraft.server.MinecraftServer/]: [Server] hello back
[26Aug2025 00:25:47.209] [Server thread/INFO] [net.minecraft.server.MinecraftServer/]: You whisper to 61616: nice
[26Aug2025 00:27:05.873] [Server thread/INFO] [net.minecraft.server.MinecraftServer/]: [61616: Set the time to 18206]
[26Aug2025 00:30:20.632] [Server thread/INFO] [AllTheLeaks/]: Listing events...
[26Aug2025 00:30:20.632] [Server thread/INFO] [AllTheLeaks/]: Listing memory leaks so far...
[26Aug2025 00:30:20.632] [Server thread/INFO] [AllTheLeaks/]: B: 2025MB / C: 2539MB / Diff: +514MB
\[(.*?)\].*INFO.*\]: <(.*)> (.*)

View File

@@ -1,10 +0,0 @@
#!/bin/bash
while true; do
read -n 1 -p "" input
if [[ $input == "q" ]]; then
break
fi
echo "hello"
sleep 3
done