Config refactor
Refactor the config away from environment variables to one YAML file that is relatively easy to edit as a Docker container volume.main
parent
95bb6fd88c
commit
947b454faf
|
@ -25,7 +25,7 @@ def rewriter(file_name):
|
|||
@rewriter('eula.txt')
|
||||
def confirm_eula(line : str, line_no : int, data):
|
||||
"""Confirm the Minecraft EULA"""
|
||||
if os.getenv('EULA') is None or os.getenv('EULA').lower() != 'true':
|
||||
if not config.EULA:
|
||||
return line
|
||||
else:
|
||||
return line.replace('eula=false', 'eula=true')
|
||||
|
@ -36,9 +36,13 @@ def fill_in_server_settings(line : str, line_no : int, data):
|
|||
if line.strip().startswith('#'):
|
||||
return line
|
||||
|
||||
value = line.split('=')[0]
|
||||
key = line.split('=')[0]
|
||||
|
||||
if value in config.SERVER_SETTINGS:
|
||||
return value + '=' + str(config.SERVER_SETTINGS[value]) + '\n'
|
||||
server_settings = config.at(['minecraft']) or {}
|
||||
|
||||
try:
|
||||
value = server_settings[key]
|
||||
except IndexError:
|
||||
return line
|
||||
else:
|
||||
return line
|
||||
return key + '=' + str(value) + '\n'
|
||||
|
|
41
config.py
41
config.py
|
@ -1,9 +1,41 @@
|
|||
import os
|
||||
import yaml
|
||||
from typing import Any, List, Optional
|
||||
|
||||
with open('config.yaml', 'r') as open_file:
|
||||
SETTINGS = yaml.load(open_file)
|
||||
|
||||
def at(keys : List[str]) -> Optional[Any]:
|
||||
"""
|
||||
Potentially get a value. If it doesn't exist, return None.
|
||||
"""
|
||||
return at_value(keys, SETTINGS)
|
||||
|
||||
def at_value(keys : List[str], value : Any) -> Optional[Any]:
|
||||
try:
|
||||
head, tail = keys[0], keys[1:]
|
||||
except IndexError:
|
||||
return value
|
||||
else:
|
||||
try:
|
||||
new_value = value[head]
|
||||
except TypeError:
|
||||
return None
|
||||
except KeyError:
|
||||
return None
|
||||
else:
|
||||
return at_value(tail, new_value)
|
||||
|
||||
# EULA
|
||||
EULA = at(['minecraft', 'eula']) or False
|
||||
|
||||
# Minecraft bridge credentials
|
||||
MATRIX_HOMESERVER = os.getenv('MATRIX_HOMESERVER') or "https://homeserv.er"
|
||||
MATRIX_USERNAME = os.getenv('MATRIX_USERNAME') or "@bridge_username:homeserv.er"
|
||||
MATRIX_PASSWORD = os.getenv('MATRIX_PASSWORD') or "bridge_password"
|
||||
MATRIX_HOMESERVER = at(['matrix', 'homeserver']) or "https://matrix.example.org/"
|
||||
MATRIX_USERNAME = at(['matrix', 'username']) or "@alice:example.org"
|
||||
MATRIX_PASSWORD = at(['matrix', 'password']) or "bridge_password"
|
||||
|
||||
# Matrix bridge room
|
||||
MATRIX_ROOM = at(['matrix', 'room_id']) or "!channel_id:example.org"
|
||||
|
||||
SERVER_IP = os.getenv('SERVER_ADDRESS') or 'unknown ip'
|
||||
|
||||
|
@ -16,9 +48,6 @@ MC_ADMINS = [
|
|||
if os.getenv('MATRIX_ADMINS') is not None:
|
||||
MC_ADMINS = os.getenv('MATRIX_ADMINS').split(',')
|
||||
|
||||
# Matrix channel that the bot should talk to
|
||||
MC_CHANNEL = os.getenv('MC_CHANNEL') or "!channel_id:homeserv.er"
|
||||
|
||||
make_bool = lambda os_value, default_value : default_value if not os_value else (
|
||||
False if os_value.lower() == 'false' else True
|
||||
)
|
||||
|
|
|
@ -10,6 +10,9 @@ matrix:
|
|||
# Matrix room
|
||||
room_id: "!channel_id:example.org"
|
||||
|
||||
# IP address or domain where users can join the server
|
||||
server_address: unknown ip
|
||||
|
||||
# List of Matrix users that can send commands to the bridge.
|
||||
# When a message starts with a slash, (/) the bridge will interpret it as a
|
||||
# Minecraft command and will put that as a command into the console.
|
||||
|
|
2
main.py
2
main.py
|
@ -13,7 +13,7 @@ STARTUP_TIME = time.time()
|
|||
client = AsyncClient(config.MATRIX_HOMESERVER, config.MATRIX_USERNAME)
|
||||
|
||||
async def message_callback(room: MatrixRoom, event: RoomMessageText) -> None:
|
||||
if room.machine_name != config.MC_CHANNEL:
|
||||
if room.machine_name != config.MATRIX_ROOM:
|
||||
return
|
||||
if event.sender == client.user_id:
|
||||
return
|
||||
|
|
Loading…
Reference in New Issue