Автоматический рефакторинг и игнорирование flake8

Выполнен автоматический рефакторинг. Для тех файлов,
которые не прошли flake8 - был добавлен `noqa`, чтобы
в будущем исправить эти проблемы
This commit is contained in:
Maxim Slipenko 2024-07-08 00:38:01 +03:00
parent 4edeef4003
commit e80a01157f
54 changed files with 540 additions and 287 deletions

View File

@ -2,4 +2,5 @@
per-file-ignores =
__init__.py:F401
max-line-length = 88
count = true
count = true
extend-ignore = E203,E701

View File

@ -55,4 +55,4 @@ OCAB - это бот для Telegram, который призван помочь
* SQLite 3 - база данных для хранения информации о чате и пользователях.
* [Poetry](https://gitflic.ru/project/armatik/ocab/blob?file=how-to%20install%20deps.md&branch=OCAB-V2) - менеджер зависимостей.
* aiogram 3 - библиотека для работы с Telegram API.
* peewee - ORM для работы с базой данных.
* peewee - ORM для работы с базой данных.

View File

@ -44,4 +44,4 @@ poetry shell
Хранить окружение внутри проекта
```shell
poetry config virtualenvs.in-project true
```
```

View File

@ -1,5 +1,5 @@
from pathlib import Path
from json import dumps
from pathlib import Path
pwd = Path().cwd()
dir_core = pwd / "src" / "core"
@ -7,9 +7,9 @@ dir_modules_standard = pwd / "src" / "modules" / "standard"
dir_modules_custom = pwd / "src" / "modules" / "custom"
json = {
'core': str(dir_core),
'modules standard': str(dir_modules_standard),
'modules custom': str(dir_modules_custom),
"core": str(dir_core),
"modules standard": str(dir_modules_standard),
"modules custom": str(dir_modules_custom),
}
with open("src/paths.json", "w", encoding="utf8") as f:
f.write(dumps(json, indent=4))

View File

@ -1,4 +1,4 @@
#! /bin/sh
cd src
python -m unittest discover -v
cd ..
cd ..

View File

@ -1,2 +1,2 @@
import src.core
import src.service
import src.core

View File

@ -18,4 +18,4 @@ ROLES:
ADMIN: 2
MODERATOR: 1
USER: 0
BOT: 3
BOT: 3

View File

@ -16,7 +16,7 @@ def setup_logger():
filename=log_file,
level=logging.INFO,
format="%(asctime)s %(message)s",
datefmt="%H:%M:%S"
datefmt="%H:%M:%S",
)

View File

@ -1,9 +1,11 @@
import asyncio
from aiogram import Bot, Dispatcher
from routers import include_routers
from src.core.logger import log, setup_logger
from src.modules.standard.config.config import get_telegram_token
from src.modules.standard.database.db_api import connect_database, create_tables
import asyncio
from aiogram import Bot, Dispatcher
async def main():

View File

@ -1,8 +1,10 @@
from aiogram import Dispatcher
from src.modules.standard.info.routers import router as info_router
from src.modules.standard.admin.routers import router as admin_router
from src.modules.standard.message_processing.message_api import router as process_message
from src.modules.standard.info.routers import router as info_router
from src.modules.standard.message_processing.message_api import (
router as process_message,
)
async def include_routers(dp: Dispatcher):
@ -13,4 +15,3 @@ async def include_routers(dp: Dispatcher):
dp.include_router(info_router)
dp.include_router(admin_router)
dp.include_router(process_message)

View File

@ -1 +0,0 @@

View File

@ -1 +1 @@
from . import yandexgpt
from . import yandexgpt

View File

@ -1,10 +1,17 @@
# flake8: noqa
import asyncio
from aiogram import Bot
from aiogram.types import Message
from src.modules.external.yandexgpt.yandexgpt import *
from src.modules.standard.config.config import get_yandexgpt_token, get_yandexgpt_catalog_id, get_yandexgpt_prompt
from src.modules.standard.database.db_api import add_message
from src.core.logger import log
import asyncio
from src.modules.external.yandexgpt.yandexgpt import *
from src.modules.standard.config.config import (
get_yandexgpt_catalog_id,
get_yandexgpt_prompt,
get_yandexgpt_token,
)
from src.modules.standard.database.db_api import add_message
async def answer_to_message(message: Message, bot: Bot):
@ -14,6 +21,8 @@ async def answer_to_message(message: Message, bot: Bot):
text = message.text
prompt = get_yandexgpt_prompt()
# response = await yagpt.async_yandexgpt(system_prompt=prompt, input_messages=text)
response = await yagpt.yandexgpt_request(chat_id = message.chat.id, message_id = message.message_id, type = "yandexgpt")
response = await yagpt.yandexgpt_request(
chat_id=message.chat.id, message_id=message.message_id, type="yandexgpt"
)
reply = await message.reply(response, parse_mode="Markdown")
add_message(reply, message_ai_model="yandexgpt")

View File

@ -3,4 +3,4 @@
"description": "Модуль для работы с Yandex GPT",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -1,7 +1,10 @@
from aiogram import Router, F
# flake8: noqa
from aiogram import F, Router
from src.modules.external.yandexgpt.handlers import answer_to_message
router = Router()
# Если сообщение содержит в начале текст "Гномик" или "гномик" или отвечает на сообщение бота, то вызывается функция answer_to_message
router.message.register(answer_to_message, F.text.startswith("Гномик") | F.text.startswith("гномик"))
router.message.register(
answer_to_message, F.text.startswith("Гномик") | F.text.startswith("гномик")
)

View File

@ -1,24 +1,27 @@
import requests
import json
# flake8: noqa
import asyncio
import json
import aiohttp
import requests
from src.core.logger import log
from ...standard.database import *
from ...standard.config.config import *
from ...standard.database import *
class YandexGPT:
token = None
catalog_id = None
languages = {
"ru": "русский язык",
"en": "английский язык",
"de": "немецкий язык",
"uk": "украинский язык",
"es": "испанский язык",
"be": "белорусский язык",
}
"ru": "русский язык",
"en": "английский язык",
"de": "немецкий язык",
"uk": "украинский язык",
"es": "испанский язык",
"be": "белорусский язык",
}
def __init__(self, token, catalog_id):
self.token = token
@ -29,11 +32,13 @@ class YandexGPT:
async with session.post(url, headers=headers, json=prompt) as response:
return await response.json()
async def async_token_check(self, messages, gpt, max_tokens, stream, temperature, del_msg_id=1):
async def async_token_check(
self, messages, gpt, max_tokens, stream, temperature, del_msg_id=1
):
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/tokenizeCompletion"
headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {self.token}"
"Authorization": f"Api-Key {self.token}",
}
answer_token = get_yandexgpt_token_for_answer()
while True:
@ -43,12 +48,14 @@ class YandexGPT:
"completionOptions": {
"stream": stream,
"temperature": temperature,
"maxTokens": max_tokens
"maxTokens": max_tokens,
},
"messages": messages
"messages": messages,
}
response = await self.async_request(url=url, headers=headers, prompt=request)
except Exception as e: # TODO: Переделать обработку ошибок
response = await self.async_request(
url=url, headers=headers, prompt=request
)
except Exception as e: # TODO: Переделать обработку ошибок
# print(e)
await log(f"Error: {e}")
@ -62,12 +69,19 @@ class YandexGPT:
Exception("IndexError: list index out of range")
return messages
async def async_yandexgpt_lite(self, system_prompt, input_messages, stream=False, temperature=0.6, max_tokens=8000):
async def async_yandexgpt_lite(
self,
system_prompt,
input_messages,
stream=False,
temperature=0.6,
max_tokens=8000,
):
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
gpt = f"gpt://{self.catalog_id}/yandexgpt-lite/latest"
headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {self.token}"
"Authorization": f"Api-Key {self.token}",
}
messages = [{"role": "system", "text": system_prompt}]
@ -80,27 +94,27 @@ class YandexGPT:
"completionOptions": {
"stream": stream,
"temperature": temperature,
"maxTokens": max_tokens
"maxTokens": max_tokens,
},
"messages": messages
"messages": messages,
}
response = requests.post(url, headers=headers, json=prompt).text
return json.loads(response)["result"]["alternatives"][0]["message"]["text"]
async def async_yandexgpt(
self,
system_prompt,
input_messages,
stream=False,
temperature=0.6,
max_tokens=get_yandexgpt_token_for_request()
self,
system_prompt,
input_messages,
stream=False,
temperature=0.6,
max_tokens=get_yandexgpt_token_for_request(),
):
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
gpt = f"gpt://{self.catalog_id}/yandexgpt/latest"
headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {self.token}"
"Authorization": f"Api-Key {self.token}",
}
messages = []
@ -108,21 +122,22 @@ class YandexGPT:
for message in input_messages:
messages.append(message)
messages = await self.async_token_check(messages, gpt, max_tokens, stream, temperature)
messages = await self.async_token_check(
messages, gpt, max_tokens, stream, temperature
)
request = {
"modelUri": gpt,
"completionOptions": {
"stream": stream,
"temperature": temperature,
"maxTokens": max_tokens
"maxTokens": max_tokens,
},
"messages": messages
"messages": messages,
}
response = await self.async_request(url=url, headers=headers, prompt=request)
return response["result"]["alternatives"][0]["message"]["text"]
async def async_yandexgpt_translate(self, input_language, output_language, text):
input_language = self.languages[input_language]
output_language = self.languages[output_language]
@ -130,7 +145,9 @@ class YandexGPT:
return await self.async_yandexgpt(
f"Переведи на {output_language} сохранив оригинальный смысл текста. Верни только результат:",
[{"role": "user", "text": text}],
stream=False, temperature=0.6, max_tokens=8000
stream=False,
temperature=0.6,
max_tokens=8000,
)
async def async_yandexgpt_spelling_check(self, input_language, text):
@ -140,15 +157,19 @@ class YandexGPT:
f"Проверьте орфографию и пунктуацию текста на {input_language}. Верни исправленный текст "
f"без смысловых искажений:",
[{"role": "user", "text": text}],
stream=False, temperature=0.6, max_tokens=8000
stream=False,
temperature=0.6,
max_tokens=8000,
)
async def async_yandexgpt_text_history(self, input_messages, stream=False, temperature=0.6, max_tokens=8000):
async def async_yandexgpt_text_history(
self, input_messages, stream=False, temperature=0.6, max_tokens=8000
):
url = "https://llm.api.cloud.yandex.net/foundationModels/v1/completion"
gpt = f"gpt://{self.catalog_id}/summarization/latest"
headers = {
"Content-Type": "application/json",
"Authorization": f"Api-Key {self.token}"
"Authorization": f"Api-Key {self.token}",
}
messages = []
@ -161,15 +182,17 @@ class YandexGPT:
"completionOptions": {
"stream": stream,
"temperature": temperature,
"maxTokens": max_tokens
"maxTokens": max_tokens,
},
"messages": messages
"messages": messages,
}
response = requests.post(url, headers=headers, json=prompt).text
return json.loads(response)["result"]["alternatives"][0]["message"]["text"]
async def async_yandex_cloud_text_to_speech(self, text, voice, emotion, speed, format, quality):
async def async_yandex_cloud_text_to_speech(
self, text, voice, emotion, speed, format, quality
):
tts = "tts.api.cloud.yandex.net/speech/v1/tts:synthesize"
# TODO: Сделать функцию TTS
return 0
@ -187,14 +210,18 @@ class YandexGPT:
if db_api.get_message_ai_model(chat_id, message_id) != None:
messages.append({"role": "assistant", "text": message})
else:
sender_name = db_api.get_user_name(db_api.get_message_sender_id(chat_id, message_id))
sender_name = db_api.get_user_name(
db_api.get_message_sender_id(chat_id, message_id)
)
messages.append({"role": "user", "text": sender_name + ": " + message})
message_id = db_api.get_answer_to_message_id(chat_id, message_id)
if message_id is None:
break
return list(reversed(messages))
async def collecting_messages_for_history(self, start_message_id, end_message_id, chat_id):
async def collecting_messages_for_history(
self, start_message_id, end_message_id, chat_id
):
messages = []
# Собираем цепочку сообщений в формате: [{"role": "user", "text": "<Имя_пользователя>: Привет!"},
# {"role": "assistant", "text": "Привет!"}]
@ -203,21 +230,33 @@ class YandexGPT:
if db_api.get_message_ai_model(chat_id, start_message_id) != None:
messages.append({"role": "assistant", "text": message})
else:
sender_name = db_api.get_user_name(db_api.get_message_sender_id(chat_id, start_message_id))
sender_name = db_api.get_user_name(
db_api.get_message_sender_id(chat_id, start_message_id)
)
messages.append({"role": "user", "text": sender_name + ": " + message})
start_message_id -= 1
if start_message_id <= end_message_id:
break
return messages.reverse()
async def yandexgpt_request(self, message_id = None, type = "yandexgpt-lite", chat_id = None,
message_id_end = None, input_language = None, output_language = None, text = None):
async def yandexgpt_request(
self,
message_id=None,
type="yandexgpt-lite",
chat_id=None,
message_id_end=None,
input_language=None,
output_language=None,
text=None,
):
if type == "yandexgpt-lite":
messages = await self.collect_messages(message_id, chat_id)
return await self.async_yandexgpt_lite(
system_prompt=get_yandexgpt_prompt(),
input_messages=messages,
stream=False, temperature=0.6, max_tokens=8000
stream=False,
temperature=0.6,
max_tokens=8000,
)
elif type == "yandexgpt":
# print("yandexgpt_request")
@ -226,24 +265,26 @@ class YandexGPT:
return await self.async_yandexgpt(
system_prompt=get_yandexgpt_prompt(),
input_messages=messages,
stream=False, temperature=0.6, max_tokens=get_yandexgpt_token_for_request()
stream=False,
temperature=0.6,
max_tokens=get_yandexgpt_token_for_request(),
)
elif type == "yandexgpt-translate":
return await self.async_yandexgpt_translate(
input_language,
output_language,
text=db_api.get_message_text(chat_id, message_id)
text=db_api.get_message_text(chat_id, message_id),
)
elif type == "yandexgpt-spelling-check":
return await self.async_yandexgpt_spelling_check(
input_language,
text=db_api.get_message_text(chat_id, message_id)
input_language, text=db_api.get_message_text(chat_id, message_id)
)
elif type == "yandexgpt-text-history":
messages = await self.collect_messages_for_history(message_id, message_id_end, chat_id)
messages = await self.collect_messages_for_history(
message_id, message_id_end, chat_id
)
return await self.async_yandexgpt_text_history(
messages=messages,
stream=False, temperature=0.6, max_tokens=8000
messages=messages, stream=False, temperature=0.6, max_tokens=8000
)
else:
return "Ошибка: Неизвестный тип запроса | Error: Unknown request type"

View File

@ -1 +1 @@
from . import config, database, exceptions, roles
from . import config, database, exceptions, roles

View File

@ -1,18 +1,26 @@
# flake8: noqa
import time
from aiogram import Bot
from aiogram.types import Message
from src.modules.standard.config.config import get_default_chat_tag
import time
async def delete_message(message: Message, bot: Bot):
reply_message_id = message.reply_to_message.message_id
await bot.delete_message(message.chat.id, reply_message_id)
async def error_access(message: Message, bot: Bot):
await message.reply("Вы не админ/модератор")
async def get_chat_id(message: Message, bot: Bot):
await message.reply(f"ID данного чата: `{message.chat.id}`", parse_mode="MarkdownV2")
await message.reply(
f"ID данного чата: `{message.chat.id}`", parse_mode="MarkdownV2"
)
async def chat_not_in_approve_list(message: Message, bot: Bot):
await message.reply(
@ -22,6 +30,7 @@ async def chat_not_in_approve_list(message: Message, bot: Bot):
)
await get_chat_id(message, bot)
async def mute_user(chat_id: int, user_id: int, time: int, bot: Bot):
# *, can_send_messages: bool | None = None, can_send_audios: bool | None = None, can_send_documents: bool | None = None, can_send_photos: bool | None = None, can_send_videos: bool | None = None, can_send_video_notes: bool | None = None, can_send_voice_notes: bool | None = None, can_send_polls: bool | None = None, can_send_other_messages: bool | None = None, can_add_web_page_previews: bool | None = None, can_change_info: bool | None = None, can_invite_users: bool | None = None, can_pin_messages: bool | None = None, can_manage_topics: bool | None = None, **extra_data: Any)
@ -39,8 +48,9 @@ async def mute_user(chat_id: int, user_id: int, time: int, bot: Bot):
"can_change_info": False,
"can_invite_users": False,
"can_pin_messages": False,
"can_manage_topics": False
"can_manage_topics": False,
}
end_time = time + int(time.time())
await bot.restrict_chat_member(chat_id, user_id, until_date=end_time, **mutePermissions)
await bot.restrict_chat_member(
chat_id, user_id, until_date=end_time, **mutePermissions
)

View File

@ -3,4 +3,4 @@
"description": "Модуль для работы с админкой",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -1,15 +1,24 @@
from aiogram import Router, F
# flake8: noqa
from aiogram import F, Router
from src.modules.standard.admin.handlers import delete_message, error_access, get_chat_id, chat_not_in_approve_list
from src.modules.standard.filters.filters import ChatModerOrAdminFilter, ChatNotInApproveFilter
from src.modules.standard.admin.handlers import (
chat_not_in_approve_list,
delete_message,
error_access,
get_chat_id,
)
from src.modules.standard.filters.filters import (
ChatModerOrAdminFilter,
ChatNotInApproveFilter,
)
router = Router()
# Если сообщение содержит какой либо текст и выполняется фильтр ChatNotInApproveFilter, то вызывается функция chat_not_in_approve_list
router.message.register(chat_not_in_approve_list, ChatNotInApproveFilter(), F.text)
router.message.register(get_chat_id, ChatModerOrAdminFilter(), F.text == '/chatID')
router.message.register(get_chat_id, ChatModerOrAdminFilter(), F.text == "/chatID")
router.message.register(delete_message, ChatModerOrAdminFilter(), F.text == '/rm')
router.message.register(error_access, F.text == '/rm')
router.message.register(error_access, F.text == '/chatID')
router.message.register(delete_message, ChatModerOrAdminFilter(), F.text == "/rm")
router.message.register(error_access, F.text == "/rm")
router.message.register(error_access, F.text == "/chatID")

View File

@ -1,4 +1,7 @@
# flake8: noqa
import yaml
from ....service import paths
@ -9,52 +12,64 @@ def get_config(is_test: bool = False) -> dict:
path = paths.core
path = f"{path}/config.yaml"
with open(path, 'r') as file:
with open(path, "r") as file:
return yaml.full_load(file)
config = get_config()
def get_telegram_token() -> str:
return config["TELEGRAM"]["TOKEN"]
def get_telegram_check_bot() -> bool:
return config["TELEGRAM"]["CHECK_BOT"]
def get_aproved_chat_id() -> list:
# Возваращем сплитованный список id чатов в формате int
return [int(chat_id) for chat_id in config["TELEGRAM"]["APPROVED_CHAT_ID"].split(" | ")]
return [
int(chat_id) for chat_id in config["TELEGRAM"]["APPROVED_CHAT_ID"].split(" | ")
]
def get_user_role_name(role_number) -> dict:
# Возвращаем название роли пользвателя по номеру роли, если такой роли нет, возвращаем неизвестно
return config["ROLES"].get(role_number, "Неизвестно")
def get_default_chat_tag() -> str:
return config["TELEGRAM"]["DEFAULT_CHAT_TAG"]
def get_yandexgpt_token() -> str:
return config["YANDEXGPT"]["TOKEN"]
def get_yandexgpt_catalog_id() -> str:
return config["YANDEXGPT"]["CATALOGID"]
def get_yandexgpt_prompt() -> str:
return config["YANDEXGPT"]["PROMPT"]
def get_yandexgpt_start_words() -> list:
return config["YANDEXGPT"]["STARTWORD"].split(" | ")
def get_yandexgpt_in_words() -> list:
return config["YANDEXGPT"]["INWORD"].split(" | ")
def get_yandexgpt_token_for_request() -> int:
return config["YANDEXGPT"]["TOKEN_FOR_REQUEST"]
def get_yandexgpt_token_for_answer() -> int:
return config["YANDEXGPT"]["TOKEN_FOR_ANSWER"]
def get_access_rights() -> dict:
return get_config()["ACCESS_RIGHTS"]

View File

@ -3,4 +3,4 @@
"description": "Модуль для работы с конфигурационным файлом бота (YAML)",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -4,4 +4,4 @@ ROLES:
ADMIN: 0
MODERATOR: 1
USER: 2
BOT: 3
BOT: 3

View File

@ -1,6 +1,7 @@
from src.modules.standard.config.config import get_config
import unittest
from src.modules.standard.config.config import get_config
yaml_load = get_config(is_test=True)
@ -18,20 +19,27 @@ class TestConfig(unittest.TestCase):
def test_yaml_keys_existence(self):
self.assertTrue(all(key in yaml_load for key in ["TELEGRAM", "ROLES"]))
self.assertIn("TOKEN", yaml_load["TELEGRAM"])
self.assertTrue(all(role in yaml_load["ROLES"] for role in ["ADMIN", "MODERATOR", "USER"]))
self.assertTrue(
all(role in yaml_load["ROLES"] for role in ["ADMIN", "MODERATOR", "USER"])
)
def test_yaml_yaml_load_types(self):
self.assertIsInstance(yaml_load["TELEGRAM"]["TOKEN"], str)
self.assertTrue(all(isinstance(yaml_load["ROLES"][role], int) for role in ["ADMIN", "MODERATOR", "USER"]))
self.assertTrue(
all(
isinstance(yaml_load["ROLES"][role], int)
for role in ["ADMIN", "MODERATOR", "USER"]
)
)
def test_yaml_values(self):
expected_token = 'xxxxxxxxxxxxxxxxxxxx'
expected_role_values = {'ADMIN': 0, 'MODERATOR': 1, 'USER': 2, 'BOT': 3}
expected_token = "xxxxxxxxxxxxxxxxxxxx"
expected_role_values = {"ADMIN": 0, "MODERATOR": 1, "USER": 2, "BOT": 3}
self.assertEqual(yaml_load["TELEGRAM"]["TOKEN"], expected_token)
for role, value in expected_role_values.items():
self.assertEqual(yaml_load["ROLES"][role], value)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@ -4,7 +4,7 @@
Модуль содержит в себе следующие таблицы:
* `Chats` - таблица для хранения информации о чатах.
* `Chats` - таблица для хранения информации о чатах.
* `Users` - таблица для хранения информации о пользователях.
* `Messages` - таблица для хранения информации о сообщениях.
* `ChatStats` - таблица для хранения статистики чатов по дням.
@ -28,9 +28,9 @@ Cтруктура таблицы `Users`:
руктура таблицы `Messages`:
* `message_chat_id` - идентификатор чата в котором отправлено сообщение.
* `message_id` - идентификатор сообщения.
* `messag_sender_id` - идентификатор пользователя отправившего сообщение. Если сообщение отправил бот, то
* `messag_sender_id` - идентификатор пользователя отправившего сообщение. Если сообщение отправил бот, то
`messag_sender_id` = 0.
* `answer_to_message_id` - идентификатор сообщения на которое дан ответ. Если ответа нет или ответ на служебное
* `answer_to_message_id` - идентификатор сообщения на которое дан ответ. Если ответа нет или ответ на служебное
сообщение о создании топика в чатах с форумным типом, то `answer_to_message_id` = 0.
* `message_ai_model` - идентификатор модели нейросети, которая использовалась для генерации ответа. Если ответ'
сгенерирован не был, то `message_ai_model` = null.
@ -45,4 +45,4 @@ Cтруктура таблицы `UserStats`:
* `chat_id` - идентификатор чата для которого собрана статистика.
* `user_id` - идентификатор пользователя для которого собрана статистика.
* `date` - дата на которую собрана статистика.
* `messages_count` - количество сообщений отправленных пользователем в чат за день.
* `messages_count` - количество сообщений отправленных пользователем в чат за день.

View File

@ -1 +1 @@
from . import db_api, models
from . import db_api, models

View File

@ -1,13 +1,14 @@
from .models.chats import Chats
from .models.messages import Messages
from .models.users import Users
from .models.user_stats import UserStats
from .models.chat_stats import ChatStats
from ....service import paths
from ..exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
import peewee as pw
from aiogram.types import Message
from ....service import paths
from ..exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
from .models.chat_stats import ChatStats
from .models.chats import Chats
from .models.messages import Messages
from .models.user_stats import UserStats
from .models.users import Users
def connect_database(is_test: bool = False, module: str | None = None):
if is_test:
@ -37,11 +38,14 @@ def create_tables(db: pw.SqliteDatabase):
def add_chat(chat_id, chat_name, chat_type=10, chat_stats=0):
chat, created = Chats.get_or_create(id=chat_id, defaults={
'chat_name': chat_name,
'chat_type': chat_type,
'chat_all_stat': chat_stats,
})
chat, created = Chats.get_or_create(
id=chat_id,
defaults={
"chat_name": chat_name,
"chat_type": chat_type,
"chat_all_stat": chat_stats,
},
)
if not created:
# Обновить существующий чат, если он уже существует
chat.chat_name = chat_name
@ -50,19 +54,30 @@ def add_chat(chat_id, chat_name, chat_type=10, chat_stats=0):
chat.save()
def add_user(user_id, user_first_name, user_last_name=None, user_tag=None, user_role=0, user_stats=0, user_rep=0):
def add_user(
user_id,
user_first_name,
user_last_name=None,
user_tag=None,
user_role=0,
user_stats=0,
user_rep=0,
):
if user_last_name is None:
user_name = user_first_name
else:
user_name = user_first_name + " " + user_last_name
user, created = Users.get_or_create(id=user_id, defaults={
'user_tag': user_tag,
'user_name': user_name,
'user_role': user_role,
'user_stats': user_stats,
'user_rep': user_rep
})
user, created = Users.get_or_create(
id=user_id,
defaults={
"user_tag": user_tag,
"user_name": user_name,
"user_role": user_role,
"user_stats": user_stats,
"user_rep": user_rep,
},
)
if not created:
# Обновить существующего пользователя, если он уже существует
user.user_tag = user_tag
@ -84,26 +99,20 @@ def add_message(message: Message, message_ai_model=None):
message_sender_id=message.from_user.id,
answer_to_message_id=answer_to_message_id,
message_ai_model=message_ai_model,
message_text=message.text
message_text=message.text,
)
def add_chat_stats(chat_id, date, messages_count):
ChatStats.create(
chat_id=chat_id,
date=date,
messages_count=messages_count
)
ChatStats.create(chat_id=chat_id, date=date, messages_count=messages_count)
def add_user_stats(chat_id, user_id, date, messages_count):
UserStats.create(
chat_id=chat_id,
user_id=user_id,
date=date,
messages_count=messages_count
chat_id=chat_id, user_id=user_id, date=date, messages_count=messages_count
)
# Работа с таблицей чатов
@ -125,6 +134,7 @@ def get_chat_all_stat(chat_id):
chat = Chats.get_or_none(Chats.id == chat_id)
return chat.chat_all_stat if chat else None
# Работа с таблицей пользователей
@ -136,6 +146,7 @@ def get_user_tag(user_id):
user = Users.get_or_none(Users.id == user_id)
return user.user_tag if user else None
def get_user_id(user_tag):
user = Users.get_or_none(Users.user_tag == user_tag)
return user.id if user else None
@ -179,32 +190,44 @@ def change_user_role(user_id, new_user_role):
query = Users.update(user_role=new_user_role).where(Users.id == user_id)
query.execute()
# Работа с таблицей сообщений
def get_message(message_chat_id, message_id):
return Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
return Messages.get_or_none(
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
)
def get_message_sender_id(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
message = Messages.get_or_none(
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
)
return message.message_sender_id if message else None
def get_message_text(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
message = Messages.get_or_none(
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
)
return message.message_text if message else None
def get_message_ai_model(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
message = Messages.get_or_none(
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
)
return message.message_ai_model if message else None
def get_answer_to_message_id(message_chat_id, message_id):
message = Messages.get_or_none(Messages.message_chat_id == message_chat_id, Messages.message_id == message_id)
message = Messages.get_or_none(
Messages.message_chat_id == message_chat_id, Messages.message_id == message_id
)
return message.answer_to_message_id if message else None
# Работа с таблицей статистики чатов
@ -214,6 +237,7 @@ def get_chat_stats(chat_id):
chat_stats[chat_stat.date] = chat_stat.messages_count
return chat_stats
# Работа с таблицей статистики пользователей
@ -223,26 +247,28 @@ def get_user_stats(user_id):
user_stats[user_stat.date] = user_stat.messages_count
return user_stats
# Функции обновления
def update_chat_all_stat(chat_id):
query = Chats.update(chat_all_stat=Chats.chat_all_stat + 1).where(Chats.id == chat_id)
query = Chats.update(chat_all_stat=Chats.chat_all_stat + 1).where(
Chats.id == chat_id
)
query.execute()
def update_chat_stats(chat_id, date):
chat_stats = ChatStats.get_or_none(ChatStats.chat_id == chat_id, ChatStats.date == date)
chat_stats = ChatStats.get_or_none(
ChatStats.chat_id == chat_id, ChatStats.date == date
)
if chat_stats:
query = ChatStats.update(messages_count=ChatStats.messages_count + 1).where(ChatStats.chat_id == chat_id,
ChatStats.date == date)
query = ChatStats.update(messages_count=ChatStats.messages_count + 1).where(
ChatStats.chat_id == chat_id, ChatStats.date == date
)
query.execute()
else:
ChatStats.create(
chat_id=chat_id,
date=date,
messages_count=1
)
ChatStats.create(chat_id=chat_id, date=date, messages_count=1)
def update_user_all_stat(user_id):
@ -251,10 +277,7 @@ def update_user_all_stat(user_id):
query = Users.update(user_stats=Users.user_stats + 1).where(Users.id == user_id)
query.execute()
else:
Users.create(
id=user_id,
user_stats=1
)
Users.create(id=user_id, user_stats=1)
def update_user_rep(user_id):
@ -263,24 +286,21 @@ def update_user_rep(user_id):
query = Users.update(user_rep=Users.user_rep + 1).where(Users.id == user_id)
query.execute()
else:
Users.create(
id=user_id,
user_rep=1
)
Users.create(id=user_id, user_rep=1)
def update_user_stats(chat_id, user_id, date):
user_stats = UserStats.get_or_none(UserStats.chat_id == chat_id, UserStats.user_id == user_id,
UserStats.date == date)
user_stats = UserStats.get_or_none(
UserStats.chat_id == chat_id,
UserStats.user_id == user_id,
UserStats.date == date,
)
if user_stats:
query = UserStats.update(messages_count=UserStats.messages_count + 1).where(UserStats.chat_id == chat_id,
UserStats.user_id == user_id,
UserStats.date == date)
query = UserStats.update(messages_count=UserStats.messages_count + 1).where(
UserStats.chat_id == chat_id,
UserStats.user_id == user_id,
UserStats.date == date,
)
query.execute()
else:
UserStats.create(
chat_id=chat_id,
user_id=user_id,
date=date,
messages_count=1
)
UserStats.create(chat_id=chat_id, user_id=user_id, date=date, messages_count=1)

View File

@ -3,4 +3,4 @@
"description": "Модуль для работы с БД",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -2,8 +2,8 @@ import peewee as pw
class ChatStats(pw.Model):
class Meta:
...
class Meta: ...
chat_id = pw.IntegerField(null=False)
date = pw.DateField(null=False)
messages_count = pw.IntegerField(null=False, default=0)

View File

@ -2,8 +2,8 @@ import peewee as pw
class Chats(pw.Model):
class Meta:
...
class Meta: ...
chat_name = pw.CharField(null=False)
chat_type = pw.IntegerField(null=False, default=10)
chat_all_stat = pw.IntegerField(null=False)

View File

@ -2,12 +2,11 @@ import peewee as pw
class Messages(pw.Model):
class Meta:
...
class Meta: ...
message_chat_id = pw.IntegerField(null=False)
message_id = pw.IntegerField(null=False)
message_sender_id = pw.IntegerField(null=False)
answer_to_message_id = pw.IntegerField(null=True)
message_ai_model = pw.TextField(null=True)
message_text = pw.TextField(null=False)

View File

@ -2,8 +2,8 @@ import peewee as pw
class UserStats(pw.Model):
class Meta:
...
class Meta: ...
chat_id = pw.IntegerField(null=False)
user_id = pw.IntegerField(null=False)
date = pw.DateField(null=False)

View File

@ -2,8 +2,8 @@ import peewee as pw
class Users(pw.Model):
class Meta:
...
class Meta: ...
user_tag = pw.CharField(null=True)
user_name = pw.CharField(null=False) # до 255 символов
user_role = pw.IntegerField(null=True, default=3)

View File

@ -1 +1 @@
Эта директория для тестовой БД
Эта директория для тестовой БД

View File

@ -1,8 +1,10 @@
import unittest
import os
# flake8: noqa
import os
import unittest
from ..db_api import *
from ...exceptions.module_exceptions import MissingModuleName, NotExpectedModuleName
from ..db_api import *
class TestDatabaseAPI(unittest.TestCase):
@ -13,6 +15,7 @@ class TestDatabaseAPI(unittest.TestCase):
def setUpClass(cls):
cls.database, cls.path = connect_database(is_test=True, module="database")
create_tables(cls.database)
def test_fail_connect(cls):
with cls.assertRaises(MissingModuleName):
cls.database, cls.path = connect_database(is_test=True)
@ -35,8 +38,12 @@ class TestDatabaseAPI(unittest.TestCase):
self.assertEqual(chat2.chat_role, 1)
def test_add_and_get_message(self):
add_message(message_id=1, message_text="Test Message 1", message_sender=1, answer_id=2)
add_message(message_id=2, message_text="Test Message 2", message_sender=2, answer_id=1)
add_message(
message_id=1, message_text="Test Message 1", message_sender=1, answer_id=2
)
add_message(
message_id=2, message_text="Test Message 2", message_sender=2, answer_id=1
)
message1 = get_message(1)
self.assertIsNotNone(message1)
@ -49,8 +56,22 @@ class TestDatabaseAPI(unittest.TestCase):
self.assertEqual(message2.message_text, "Test Message 2")
def test_add_and_get_user(self):
add_user(user_id=100, user_name="TestUser1", user_tag="TestTag1", user_role=0, user_stats=10, user_rep=5)
add_user(user_id=101, user_name="TestUser2", user_tag="TestTag2", user_role=1, user_stats=20, user_rep=10)
add_user(
user_id=100,
user_name="TestUser1",
user_tag="TestTag1",
user_role=0,
user_stats=10,
user_rep=5,
)
add_user(
user_id=101,
user_name="TestUser2",
user_tag="TestTag2",
user_role=1,
user_stats=20,
user_rep=10,
)
user1 = get_user(100)
self.assertIsNotNone(user1)
@ -63,8 +84,22 @@ class TestDatabaseAPI(unittest.TestCase):
self.assertEqual(user2.user_name, "TestUser2")
def test_get_user_role(self):
add_user(user_id=102, user_name="TestUser3", user_tag="TestTag3", user_role=0, user_stats=30, user_rep=15)
add_user(user_id=103, user_name="TestUser4", user_tag="TestTag4", user_role=1, user_stats=40, user_rep=20)
add_user(
user_id=102,
user_name="TestUser3",
user_tag="TestTag3",
user_role=0,
user_stats=30,
user_rep=15,
)
add_user(
user_id=103,
user_name="TestUser4",
user_tag="TestTag4",
user_role=1,
user_stats=40,
user_rep=20,
)
user_role1 = get_user_role(102)
self.assertEqual(user_role1, 0)
@ -73,12 +108,26 @@ class TestDatabaseAPI(unittest.TestCase):
self.assertEqual(user_role2, 1)
def test_change_user_name(self):
add_user(user_id=104, user_name="OldName1", user_tag="TestTag5", user_role=0, user_stats=50, user_rep=25)
add_user(
user_id=104,
user_name="OldName1",
user_tag="TestTag5",
user_role=0,
user_stats=50,
user_rep=25,
)
change_user_name(104, "NewName1")
updated_user1 = get_user(104)
self.assertEqual(updated_user1.user_name, "NewName1")
add_user(user_id=105, user_name="OldName2", user_tag="TestTag6", user_role=1, user_stats=60, user_rep=30)
add_user(
user_id=105,
user_name="OldName2",
user_tag="TestTag6",
user_role=1,
user_stats=60,
user_rep=30,
)
change_user_name(105, "NewName2")
updated_user2 = get_user(105)
self.assertEqual(updated_user2.user_name, "NewName2")
@ -89,5 +138,5 @@ class TestDatabaseAPI(unittest.TestCase):
os.system(f"rm {cls.path}")
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@ -1 +1 @@
from . import module_exceptions
from . import module_exceptions

View File

@ -3,4 +3,4 @@
"description": "Модуль с исключениями",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -10,4 +10,3 @@ class NotExpectedModuleName(BaseException):
self.message = "Не ожидалось название директории модуля"
super().__init__(self.message)

View File

@ -1,18 +1,23 @@
from aiogram import Bot
from aiogram.filters import BaseFilter
from aiogram.types import Message
from aiogram import Bot
from src.modules.standard.roles.roles import Roles
from src.modules.standard.config.config import get_aproved_chat_id
from src.core.logger import log
from src.modules.standard.config.config import get_aproved_chat_id
from src.modules.standard.roles.roles import Roles
class ChatModerOrAdminFilter(BaseFilter):
async def __call__(self, message: Message, bot: Bot) -> bool:
user_id = message.from_user.id
roles = Roles()
admins = await bot.get_chat_administrators(message.chat.id)
return await roles.check_admin_permission(user_id) or \
await roles.check_moderator_permission(user_id) or any(user_id == admin.user.id for admin in admins)
return (
await roles.check_admin_permission(user_id)
or await roles.check_moderator_permission(user_id)
or any(user_id == admin.user.id for admin in admins)
)
class ChatNotInApproveFilter(BaseFilter):
async def __call__(self, message: Message, bot: Bot) -> bool:

View File

@ -3,4 +3,4 @@
"description": "Модуль с фильтрами",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -1,13 +1,20 @@
# flake8: noqa
from aiogram import Bot
from aiogram.types import Message
from src.modules.standard.config.config import get_user_role_name
from src.modules.standard.roles.roles import Roles
from src.modules.standard.database.db_api import *
from src.core.logger import log
from src.modules.standard.config.config import get_user_role_name
from src.modules.standard.database.db_api import *
from src.modules.standard.roles.roles import Roles
async def get_info_answer_by_id(message: Message, bot: Bot, user_id: int):
if get_message_ai_model(message.chat.id, message.message_id) is not None:
await message.reply("Это сообщение было сгенерировано ботом используя модель: " + get_message_ai_model(message.chat.id, message.message_id))
await message.reply(
"Это сообщение было сгенерировано ботом используя модель: "
+ get_message_ai_model(message.chat.id, message.message_id)
)
elif user_id == bot.id:
await message.reply("Это сообщение было отправлено ботом")
elif get_user(user_id) is None:
@ -44,11 +51,14 @@ async def get_user_info(message: Message, bot: Bot):
else:
await get_info_answer_by_id(message, bot, message.from_user.id)
except Exception as e:
await message.reply("В вашем запросе что-то пошло не так,"
" попробуйте запросить информацию о пользователе по его тегу или ответив на его сообщение")
await message.reply(
"В вашем запросе что-то пошло не так,"
" попробуйте запросить информацию о пользователе по его тегу или ответив на его сообщение"
)
# print(e)
await log(e)
async def get_chat_info(message: Message, bot: Bot):
answer = (
f"*Название чата:* {message.chat.title}\n"
@ -57,4 +67,4 @@ async def get_chat_info(message: Message, bot: Bot):
f"*Количество пользователей в чате:* {await bot.get_chat_member_count(message.chat.id)}\n"
f"*Количество администраторов в чате:* {len(await bot.get_chat_administrators(message.chat.id))}"
)
await message.reply(answer, parse_mode="MarkdownV2")
await message.reply(answer, parse_mode="MarkdownV2")

View File

@ -1,8 +1,10 @@
from aiogram import Router, F
# flake8: noqa
from src.modules.standard.info.handlers import get_user_info, get_chat_info
from aiogram import F, Router
from src.modules.standard.info.handlers import get_chat_info, get_user_info
router = Router()
router.message.register(get_user_info, F.text.startswith("/info") == True)
router.message.register(get_chat_info, F.text.startswith("/chatinfo") == True)
router.message.register(get_chat_info, F.text.startswith("/chatinfo") == True)

View File

@ -1,9 +1,15 @@
from aiogram import Router, F, Bot, types
# flake8: noqa
from aiogram import Bot, F, Router, types
from src.modules.external.yandexgpt.handlers import answer_to_message
from src.modules.standard.database.db_api import *
from src.modules.standard.config.config import get_yandexgpt_start_words, get_yandexgpt_in_words, get_aproved_chat_id
from src.core.logger import log
from src.modules.external.yandexgpt.handlers import answer_to_message
from src.modules.standard.config.config import (
get_aproved_chat_id,
get_yandexgpt_in_words,
get_yandexgpt_start_words,
)
from src.modules.standard.database.db_api import *
async def chat_check(message: types.Message):
@ -19,7 +25,9 @@ async def chat_check(message: types.Message):
await log(f"Chat added: {message.chat.id} {message.chat.title}")
else:
# print(f"Chat not in approve list: {message.chat.id} {message.chat.title}")
await log(f"Chat not in approve list: {message.chat.id} {message.chat.title}")
await log(
f"Chat not in approve list: {message.chat.id} {message.chat.title}"
)
pass
else:
# Проверяем обновление названия чата
@ -34,6 +42,7 @@ async def chat_check(message: types.Message):
await log(f"Chat already exists: {message.chat.id} {message.chat.title}")
pass
async def user_check(message: types.Message):
# Проверка наличия id пользователя в базе данных пользователей
# Если пользователя нет в базе данных, то добавляем его
@ -42,17 +51,24 @@ async def user_check(message: types.Message):
if message.from_user.last_name is None:
current_user_name = message.from_user.first_name
else:
current_user_name = message.from_user.first_name + " " + message.from_user.last_name
current_user_name = (
message.from_user.first_name + " " + message.from_user.last_name
)
if get_user(message.from_user.id) is None:
add_user(message.from_user.id,
message.from_user.first_name, message.from_user.last_name,
message.from_user.username)
add_user(
message.from_user.id,
message.from_user.first_name,
message.from_user.last_name,
message.from_user.username,
)
# print(f"User added: {message.from_user.id} {message.from_user.first_name} {message.from_user.last_name}")
await log(f"User added: {message.from_user.id} {current_user_name}")
else:
# print(f"User already exists: {message.from_user.id} {message.from_user.first_name} {message.from_user.last_name} {message.from_user.username}")
await log(f"User already exists: {message.from_user.id} {current_user_name} {message.from_user.username}")
await log(
f"User already exists: {message.from_user.id} {current_user_name} {message.from_user.username}"
)
# Проверяем обновление имени пользователя
if get_user_name(message.from_user.id) != current_user_name:
change_user_name(message.from_user.id, current_user_name)
@ -62,9 +78,12 @@ async def user_check(message: types.Message):
if get_user_tag(message.from_user.id) != message.from_user.username:
change_user_tag(message.from_user.id, message.from_user.username)
# print(f"User updated: {message.from_user.id} {message.from_user.username}")
await log(f"User tag updated: {message.from_user.id} {message.from_user.username}")
await log(
f"User tag updated: {message.from_user.id} {message.from_user.username}"
)
pass
async def add_stats(message: types.Message):
# Добавляем пользователю и чату статистику
update_chat_all_stat(message.chat.id)
@ -79,8 +98,9 @@ async def message_processing(message: types.Message, bot: Bot):
add_message(message)
# Если сообщение в начале содержит слово из списка или внутри сообщения содержится слово из списка или сообщение отвечает на сообщение бота
if ((message.text.split(" ")[0] in get_yandexgpt_start_words())
or (any(word in message.text for word in get_yandexgpt_in_words()))):
if (message.text.split(" ")[0] in get_yandexgpt_start_words()) or (
any(word in message.text for word in get_yandexgpt_in_words())
):
# print("message_processing")
await log("message_processing")
await answer_to_message(message, bot)
@ -92,8 +112,6 @@ async def message_processing(message: types.Message, bot: Bot):
await answer_to_message(message, bot)
router = Router()
# Если сообщение содержит текст то вызывается функция message_processing
router.message.register(message_processing, F.text)
router.message.register(message_processing, F.text)

View File

@ -3,4 +3,4 @@
"description": "Moderation commands for OCAB",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -1,12 +1,15 @@
# flake8: noqa
import asyncio
import aiohttp
import aiogram
import time
import aiogram
import aiohttp
from ...standard.config.config import *
from ...standard.roles.roles import *
class Moderation:
def __init__(self):
access_rights = get_access_rights()
@ -19,24 +22,24 @@ class Moderation:
async def time_to_seconds(time):
# Конвертация текстового указания времени по типу 3h, 5m, 10s в минуты
if time[-1] == 'd':
if time[-1] == "d":
return int(time[:-1]) * 86400
elif time[-1] == 'h':
elif time[-1] == "h":
return int(time[:-1]) * 3600
elif time[-1] == 'm':
elif time[-1] == "m":
return int(time[:-1]) * 60
elif time[-1] == 's':
elif time[-1] == "s":
return int(time[:-1])
async def short_time_to_time(self, time):
# Конвертация времени в длинное название
if time[-1] == 'd':
if time[-1] == "d":
return str(f"{time[0:-1]} дней")
elif time[-1] == 'h':
elif time[-1] == "h":
return str(f"{time[0:-1]} часов")
elif time[-1] == 'm':
elif time[-1] == "m":
return str(f"{time[0:-1]} минут")
elif time[-1] == 's':
elif time[-1] == "s":
return str(f"{time[0:-1]} секунд")
async def delete_message(self, chat_id, message_id, bot: aiogram.Bot):
@ -45,6 +48,7 @@ class Moderation:
async def ban_user(self, chat_id, user_id, bot: aiogram.Bot):
await bot.ban_chat_member(chat_id, user_id)
async def mute_user(chat_id, user_id, time, bot: aiogram.Bot):
mutePermissions = {
"can_send_messages": False,
@ -60,16 +64,19 @@ async def mute_user(chat_id, user_id, time, bot: aiogram.Bot):
"can_change_info": False,
"can_invite_users": False,
"can_pin_messages": False,
"can_manage_topics": False
"can_manage_topics": False,
}
end_time = time + int(time.time())
await bot.restrict_chat_member(chat_id, user_id, until_date=end_time, **mutePermissions)
await bot.restrict_chat_member(
chat_id, user_id, until_date=end_time, **mutePermissions
)
async def unmute_user(chat_id, user_id, bot: aiogram.Bot):
await bot.restrict_chat_member(chat_id, user_id, use_independent_chat_permissions=True)
await bot.restrict_chat_member(
chat_id, user_id, use_independent_chat_permissions=True
)
async def ban_user(chat_id, user_id, bot: aiogram.Bot):
await bot.ban_chat_member(chat_id, user_id)

View File

@ -3,4 +3,4 @@
"description": "Модуль для работы с ролями",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -1,7 +1,9 @@
from ..database.db_api import get_user_role
from ..config.config import get_config
from ..database.db_api import get_user_role
yaml_load = get_config()
class Roles:
user = "USER"
moderator = "MODERATOR"

View File

@ -4,4 +4,4 @@ ROLES:
ADMIN: 0
MODERATOR: 1
USER: 2
BOT: 3
BOT: 3

View File

@ -1,6 +1,7 @@
import os
import unittest
from ...database.db_api import create_tables, connect_database, add_user
from ...database.db_api import add_user, connect_database, create_tables
from ..roles import Roles
@ -14,10 +15,39 @@ class TestRoles(unittest.IsolatedAsyncioTestCase):
cls.database, cls.path = connect_database(is_test=True, module="roles")
create_tables(cls.database)
add_user(user_id=1, user_name="TestUser1", user_tag="TestTag1", user_role=0, user_stats=30, user_rep=15)
add_user(user_id=2, user_name="TestUser3", user_tag="TestTag3", user_role=1, user_stats=30, user_rep=15)
add_user(user_id=3, user_name="TestUser4", user_tag="TestTag4", user_role=2, user_stats=30, user_rep=15)
add_user(user_id=4, user_name="TestUser2", user_tag="TestTag2", user_role=3, user_stats=30, user_rep=15)
add_user(
user_id=1,
user_name="TestUser1",
user_tag="TestTag1",
user_role=0,
user_stats=30,
user_rep=15,
)
add_user(
user_id=2,
user_name="TestUser3",
user_tag="TestTag3",
user_role=1,
user_stats=30,
user_rep=15,
)
add_user(
user_id=3,
user_name="TestUser4",
user_tag="TestTag4",
user_role=2,
user_stats=30,
user_rep=15,
)
add_user(
user_id=4,
user_name="TestUser2",
user_tag="TestTag2",
user_role=3,
user_stats=30,
user_rep=15,
)
async def test_check_admin_permission(cls):
cls.assertTrue(await cls.roles.check_admin_permission(1))
cls.assertFalse(await cls.roles.check_admin_permission(2))
@ -32,7 +62,9 @@ class TestRoles(unittest.IsolatedAsyncioTestCase):
async def test_get_role_name(cls):
cls.assertEqual(await cls.roles.get_role_name(cls.roles.admin_role_id), "ADMIN")
cls.assertEqual(await cls.roles.get_role_name(cls.roles.moderator_role_id), "MODERATOR")
cls.assertEqual(
await cls.roles.get_role_name(cls.roles.moderator_role_id), "MODERATOR"
)
cls.assertEqual(await cls.roles.get_role_name(cls.roles.user_role_id), "USER")
cls.assertEqual(await cls.roles.get_role_name(cls.roles.bot_role_id), "BOT")
with cls.assertRaises(ValueError):
@ -44,7 +76,5 @@ class TestRoles(unittest.IsolatedAsyncioTestCase):
os.system(f"rm {cls.path}")
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@ -1,13 +1,19 @@
# flake8: noqa
import asyncio
import random
from threading import Thread
from aiogram import Bot
from aiogram.types import Message
from src.modules.standard.config.config import get_telegram_check_bot
from src.modules.standard.roles.roles import Roles
from src.modules.standard.database.db_api import *
from src.modules.standard.moderation.moderation import mute_user, unmute_user, ban_user
from aiogram.utils.keyboard import InlineKeyboardBuilder
from aiogram.types import inline_keyboard_button as types
import random, asyncio
from threading import Thread
from aiogram.utils.keyboard import InlineKeyboardBuilder
from src.modules.standard.config.config import get_telegram_check_bot
from src.modules.standard.database.db_api import *
from src.modules.standard.moderation.moderation import ban_user, mute_user, unmute_user
from src.modules.standard.roles.roles import Roles
async def create_math_task():
first_number = random.randint(1, 100)
@ -22,6 +28,7 @@ async def create_math_task():
random.shuffle(fake_answers)
return [answer, first_number, second_number, fake_answers]
async def ban_user_timer(chat_id: int, user_id: int, time: int, bot: Bot):
await asyncio.sleep(time)
if get_user(user_id) is not None:
@ -30,8 +37,6 @@ async def ban_user_timer(chat_id: int, user_id: int, time: int, bot: Bot):
await ban_user()
async def check_new_user(message: Message, bot: Bot):
print("check_new_user")
if get_telegram_check_bot():
@ -39,7 +44,10 @@ async def check_new_user(message: Message, bot: Bot):
if get_user(message.from_user.id) is None:
# Выдаём пользователю ограничение на отправку сообщений на 3 минуты
ban_task = Thread(target=ban_user_timer, args=(message.chat.id, message.from_user.id, 180, bot))
ban_task = Thread(
target=ban_user_timer,
args=(message.chat.id, message.from_user.id, 180, bot),
)
ban_task.start()
# Создаём задачу с отложенным выполнением на 3 минуты
@ -48,27 +56,31 @@ async def check_new_user(message: Message, bot: Bot):
builder = InlineKeyboardBuilder()
for answer in math_task[3]:
if answer == math_task[0]:
builder.add(types.InlineKeyboardButton(
text=answer,
callback_data=f"check_math_task_true")
builder.add(
types.InlineKeyboardButton(
text=answer, callback_data=f"check_math_task_true"
)
)
else:
builder.add(types.InlineKeyboardButton(
text=answer,
callback_data=f"check_math_task_false")
builder.add(
types.InlineKeyboardButton(
text=answer, callback_data=f"check_math_task_false"
)
)
await message.reply(
f"Приветствую, {message.from_user.first_name}!\n"
f"Для продолжения работы с ботом, пожалуйста, решите математический пример в течении 3х минут:\n"
f"*{text}*",
reply_markup=builder.as_markup()
reply_markup=builder.as_markup(),
)
async def math_task_true(message: Message, bot: Bot):
await message.reply(f"Верно! Добро пожаловать в чат {message.from_user.first_name}")
await unmute_user(message.chat.id, message.from_user.id, bot)
add_user(message.from_user.id,
message.from_user.first_name + ' ' + message.from_user.last_name,
message.from_user.username)
add_user(
message.from_user.id,
message.from_user.first_name + " " + message.from_user.last_name,
message.from_user.username,
)
pass

View File

@ -3,4 +3,4 @@
"description": "Мо",
"author": "OCAB Team",
"version": "1.0"
}
}

View File

@ -1,4 +1,4 @@
from aiogram import Router, F
from aiogram import F, Router
from src.modules.standard.welcome.handlers import check_new_user
@ -7,4 +7,6 @@ router = Router()
# Если в чат пришел новый пользователь
router.message.register(check_new_user, F.new_chat_members.exists())
# Ловин колбеки от кнопок с callback_data=f"check_math_task_true"
router.callback_query.register(check_new_user, F.callback_data == "check_math_task_true")
router.callback_query.register(
check_new_user, F.callback_data == "check_math_task_true"
)

View File

@ -9,13 +9,14 @@ class Path:
modules_standard: str
modules_custom: str
def _get_paths(path_to_json: str):
with open(path_to_json, encoding="utf8") as f:
paths = loads(f.read())
return Path(
core=paths["core"],
modules_standard=paths["modules standard"],
modules_custom=paths["modules custom"]
modules_custom=paths["modules custom"],
)