77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
'''Essentials for the bot to function'''
|
|
import configparser
|
|
import json
|
|
from os import environ, path
|
|
|
|
|
|
class ConfigError(Exception):
|
|
'''Could not find config file'''
|
|
|
|
|
|
def get_config_file() -> str:
|
|
'''Gets the path to the config file in the current environment'''
|
|
env: str | None = environ.get('KEMOVERSE_ENV')
|
|
if not env:
|
|
raise ConfigError('Error: KEMOVERSE_ENV is unset')
|
|
if not (env in ['prod', 'dev']):
|
|
raise ConfigError(f'Error: Invalid environment: {env}')
|
|
|
|
config_path: str = f'config_{env}.ini'
|
|
|
|
if not path.isfile(config_path):
|
|
raise ConfigError(f'Could not find {config_path}')
|
|
return config_path
|
|
|
|
|
|
def get_rarity_to_weight(
|
|
config_section: configparser.SectionProxy) -> dict[int, float]:
|
|
"""Parses Rarity_X keys from config and returns a {rarity: weight} dict."""
|
|
rarity_weights = {}
|
|
for key, value in config_section.items():
|
|
if key.startswith("rarity_"):
|
|
rarity = int(key.removeprefix("rarity_"))
|
|
rarity_weights[rarity] = float(value)
|
|
return rarity_weights
|
|
|
|
|
|
config = configparser.ConfigParser()
|
|
config.read(get_config_file())
|
|
|
|
# Username for the bot
|
|
USER = config['credentials']['User'].lower()
|
|
# API key for the bot
|
|
KEY = config['credentials']['Token']
|
|
# Bot's Misskey/Pleroma instance URL
|
|
INSTANCE = config['credentials']['Instance'].lower()
|
|
|
|
# Instance type validation
|
|
if 'InstanceType' not in config['application']:
|
|
raise ValueError("InstanceType must be specified in config.ini")
|
|
|
|
instance_type = config['application']['InstanceType'].lower()
|
|
if instance_type not in ('misskey', 'pleroma'):
|
|
raise ValueError("InstanceType must be either 'misskey' or 'pleroma'")
|
|
|
|
INSTANCE_TYPE = instance_type
|
|
|
|
# Web server port
|
|
WEB_PORT = config['application'].getint('WebPort', 5000)
|
|
|
|
# Trusted instances
|
|
trusted_instances_str = config['application'].get('TrustedInstances', '')
|
|
TRUSTED_INSTANCES = [instance.strip() for instance in trusted_instances_str.split(',') if instance.strip()]
|
|
|
|
# Fedi handles in the traditional 'user@domain.tld' style, allows these users
|
|
# to use extra admin exclusive commands with the bot
|
|
ADMINS = json.loads(config['application']['DefaultAdmins'])
|
|
# SQLite Database location
|
|
DB_PATH = config['application']['DatabaseLocation']
|
|
# Whether to enable the instance whitelist
|
|
USE_WHITELIST = config['application'].getboolean('UseWhitelist', False)
|
|
|
|
NOTIFICATION_POLL_INTERVAL = int(config['notification']['PollInterval'])
|
|
NOTIFICATION_BATCH_SIZE = int(config['notification']['BatchSize'])
|
|
|
|
GACHA_ROLL_INTERVAL = int(config['gacha']['RollInterval'])
|
|
|
|
RARITY_TO_WEIGHT = get_rarity_to_weight(config['gacha'])
|