it's version 0.8 Добавлена возможность выключения хранения логов и баз данных
This commit is contained in:
+6
-1
@@ -1,6 +1,9 @@
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
DIR = "/Users/mac/myfirstprogramm/storage/message.db"
|
||||
from config import Config
|
||||
|
||||
DIR = Path(__file__).resolve().parent / "message.db"
|
||||
|
||||
if __name__ == "__main__":
|
||||
db = sqlite3.connect(DIR)
|
||||
@@ -33,4 +36,6 @@ if __name__ == "__main__":
|
||||
|
||||
|
||||
def get_db():
|
||||
if Config.DISABLE_STORAGE:
|
||||
raise RuntimeError("Хранение отключено (DISABLE_STORAGE=1)")
|
||||
return sqlite3.connect(DIR)
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
from .DB import get_db
|
||||
from config import Config
|
||||
|
||||
|
||||
def save_message(chat_id: int, message_id: int):
|
||||
if Config.DISABLE_STORAGE:
|
||||
return
|
||||
from .DB import get_db
|
||||
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("INSERT INTO message VALUES (?, ?)", (int(chat_id), int(message_id)))
|
||||
@@ -11,6 +15,10 @@ def save_message(chat_id: int, message_id: int):
|
||||
|
||||
|
||||
def load_messages():
|
||||
if Config.DISABLE_STORAGE:
|
||||
return []
|
||||
from .DB import get_db
|
||||
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("SELECT * FROM message")
|
||||
@@ -21,6 +29,10 @@ def load_messages():
|
||||
|
||||
|
||||
def clear_messages():
|
||||
if Config.DISABLE_STORAGE:
|
||||
return
|
||||
from .DB import get_db
|
||||
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("DELETE FROM message")
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
from .DB import get_db
|
||||
from config import Config
|
||||
|
||||
_DEFAULT_GROUP = "30тс"
|
||||
|
||||
|
||||
def save_user(user_id: int, group: str = _DEFAULT_GROUP):
|
||||
if Config.DISABLE_STORAGE:
|
||||
return
|
||||
from .DB import get_db
|
||||
|
||||
def save_user(user_id: int, group: str = "30тс"):
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("INSERT INTO users (user_id, user_group) VALUES (?, ?)", (user_id, group))
|
||||
@@ -8,19 +15,20 @@ def save_user(user_id: int, group: str = "30тс"):
|
||||
cur.close()
|
||||
db.close()
|
||||
|
||||
def set_group(user_id: int, group: str = "30тс"):
|
||||
|
||||
def set_group(user_id: int, group: str = _DEFAULT_GROUP):
|
||||
if Config.DISABLE_STORAGE:
|
||||
return
|
||||
from .DB import get_db
|
||||
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
|
||||
# проверяем, есть ли пользователь
|
||||
cur.execute("SELECT 1 FROM users WHERE user_id = ?", (user_id,))
|
||||
exists = cur.fetchone()
|
||||
|
||||
if exists:
|
||||
# если есть — обновляем группу
|
||||
cur.execute("UPDATE users SET user_group = ? WHERE user_id = ?", (group, user_id))
|
||||
else:
|
||||
# если нет — регистрируем нового пользователя
|
||||
cur.execute("INSERT INTO users (user_id, user_group) VALUES (?, ?)", (user_id, group))
|
||||
|
||||
db.commit()
|
||||
@@ -28,7 +36,11 @@ def set_group(user_id: int, group: str = "30тс"):
|
||||
db.close()
|
||||
|
||||
|
||||
def get_group(user_id: int, default: str = "30тс") -> str:
|
||||
def get_group(user_id: int, default: str = _DEFAULT_GROUP) -> str:
|
||||
if Config.DISABLE_STORAGE:
|
||||
return default
|
||||
from .DB import get_db
|
||||
|
||||
db = get_db()
|
||||
cur = db.cursor()
|
||||
cur.execute("SELECT user_group FROM users WHERE user_id = ?", (user_id,))
|
||||
@@ -36,7 +48,6 @@ def get_group(user_id: int, default: str = "30тс") -> str:
|
||||
if row:
|
||||
group = row[0]
|
||||
else:
|
||||
# если пользователя нет — регистрируем с дефолтной группой
|
||||
cur.execute("INSERT INTO users (user_id, user_group) VALUES (?, ?)", (user_id, default))
|
||||
db.commit()
|
||||
group = default
|
||||
|
||||
Reference in New Issue
Block a user