add dynamic loading of regex for testing

This commit is contained in:
Nayan
2025-08-26 00:02:28 -04:00
parent 727caef3dc
commit f01e7ad6d9
2 changed files with 30 additions and 5 deletions

24
bot.py
View File

@@ -12,6 +12,7 @@ load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
BASE_PATH = os.getenv('BASE_PATH', './')
FILE_REGEX = os.getenv('FILE_REGEX', 'false')
chat_channel = None
command_channel = None
@@ -165,10 +166,27 @@ async def _read_from_pipe(servername, channel_id, bot_instance):
# print(f"Raw server output: {line}") # Debugging raw output
match = MINECRAFT_CHAT_REGEX.match(line)
regex = None
capture = None
if (FILE_REGEX):
try:
with open('config.json', 'r') as f:
config = json.load(f)
tempregex = config['config']['regex'][0]['match']
regex = re.compile(tempregex)
capture = config['config']['regex'][0]['capture']
except Exception as e:
print(f"Error in regex compilation for {servername}: {e}")
regex = MINECRAFT_CHAT_REGEX
capture = "**[{groups[0]}]**: {groups[2]}"
else:
regex = MINECRAFT_CHAT_REGEX
capture = "**[{groups[0]}]**: {groups[2]}"
match = regex.match(line)
if match:
timestamp, player_name, message_content = match.groups()
discord_message = f"**[{player_name}]**: {message_content}"
groups = match.groups()
discord_message = capture.format(groups=groups)
# Limit message length for Discord if necessary
if len(discord_message) > 2000:
discord_message = discord_message[:1997] + "..."

View File

@@ -2,6 +2,13 @@
"config": {
"chat_channel": "testchatchannel",
"command_channel": "testchatchannel",
"command_prefix": "!"
"command_prefix": "!",
"regex": [
{
"type": "groups",
"match": "\\[(.*?)\\].*INFO.*\\]: <(.*)> (.*)",
"capture": "**[{groups[0]}]**: {groups[2]}"
}
]
}
}