Refactoring: step3
- вся работа с shelve унесена в report - вся работа с БД унесена в моделс (крме команд админа) - ид книги лутца и звука вынесены в константы - исправлен баг в проверке на смешанный алфавит при определении спама, добавлены константы для более точного выявления - написаны новые тесты в том числе на цитаты и действия админов
This commit is contained in:
+8
-11
@@ -3,21 +3,21 @@ import asyncio
|
||||
import html
|
||||
import random
|
||||
|
||||
from sqlalchemy import delete
|
||||
from telebot import types
|
||||
|
||||
from .filters import is_white_id
|
||||
from .helpers import is_nongrata
|
||||
from .models import Quote, session
|
||||
from .models import all_chat_quotes, add_quote, is_quote_in_chat, delete_quote_in_chat
|
||||
from .robot import bot
|
||||
|
||||
TYPING_TIMEOUT = 0.13 / 4 # Reading time is quarter of the same text writing time
|
||||
|
||||
|
||||
def speak(chance_of, group_id):
|
||||
def speak(chance_of: int, chat_id: int) -> None | str:
|
||||
number = random.randint(0, chance_of)
|
||||
if number == 0:
|
||||
return random.choice([i[0] for i in session.query(Quote.text).filter(Quote.chat_id == group_id).all()])
|
||||
return random.choice([i[0] for i in all_chat_quotes(chat_id)])
|
||||
return None
|
||||
|
||||
|
||||
async def send_quote(after_sec, message, quote):
|
||||
@@ -39,9 +39,8 @@ async def act(message: types.Message):
|
||||
async def add_stan_quote(message: types.Message):
|
||||
if message.reply_to_message and message.reply_to_message.text:
|
||||
quote = message.reply_to_message.text
|
||||
if quote not in [i[0] for i in session.query(Quote.text).filter(Quote.chat_id == message.chat.id).all()]:
|
||||
session.add(Quote(chat_id=message.chat.id, text=quote.replace("\n", " ")))
|
||||
session.commit()
|
||||
if quote not in {i[0] for i in all_chat_quotes(message.chat.id)}:
|
||||
add_quote(message.chat.id, quote.replace("\n", " "))
|
||||
await bot.send_message(message.chat.id, "➕\n └ " + quote.replace("\n", " "), parse_mode='Markdown')
|
||||
await bot.delete_message(message.chat.id, message.id)
|
||||
else:
|
||||
@@ -52,10 +51,8 @@ async def add_stan_quote(message: types.Message):
|
||||
async def remove_stan_quote(message: types.Message):
|
||||
if message.reply_to_message and message.reply_to_message.text:
|
||||
quote = message.reply_to_message.text
|
||||
already_exist = session.query(Quote.text).filter_by(text=quote, chat_id=message.chat.id).first()
|
||||
if already_exist:
|
||||
session.execute(delete(Quote).filter_by(text=quote, chat_id=message.chat.id))
|
||||
session.commit()
|
||||
if is_quote_in_chat(quote, message.chat.id):
|
||||
delete_quote_in_chat(quote, message.chat.id)
|
||||
await bot.send_message(message.chat.id, f"➖ \n └ {quote}", parse_mode='Markdown')
|
||||
await bot.delete_message(message.chat.id, message.id)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user