From 16664179ead2d3ddeca998e686359079bc05ed7b Mon Sep 17 00:00:00 2001 From: Armatik <57626821+armatik@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:11:48 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=BA=D0=B0=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=82=D0=BC=D0=B0=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0.=20-=20=D0=9F=D0=B5=D1=80=D0=B5=D1=85?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=BD=D0=B0=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D1=83=D1=8E=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC?= =?UTF-8?q?=D1=83.=20-=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20DataBase.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 + .idea/modules.xml | 8 ++ .idea/ocab.iml | 10 ++ .idea/vcs.xml | 6 + src/config.json | 0 src/core.py | 1 + src/modules/DataBase.py | 128 ++++++++++++++++++ src/modules/Roles.py | 0 10 files changed, 174 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/ocab.iml create mode 100644 .idea/vcs.xml create mode 100644 src/config.json create mode 100644 src/core.py create mode 100644 src/modules/DataBase.py create mode 100644 src/modules/Roles.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..81884d3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..933052a --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/ocab.iml b/.idea/ocab.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/ocab.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/config.json b/src/config.json new file mode 100644 index 0000000..e69de29 diff --git a/src/core.py b/src/core.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/core.py @@ -0,0 +1 @@ + diff --git a/src/modules/DataBase.py b/src/modules/DataBase.py new file mode 100644 index 0000000..2d092d4 --- /dev/null +++ b/src/modules/DataBase.py @@ -0,0 +1,128 @@ +import os +import sqlite3 + + +class DataBase: + def __init__(self): + self.bot_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd()))) + self.db = sqlite3.connect(os.path.join(self.bot_dir, 'DataBase/OCAB_DB.db')) + self.cursor = self.db.cursor() + self.check_db() + + # Проверки наличия таблиц в базе данных + def check_db(self): + self.check_chatdb() + self.check_userdb() + self.check_messagedb() + + def check_chatdb(self): + self.cursor.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='chat_list'""") + if self.cursor.fetchone() is None: + self.create_chatdb() + + def check_userdb(self): + self.cursor.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='user_list'""") + if self.cursor.fetchone() is None: + self.create_userdb() + + def check_messagedb(self): + self.cursor.execute("""SELECT name FROM sqlite_master WHERE type='table' AND name='message_list'""") + if self.cursor.fetchone() is None: + self.create_messagedb() + + # Создание таблиц в базе данных + def create_chatdb(self): + self.cursor.execute("""CREATE TABLE IF NOT EXISTS chat_list ( + chat_id INTEGER PRIMARY KEY, + chat_role INTEGER NOT NULL, + chat_stats INTEGER NOT NULL, + chat_federation INTEGER, + )""") + self.db.commit() + + def create_userdb(self): + self.cursor.execute("""CREATE TABLE IF NOT EXISTS user_list ( + user_id INTEGER PRIMARY KEY, + user_name TEXT NOT NULL, + user_tag TEXT, + user_role INTEGER, + user_stats INTEGER + user_rep INTEGER + )""") + self.db.commit() + + def create_messagedb(self): + self.cursor.execute("""CREATE TABLE IF NOT EXISTS message_list ( + message_id INTEGER PRIMARY KEY, + message_text TEXT NOT NULL, + message_id_sender INTEGER NOT NULL, + answer_to_id INTEGER + )""") + self.db.commit() + + async def add_chat(self, chat_id, chat_role, chat_stats=0, chat_federation=0): + self.cursor.execute("""INSERT INTO chat_list VALUES (?, ?, ?, ?)""", + (chat_id, chat_role, chat_stats, chat_federation)) + + async def add_user(self, user_id, user_name, user_tag=None, user_role=0, user_stats=0, user_rep=0): + self.cursor.execute("""INSERT INTO user_list VALUES (?, ?, ?, ?, ?, ?)""", + (user_id, user_name, user_tag, user_role, user_stats, user_rep)) + self.db.commit() + + async def add_message(self, message_id, message_text, message_sender, answer_id): + self.cursor.execute("""INSERT INTO message_list VALUES (?, ?, ?, ?)""", + (message_id, message_text, message_sender, answer_id)) + self.db.commit() + + async def get_chat(self, chat_id): + self.cursor.execute("""SELECT * FROM chat_list WHERE chat_id=?""", (chat_id,)) + return self.cursor.fetchone() + + async def get_user(self, user_id): + self.cursor.execute("""SELECT * FROM user_list WHERE user_id=?""", (user_id,)) + return self.cursor.fetchone() + + async def get_message(self, message_id): + self.cursor.execute("""SELECT * FROM message_list WHERE message_id=?""", (message_id,)) + return self.cursor.fetchone() + + async def change_user_name(self, user_id, new_user_name): + self.cursor.execute("""UPDATE user_list SET user_name=? WHERE user_id=?""", (new_user_name, user_id)) + self.db.commit() + + async def change_user_role(self, user_id, new_user_role): + self.cursor.execute("""UPDATE user_list SET user_role=? WHERE user_id=?""", (new_user_role, user_id)) + self.db.commit() + + async def change_user_stats(self, user_id, new_user_stats): + self.cursor.execute("""UPDATE user_list SET user_stats=? WHERE user_id=?""", (new_user_stats, user_id)) + self.db.commit() + + async def change_user_rep(self, user_id, new_user_rep): + self.cursor.execute("""UPDATE user_list SET user_rep=? WHERE user_id=?""", (new_user_rep, user_id)) + self.db.commit() + + async def change_chat_role(self, chat_id, new_chat_role): + self.cursor.execute("""UPDATE chat_list SET chat_role=? WHERE chat_id=?""", (new_chat_role, chat_id)) + self.db.commit() + + async def change_chat_stats(self, chat_id, new_chat_stats): + self.cursor.execute("""UPDATE chat_list SET chat_stats=? WHERE chat_id=?""", (new_chat_stats, chat_id)) + self.db.commit() + + async def change_chat_federation(self, chat_id, new_chat_federation): + self.cursor.execute("""UPDATE chat_list SET chat_federation=? WHERE chat_id=?""", + (new_chat_federation, chat_id)) + self.db.commit() + + async def update_user_stats(self): + self.cursor.execute("""UPDATE user_list SET user_stats=user_stats+1""") + self.db.commit() + + async def update_user_rep(self): + self.cursor.execute("""UPDATE user_list SET user_rep=user_rep+1""") + self.db.commit() + + async def update_chat_stats(self): + self.cursor.execute("""UPDATE chat_list SET chat_stats=chat_stats+1""") + self.db.commit() diff --git a/src/modules/Roles.py b/src/modules/Roles.py new file mode 100644 index 0000000..e69de29