vk_chat_bot/tg/handlers/user.py

159 lines
6.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import List, Any
from aiogram import Bot, Router, F
from aiogram.enums import ContentType
from aiogram.types import Message
from aiogram.utils.formatting import Bold, Italic
import utils
from messages import *
import tg.tg_database as database
from .default import get_ai_reply
router = Router()
# top_users - массив sqlite3.Row с двумя столбцами: user_id и value
async def format_rating(chat_id: int, top_users: List[Any], bot: Bot) -> str:
result = ''
i = 1
for user in top_users:
info = await bot.get_chat_member(chat_id, user['user_id'])
result += '{}. {} - {}\n'.format(i, utils.full_name(info.user.first_name, info.user.last_name), user['value'])
i = i + 1
return result
def calculate_total_messages(top_users: List[Any]) -> int:
total = 0
for user in top_users:
total += user['value']
return total
@router.message(F.text == '!помощь')
async def help_handler(message: Message):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
response = Bold('Команды для всех') + '\n'
response += '!правила - вывести правила\n'
response += '!сегодня - статистика сообщений за сегодня\n'
response += '!месяц - статистика сообщений за месяц\n'
response += '!молчуны - список молчунов\n'
response += '!предупреждения - список участников с предупреждениями\n'
response += '!проверка* - проверить сообщения на нарушение правил\n'
response += '\n'
response += Bold('Команды для администраторов') + '\n'
response += '!старт - начать работу в чате\n'
response += '!правила* - изменить правила\n'
response += '!приветствие* - изменить приветствие новичков\n'
response += '!личность* - изменить описание личности ИИ\n'
response += '!предупреждение* - выдать предупреждение участнику\n'
response += '\n'
response += Italic('Команды с пометкой * нужно вызывать в ответном сообщении.')
await message.answer(**response.as_kwargs())
@router.message(F.text == "!сегодня")
async def stats_today_handler(message: Message, bot: Bot):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
top_users = database.DB.get_top_messages_today(chat_id)
if len(top_users) == 0:
await message.answer('Сегодня еще никто не писал.')
return
response = Bold('Статистика за сегодня') + '\n'
response += 'Всего сообщений - {}\n'.format(calculate_total_messages(top_users))
response += await format_rating(chat_id, top_users, bot)
await message.answer(**response.as_kwargs())
@router.message(F.text == "!месяц")
async def stats_month_handler(message: Message, bot: Bot):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
top_users = database.DB.get_top_messages_month(chat_id)
if len(top_users) == 0:
await message.answer('В этом месяце еще никто не писал.')
return
response = Bold('Статистика за месяц') + '\n'
response += 'Всего сообщений - {}\n'.format(calculate_total_messages(top_users))
response += await format_rating(chat_id, top_users, bot)
await message.answer(**response.as_kwargs())
@router.message(F.text == "!молчуны")
async def silent_handler(message: Message, bot: Bot):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
top_users = database.DB.get_top_messages_today(chat_id)
if len(top_users) == 0:
await message.answer('Молчунов нет. Все молодцы!')
return
response = Bold('Молчуны чата') + ' (не писали более 14 дней)\n'
response += await format_rating(chat_id, top_users, bot)
await message.answer(**response.as_kwargs())
@router.message(F.text == "!предупреждения")
async def warnings_handler(message: Message, bot: Bot):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
top_users = database.DB.get_top_warnings(chat_id)
if len(top_users) == 0:
await message.answer('Пока все спокойно. Продолжайте в том же духе.')
return
response = Bold('Участники с предупреждениями') + '\n'
response += await format_rating(chat_id, top_users, bot)
await message.answer(**response.as_kwargs())
@router.message(F.text == "!проверка")
async def check_rules_violation_handler(message: Message):
chat_id = message.chat.id
chat = database.DB.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
if message.reply_to_message is None or message.reply_to_message.content_type != ContentType.TEXT:
await message.answer(MESSAGE_NEED_REPLY)
return
chat_rules = chat['rules']
if chat_rules is None:
await message.answer(MESSAGE_DEFAULT_CHECK_RULES)
return
prompt = 'В чате действуют следующие правила:\n'
prompt += chat_rules + '\n\n'
prompt += 'Проверь, не нарушают ли правила следующие сообщения (если нарушают, то укажи пункты правил):'
await message.answer(await get_ai_reply(message.bot, chat_id, message.from_user, prompt,
message.reply_to_message.from_user, message.reply_to_message.text))