replace-bot/handlers/groups/main.py
2022-10-09 20:18:58 +03:00

82 lines
2.8 KiB
Python

import logging
import io
import base64
from aiogram import types
from aiogram.dispatcher.filters import ChatTypeFilter
from load import dp, bot, config
from database import set_group_settings, get_group
from parser import get_about_replacements
from database import register
@dp.message_handler(ChatTypeFilter(['group', 'supergroup']), commands=['set'])
async def set_group(message: types.Message):
if (message.from_user.id not in [admin.user.id for admin in await bot.get_chat_administrators(message.chat.id)]) and (message.from_user.id not in config.admin_user):
await message.answer("Вы не являетесь администратором чата!")
return
args = message.text.split()
if len(args) < 2:
await message.answer(
("Вы не передали имя своей группы!\n"
"Пример: /set 221")
)
return
set_group_settings(message.chat.id, args[1])
await message.answer("Настройка завершена успешно!")
@dp.message_handler(ChatTypeFilter(['group', 'supergroup']), commands=['start', 'get'])
async def get_replace_on_chat(message: types.Message):
if config.logging_user:
register(
user_id=message.from_user.id,
username=message.from_user.username,
first_name=str(message.from_user.first_name),
last_name=message.from_user.last_name
)
logging.info("User: {user_id} - {username}".format(
user_id=str(message.from_user.id),
username=str(message.from_user.username)
))
try:
data = get_about_replacements()
if 'image' in data:
await bot.send_photo(
message.chat.id,
io.BytesIO(
base64.b64decode(
data["data"]['all']
)
),
parse_mode="Markdown",
)
return
except Exception as e:
logging.error(e)
await bot.send_message(message.chat.id, "Error")
return
group = get_group(message.chat.id)
if group is not None:
if group in data['data']:
await message.answer(
(
"Группа: {group}\n"
"Замены {date}\n"
"{teacher}\n"
"Замены: {replace}\n"
).format(
group=str(group),
replace=data['data'][group],
date=data["date"].lower(),
teacher=data["another_teacher"]
)
)
else:
await message.answer("Похоже замен нет")
else:
await message.answer("Похоже администратор группы не настроил привязку")