Переработка алгоритма работы бота.

- Переход на модульную систему.
- Добавлен модуль DataBase.py
This commit is contained in:
Armatik 2023-10-27 14:11:48 +03:00 committed by Armatik
parent cfb61bfc6a
commit 16664179ea
10 changed files with 174 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

7
.idea/misc.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.11" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (ocab)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ocab.iml" filepath="$PROJECT_DIR$/.idea/ocab.iml" />
</modules>
</component>
</project>

10
.idea/ocab.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

0
src/config.json Normal file
View File

1
src/core.py Normal file
View File

@ -0,0 +1 @@

128
src/modules/DataBase.py Normal file
View File

@ -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()

0
src/modules/Roles.py Normal file
View File