Update database schema to use AUTOINCREMENT for user IDs and enforce UNIQUE constraint on destination.

Improve error handling for topic and emoticon loading.

Update time command to use server time directly.

Adjust effective limit calculation to use DISPLAY_LIMIT.
This commit is contained in:
2025-11-22 23:53:25 -06:00
parent 1221d9a4f0
commit b54c8faccd
2 changed files with 33 additions and 24 deletions

View File

@@ -18,9 +18,9 @@ if not os.path.exists(DB_PATH):
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE users (
id INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY AUTOINCREMENT,
remote_identity TEXT,
dest TEXT,
dest TEXT UNIQUE NOT NULL,
display_name TEXT
);
""")
@@ -274,14 +274,16 @@ safe_username = (
# Функции темы чата
topic_file = os.path.join(os.path.dirname(__file__), "topic.json")
topic_data = {}
try:
with open(topic_file, "r") as tf:
topic_data = json.load(tf)
topic_text = topic_data.get("text", "Добро пожаловать в чат!")
topic_author = topic_data.get("user", "System")
except:
except (IOError, json.JSONDecodeError, KeyError):
topic_text = "Добро пожаловать в чат!"
topic_author = "System"
topic_data = {}
log_file = os.path.join(os.path.dirname(__file__), "chat_log.json")
@@ -549,7 +551,7 @@ elif cmd == "/time":
try:
import pytz, datetime
user_time = datetime.datetime.now(pytz.timezone("Europe/Rome")).strftime("%Y-%m-%d %H:%M:%S")
except:
except (ImportError, AttributeError, OSError):
user_time = "(Local time not available)"
time_text = f"Server time: {server_time} // User time (Naples): {user_time}"
log.append({"time": time.strftime("[%a,%H:%M]"), "user": "System", "text": time_text})
@@ -672,7 +674,6 @@ elif cmd == "/e":
emojis = [line.strip() for line in f if line.strip()]
if emojis and safe_username:
import random
chosen = random.choice(emojis)
# Treat emoji as a normal message
@@ -708,8 +709,6 @@ elif cmd.startswith("/c "):
user_message = message[3:].strip().replace("`", "") # Remove backticks to avoid formatting issues
if user_message and safe_username:
import random, json
def hex_brightness(hex_code):
r = int(hex_code[0], 16)
g = int(hex_code[1], 16)
@@ -1257,12 +1256,18 @@ def highlight_mentions_in_line(line, known_users):
return re.sub(r"@(\w+)", replacer, line)
######## $E ДЛЯ ЭМОТИКОНОВ ########
with open(EMO_DB, "r", encoding="utf-8") as f:
EMOTICONS = []
EMOTICONS = []
try:
with open(EMO_DB, "r", encoding="utf-8") as f:
for line in f:
EMOTICONS.extend(line.strip().split())
except (IOError, OSError):
EMOTICONS = [":)", ":(", ":D", ":P"]
# $e catching for emoticons in messages
def substitute_emoticons_in_line(line):
if not EMOTICONS:
return line
return re.sub(r"\$e", lambda _: random.choice(EMOTICONS), line)
######## УПОМИНАНИЯ ССЫЛОК ######

View File

@@ -18,9 +18,9 @@ if not os.path.exists(DB_PATH):
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE users (
id INTEGER PRIMARY KEY,
id INTEGER PRIMARY KEY AUTOINCREMENT,
remote_identity TEXT,
dest TEXT,
dest TEXT UNIQUE NOT NULL,
display_name TEXT
);
""")
@@ -274,14 +274,16 @@ safe_username = (
# Функции темы чата
topic_file = os.path.join(os.path.dirname(__file__), "topic.json")
topic_data = {}
try:
with open(topic_file, "r") as tf:
topic_data = json.load(tf)
topic_text = topic_data.get("text", "Добро пожаловать в чат!")
topic_author = topic_data.get("user", "System")
except:
except (IOError, json.JSONDecodeError, KeyError):
topic_text = "Добро пожаловать в чат!"
topic_author = "System"
topic_data = {}
log_file = os.path.join(os.path.dirname(__file__), "chat_log.json")
@@ -545,8 +547,7 @@ elif cmd == "/info":
############ КОМАНДА TIME ###############
elif cmd == "/time":
from datetime import datetime
server_time = datetime.utcnow().strftime("%A, %B %d, %Y at %H:%M:%S UTC")
server_time = time.strftime("%A, %B %d, %Y at %H:%M:%S UTC")
time_text = f"Текущее время сервера: {server_time}"
log.append({"time": time.strftime("[%a,%H:%M]"), "user": "System", "text": time_text})
@@ -668,7 +669,6 @@ elif cmd == "/e":
emojis = [line.strip() for line in f if line.strip()]
if emojis and safe_username:
import random
chosen = random.choice(emojis)
# Treat emoji as a normal message
@@ -704,8 +704,6 @@ elif cmd.startswith("/c "):
user_message = message[3:].strip().replace("`", "") # Remove backticks to avoid formatting issues
if user_message and safe_username:
import random, json
def hex_brightness(hex_code):
r = int(hex_code[0], 16)
g = int(hex_code[1], 16)
@@ -1205,7 +1203,7 @@ def calculate_effective_limit(log, max_lines, max_chars):
return max(effective_limit, 1), total_lines
effective_limit, total_lines = calculate_effective_limit(log, MAX_LINES, MAX_CHARS)
effective_limit, total_lines = calculate_effective_limit(log, DISPLAY_LIMIT, MAX_CHARS)
########## Динамическое преобразование времени сервера UTC в локальное время ##########
@@ -1253,12 +1251,18 @@ def highlight_mentions_in_line(line, known_users):
return re.sub(r"@(\w+)", replacer, line)
######## $E ДЛЯ ЭМОТИКОНОВ ########
with open(EMO_DB, "r", encoding="utf-8") as f:
EMOTICONS = []
EMOTICONS = []
try:
with open(EMO_DB, "r", encoding="utf-8") as f:
for line in f:
EMOTICONS.extend(line.strip().split())
except (IOError, OSError):
EMOTICONS = [":)", ":(", ":D", ":P"]
# Перехват $e для эмотиконов в сообщениях
def substitute_emoticons_in_line(line):
if not EMOTICONS:
return line
return re.sub(r"\$e", lambda _: random.choice(EMOTICONS), line)
######## УПОМИНАНИЯ ССЫЛОК ######