fix config module ref
This commit is contained in:
parent
fdf21b3f5f
commit
2ef70801c7
3 changed files with 66 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
'''Essentials for the bot to function'''
|
'''Essentials for the bot to function'''
|
||||||
import configparser
|
import configparser
|
||||||
import json
|
import json
|
||||||
|
import re
|
||||||
from os import environ, path
|
from os import environ, path
|
||||||
|
|
||||||
|
|
||||||
|
@ -21,6 +22,50 @@ def get_config_file() -> str:
|
||||||
return config_path
|
return config_path
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_user(user_string: str) -> str:
|
||||||
|
"""
|
||||||
|
Normalizes a user string to the format @user@domain.tld where domain is lowercase and user is case-sensitive
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user_string: User string in various formats
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Normalized user string
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: If the user string is invalid or domain is malformed
|
||||||
|
"""
|
||||||
|
if not user_string or not user_string.strip():
|
||||||
|
raise ValueError("User string cannot be empty")
|
||||||
|
|
||||||
|
user_string = user_string.strip()
|
||||||
|
|
||||||
|
# Add leading @ if missing
|
||||||
|
if not user_string.startswith('@'):
|
||||||
|
user_string = '@' + user_string
|
||||||
|
|
||||||
|
# Split into user and domain parts
|
||||||
|
parts = user_string[1:].split('@', 1) # Remove leading @ and split
|
||||||
|
if len(parts) != 2:
|
||||||
|
raise ValueError(f"Invalid user format: {user_string}. Expected @user@domain.tld")
|
||||||
|
|
||||||
|
username, domain = parts
|
||||||
|
|
||||||
|
if not username:
|
||||||
|
raise ValueError("Username cannot be empty")
|
||||||
|
|
||||||
|
if not domain:
|
||||||
|
raise ValueError("Domain cannot be empty")
|
||||||
|
|
||||||
|
# Validate domain format (basic check for valid domain structure)
|
||||||
|
domain_pattern = r'^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$'
|
||||||
|
if not re.match(domain_pattern, domain):
|
||||||
|
raise ValueError(f"Invalid domain format: {domain}")
|
||||||
|
|
||||||
|
# Return normalized format: @user@domain.tld (domain lowercase, user case-sensitive)
|
||||||
|
return f"@{username}@{domain.lower()}"
|
||||||
|
|
||||||
|
|
||||||
def get_rarity_to_weight(
|
def get_rarity_to_weight(
|
||||||
config_section: configparser.SectionProxy) -> dict[int, float]:
|
config_section: configparser.SectionProxy) -> dict[int, float]:
|
||||||
"""Parses Rarity_X keys from config and returns a {rarity: weight} dict."""
|
"""Parses Rarity_X keys from config and returns a {rarity: weight} dict."""
|
||||||
|
@ -36,17 +81,24 @@ config = configparser.ConfigParser()
|
||||||
config.read(get_config_file())
|
config.read(get_config_file())
|
||||||
|
|
||||||
# Username for the bot
|
# Username for the bot
|
||||||
USER = config['credentials']['User'].lower()
|
if 'User' not in config['credentials'] or not config['credentials']['User'].strip():
|
||||||
|
raise ConfigError("User must be specified in config.ini under [credentials]")
|
||||||
|
|
||||||
|
USER = normalize_user(config['credentials']['User'])
|
||||||
# API key for the bot
|
# API key for the bot
|
||||||
KEY = config['credentials']['Token']
|
KEY = config['credentials']['Token']
|
||||||
# Bot's Misskey instance URL
|
# Bot's Misskey instance URL
|
||||||
INSTANCE = config['credentials']['Instance'].lower()
|
INSTANCE = config['credentials']['Instance'].lower()
|
||||||
|
|
||||||
|
# Web server port
|
||||||
|
WEB_PORT = config['application'].getint('WebPort', 5000)
|
||||||
|
BIND_ADDRESS = config['application'].get('BindAddress', '127.0.0.1')
|
||||||
|
|
||||||
# Fedi handles in the traditional 'user@domain.tld' style, allows these users
|
# Fedi handles in the traditional 'user@domain.tld' style, allows these users
|
||||||
# to use extra admin exclusive commands with the bot
|
# to use extra admin exclusive commands with the bot
|
||||||
ADMINS = json.loads(config['application']['DefaultAdmins'])
|
ADMINS = json.loads(config['application']['DefaultAdmins'])
|
||||||
# SQLite Database location
|
# SQLite Database location
|
||||||
DB_PATH = config['application']['DatabaseLocation']
|
DB_PATH = config['application'].get('DatabaseLocation', './gacha_game.db')
|
||||||
# Whether to enable the instance whitelist
|
# Whether to enable the instance whitelist
|
||||||
USE_WHITELIST = config['application']['UseWhitelist']
|
USE_WHITELIST = config['application']['UseWhitelist']
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,11 @@
|
||||||
DefaultAdmins = ["@localadmin", "@remoteadmin@example.tld"]
|
DefaultAdmins = ["@localadmin", "@remoteadmin@example.tld"]
|
||||||
; SQLite Database location
|
; SQLite Database location
|
||||||
DatabaseLocation = ./gacha_game.db
|
DatabaseLocation = ./gacha_game.db
|
||||||
|
; Web server port (default: 5000)
|
||||||
|
WebPort = 5000
|
||||||
|
; Web server bind address (default: 127.0.0.1, set to 0.0.0.0 to listen on all interfaces)
|
||||||
|
BindAddress = 127.0.0.1
|
||||||
|
|
||||||
; Whether to lmit access to the bot via an instance whitelist
|
; Whether to lmit access to the bot via an instance whitelist
|
||||||
; The whitelist can be adjusted via the application
|
; The whitelist can be adjusted via the application
|
||||||
UseWhitelist = False
|
UseWhitelist = False
|
||||||
|
|
|
@ -1,10 +1,15 @@
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# Add parent directory to Python path so we can import from bot/
|
||||||
|
sys.path.append(str(Path(__file__).parent.parent))
|
||||||
|
|
||||||
|
from bot.config import WEB_PORT, BIND_ADDRESS, DB_PATH
|
||||||
from flask import Flask, render_template, abort
|
from flask import Flask, render_template, abort
|
||||||
from werkzeug.exceptions import HTTPException
|
from werkzeug.exceptions import HTTPException
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
DB_PATH = "./gacha_game.db" # Adjust path if needed
|
|
||||||
|
|
||||||
def get_db_connection():
|
def get_db_connection():
|
||||||
conn = sqlite3.connect(DB_PATH)
|
conn = sqlite3.connect(DB_PATH)
|
||||||
|
@ -68,4 +73,4 @@ def submit_character():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5000, debug=True)
|
app.run(host=BIND_ADDRESS, port=WEB_PORT, debug=True)
|
||||||
|
|
Loading…
Add table
Reference in a new issue