chat_moderator_bot/handlers/group.py
2024-07-17 23:35:45 +03:00

92 lines
2.3 KiB
Python

from aiogram import Router, F
from aiogram.fsm.context import FSMContext
from aiogram.types import Message
from templates.message import send_preview
from utils.db import Redis, Postgres
from utils.defs import delete_msg
router = Router()
@router.message(F.chat.func(lambda chat: chat.type in ('group', 'supergroup')))
async def get_all_messages(msg: Message, state: FSMContext):
"""
Ловит все сообщения в чате
:param msg: Message
:param state: FSMContext
:return:
"""
ban = False
msg_text = msg.text
if not msg_text:
msg_text = msg.caption
r = Redis()
p = Postgres()
if msg_text:
ban_words = await r.get_list(
key='ban_words'
)
if not ban_words:
ban_words = await p.get_data(
table_name='ban_words'
)
ban_words = [word['word'] for word in ban_words]
await r.delete_key(
'ban_words'
)
await r.update_list(
'ban_words',
*ban_words
)
for ban_word in ban_words:
if ban_word in msg_text.lower():
ban = True
else:
ban_media = await r.get_dict(
key='ban_media'
)
print(ban_media)
if not ban_media.get('video') or ban_media.get('photo'):
postgres_ban_media = await p.get_data(
table_name='ban_media'
)
ban_media = {}
for key, value in postgres_ban_media[0].items():
ban_media[key] = 'yes' if value else ''
await r.update_dict(
'ban_media',
value=ban_media
)
if ban_media.get('video') and msg.video:
ban = True
if ban_media.get('photo') and msg.photo:
ban = True
if ban:
await delete_msg(
msg=msg
)
message_data = await p.get_data(
table_name='message'
)
if msg.from_user.username:
username = f'@{msg.from_user.username}'
else:
username = msg.from_user.full_name
if message_data[0]['included']:
await send_preview(
chat_id=msg.chat.id,
message_data=message_data[0],
username=username
)