vk_chat_bot/handlers/user.py

156 lines
6.2 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 vkbottle import bold, italic, API
from vkbottle.bot import Message
import database
from messages import *
from labeler import labeler
# top_users - массив sqlite3.Row с двумя столбцами: user_id и value
async def format_rating(top_users: List[Any], api: API) -> str:
top_user_ids = [user['user_id'] for user in top_users]
users_info = await api.users.get(user_ids=top_user_ids)
result = ''
i = 1
for user in top_users:
for info in users_info:
if info.id == user['user_id']:
result += '{}. {} {} - {}\n'.format(i, info.first_name, info.last_name, user['value'])
i = i + 1
break
return result
def calculate_total_messages(top_users: List[Any]) -> int:
total = 0
for user in top_users:
total += user['value']
return total
# noinspection SpellCheckingInspection
@labeler.chat_message(text="!помощь")
async def rules_handler(message: Message):
chat_id = message.peer_id
chat = database.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 += '\n'
response += italic('Команды с пометкой * нужно вызывать в ответном сообщении.')
await message.answer(response)
@labeler.chat_message(text="!сегодня")
async def stats_today_handler(message: Message):
chat_id = message.peer_id
chat = database.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(top_users, message.ctx_api)
await message.answer(response)
@labeler.chat_message(text="!месяц")
async def stats_month_handler(message: Message):
chat_id = message.peer_id
chat = database.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(top_users, message.ctx_api)
await message.answer(response)
@labeler.chat_message(text="!молчуны")
async def silent_handler(message: Message):
chat_id = message.peer_id
chat = database.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_silent(chat_id, 14)
if len(top_users) == 0:
await message.answer('Молчунов нет. Все молодцы!')
return
response = bold('Молчуны чата') + ' (не писали более 14 дней)\n'
response += await format_rating(top_users, message.ctx_api)
await message.answer(response)
@labeler.chat_message(text="!предупреждения")
async def warnings_handler(message: Message):
chat_id = message.peer_id
chat = database.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(top_users, message.ctx_api)
await message.answer(response)
@labeler.chat_message(text="!поздравление")
async def no_birthday_handler(message: Message):
chat_id = message.peer_id
chat = database.create_chat_if_not_exists(chat_id)
if chat['active'] == 0:
await message.answer(MESSAGE_CHAT_NOT_ACTIVE)
return
user_id = message.from_id
user = database.create_user_if_not_exists(chat_id, user_id)
happy_birthday = 1 if user['happy_birthday'] == 0 else 0
database.DB.user_toggle_happy_birthday(chat_id, user_id, happy_birthday)
if happy_birthday == 1:
await message.answer('Хорошо, я буду поздравлять тебя с днем рождения, если его дата не скрыта.')
else:
await message.answer('Хорошо, я не буду поздравлять тебя с днем рождения.')