From 7f232eff50ec45f6f20ae03af9e652d12300f882 Mon Sep 17 00:00:00 2001 From: Armatik Date: Fri, 27 Oct 2023 16:30:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=BC=D0=BE?= =?UTF-8?q?=D0=B4=D1=83=D0=BB=D0=B8.=20-=20Config.py=20=D0=9C=D0=BE=D0=B4?= =?UTF-8?q?=D1=83=D0=BB=D1=8C=20=D0=B2=D0=B7=D0=B0=D0=B8=D0=BC=D0=BE=D0=B4?= =?UTF-8?q?=D0=B5=D0=B9=D1=81=D1=82=D0=B2=D0=B8=D1=8F=20=D1=81=20config.ya?= =?UTF-8?q?ml=20-=20Roles.py=20=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 3 ++- src/config.yaml | 23 ++++++++++++++++ src/{config.json => modules/AdminChat.py} | 0 src/modules/Config.py | 21 +++++++++++++++ src/modules/DataBase.py | 5 +++- src/modules/Moderation.py | 0 src/modules/Roles.py | 33 +++++++++++++++++++++++ 7 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/config.yaml rename src/{config.json => modules/AdminChat.py} (100%) create mode 100644 src/modules/Config.py create mode 100644 src/modules/Moderation.py diff --git a/requirements.txt b/requirements.txt index 29ab8a3..3bc6ce7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ -aiogram==2.25.1 +aiogram==3.1.1 openai==0.27.8 +PyYAML==6.0.1 diff --git a/src/config.yaml b/src/config.yaml new file mode 100644 index 0000000..3392efe --- /dev/null +++ b/src/config.yaml @@ -0,0 +1,23 @@ +TELEGRAM: + TOKEN: 1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 + PRIVATE_ANSWER: + +BOT: + ANSWER: + HELP_ANSWER: + START_ANSWER: + ABOUT_ANSWER: + +ROLES: + ADMIN: + MODERATOR: + USER: + DEFAULT_FOR_ADMIN: + +DATABASE: + MESSAGE_LIMIT: + +GPT_MODULE: + TRIGGER_FRONT: + TRIGGER_ALL: + REPLY_IGNORE: diff --git a/src/config.json b/src/modules/AdminChat.py similarity index 100% rename from src/config.json rename to src/modules/AdminChat.py diff --git a/src/modules/Config.py b/src/modules/Config.py new file mode 100644 index 0000000..99e2c70 --- /dev/null +++ b/src/modules/Config.py @@ -0,0 +1,21 @@ +import os + +from yaml import load, Loader + + +class Config: + def __init__(self): + self.bot_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd()))) + self.config_dir = os.path.join(self.bot_dir, 'config.yaml') + + def get_config(self): + return load(open(self.config_dir), Loader=Loader) + + def get_bot_token(self): + return self.get_config()['BOT']['TOKEN'] + + def get_roles(self): + return self.get_config()['ROLES'] + + def get_bot_answers(self): + return self.get_config()['BOT']['ANSWER'] diff --git a/src/modules/DataBase.py b/src/modules/DataBase.py index 2d092d4..b52ea2f 100644 --- a/src/modules/DataBase.py +++ b/src/modules/DataBase.py @@ -7,7 +7,6 @@ class DataBase: 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): @@ -82,6 +81,10 @@ class DataBase: self.cursor.execute("""SELECT * FROM user_list WHERE user_id=?""", (user_id,)) return self.cursor.fetchone() + async def get_user_role(self, user_id): + self.cursor.execute("""SELECT user_role 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() diff --git a/src/modules/Moderation.py b/src/modules/Moderation.py new file mode 100644 index 0000000..e69de29 diff --git a/src/modules/Roles.py b/src/modules/Roles.py index e69de29..ee13a4f 100644 --- a/src/modules/Roles.py +++ b/src/modules/Roles.py @@ -0,0 +1,33 @@ +from Config import Config +from DataBase import DataBase + + +class Roles: + def __init__(self): + self.DB = DataBase() + self.Config = Config() + self.user_role_name = self.Config.get_roles()['USER'] + self.moderator_role_name = self.Config.get_roles()['MODERATOR'] + self.admin_role_name = self.Config.get_roles()['ADMIN'] + + async def check_admin_permission(self, user_id): + if await self.DB.get_user_role(user_id) == 2: + return True + else: + return False + + async def check_moderator_permission(self, user_id): + if await self.DB.get_user_role(user_id) == 1: + return True + else: + return False + + async def get_role_name(self, role_number): + if role_number == 0: + return self.user_role_name + elif role_number == 1: + return self.moderator_role_name + elif role_number == 2: + return self.admin_role_name + else: + return None