vk_chat_bot/tg/handlers/admin.py

103 lines
3.5 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 aiogram import Bot, Router, F
from aiogram.types import Message
from aiogram.utils.formatting import Bold
import utils
from messages import *
import tg.tg_database as database
router = Router()
async def tg_user_is_admin(chat_id: int, user_id: int, bot: Bot):
for admin in await bot.get_chat_administrators(chat_id=chat_id):
if admin.user.id == user_id:
return True
return False
@router.message(F.text == "!старт")
async def start_handler(message: Message, bot: Bot):
chat_id = message.chat.id
database.DB.create_chat_if_not_exists(chat_id)
if not await tg_user_is_admin(chat_id, message.from_user.id, bot):
await message.answer(MESSAGE_PERMISSION_DENIED)
return
database.DB.chat_update(chat_id, active=1)
await message.answer('Готова к работе!')
@router.message(F.text == "!правила")
async def rules_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
if message.reply_to_message is None:
if chat['rules'] is not None:
response = Bold('Правила чата') + '\n' + chat['rules']
await message.answer(**response.as_kwargs())
else:
await message.answer(MESSAGE_DEFAULT_RULES)
else:
if not await tg_user_is_admin(chat_id, message.from_user.id, bot):
await message.answer(MESSAGE_PERMISSION_DENIED)
return
database.DB.chat_update(chat_id, rules=message.reply_to_message.text)
await message.answer('Правила чата изменены.')
@router.message(F.text == "!приветствие")
async def set_greeting_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
if not await tg_user_is_admin(chat_id, message.from_user.id, bot):
await message.answer(MESSAGE_PERMISSION_DENIED)
return
if message.reply_to_message is None:
await message.answer(MESSAGE_NEED_REPLY)
return
database.DB.chat_update(chat_id, greeting_join=message.reply_to_message.text)
await message.answer('Приветствие изменено.')
@router.message(F.text == "!предупреждение")
async def warning_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
if not await tg_user_is_admin(chat_id, message.from_user.id, bot):
await message.answer(MESSAGE_PERMISSION_DENIED)
return
if message.reply_to_message is None:
await message.answer(MESSAGE_NEED_REPLY)
return
user_id = message.reply_to_message.from_user.id
database.DB.create_user_if_not_exists(chat_id, user_id)
database.DB.user_increment_warnings(chat_id, user_id)
user = database.DB.get_user(chat_id, user_id)
user_info = message.reply_to_message.from_user
# TODO: родительный падеж имени и фамилии, если возможно
await message.answer('У {} {} {}.'.format(
utils.full_name(user_info.first_name, user_info.last_name),
user['warnings'],
utils.make_word_agree_with_number(user['warnings'], 'предупреждение'))
)