This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
moderator-bot/handlers/users/user.py

160 lines
4.6 KiB
Python
Raw Normal View History

2023-02-18 08:16:54 -05:00
from load import dp, bot, types
2022-11-06 03:48:10 -05:00
from database import Member, Restriction
2022-08-07 04:45:35 -04:00
import config
2023-02-18 08:16:54 -05:00
from aiogram.utils.keyboard import InlineKeyboardBuilder, ReplyKeyboardBuilder
from aiogram.fsm.context import FSMContext
from aiogram.types import ReplyKeyboardRemove
from aiogram.fsm.state import State, StatesGroup
2022-08-07 04:45:35 -04:00
2023-02-18 08:16:54 -05:00
from aiogram import F
from aiogram.filters import Command
from filters import ChatTypeFilter
2022-08-07 04:45:35 -04:00
2023-02-18 08:16:54 -05:00
class ReportRestriction(StatesGroup):
state1 = State()
state2 = State()
2023-01-22 05:27:20 -05:00
2023-02-18 08:16:54 -05:00
@dp.message(
Command("start", "help"),
ChatTypeFilter("private")
2023-01-22 05:27:20 -05:00
)
async def start_command_private(message: types.Message):
2023-02-18 08:16:54 -05:00
bot_description_menu = ReplyKeyboardBuilder()
bot_description_menu.button(text="Check restrictions")
bot_description_menu.button(text="About Us")
2022-08-07 04:45:35 -04:00
await message.answer((
2022-11-06 03:48:10 -05:00
f"Hi, **{message.from_user.first_name}**!\n"
2022-08-16 06:28:08 -04:00
"My commands:\n"
2023-01-22 05:27:20 -05:00
"\t\t/help /start - read this message."
2023-02-18 08:16:54 -05:00
), reply_markup=bot_description_menu.as_markup(resize_keyboard=True))
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
2023-02-18 08:16:54 -05:00
@dp.message(F.text == "About Us")
2023-01-22 05:27:20 -05:00
async def about_us(message: types.Message):
2022-08-07 04:45:35 -04:00
await message.answer((
"Moderator bot - an open source project for managing a Telegram group.\n\n"
"Possibilities:\n"
"1. Role system\n"
"2. Simple commands such as !ban, !mute\n"
"3. Convenient sticker/photo disabling with !stickers, !media\n"
"4. Users can report admins.\n"
"5. Admins can give warnings to users.\n"
2023-02-18 08:16:54 -05:00
"\nRelease version: 2.5.2\n"
"[Github](https://github.com/hok7z/moderator-bot)")
2022-08-07 04:45:35 -04:00
)
2023-02-18 08:16:54 -05:00
@dp.message(F.text == "Check restrictions")
async def check_for_restrict(message: types.Message, state: FSMContext):
await state.set_state(ReportRestriction.state1)
2022-11-06 03:48:10 -05:00
user = Member.get(Member.user_id == message.from_user.id)
2022-12-01 05:54:32 -05:00
restrictions = Restriction.select().where(Restriction.to_user == user)
2023-02-18 08:16:54 -05:00
2022-11-06 03:48:10 -05:00
if (not restrictions):
2022-08-07 04:45:35 -04:00
await message.answer("✅No restrictions.")
return
for restriction in restrictions:
2023-02-18 08:16:54 -05:00
markup = InlineKeyboardBuilder()
2022-08-07 04:45:35 -04:00
2023-02-18 08:16:54 -05:00
markup.button(
text="✉️ Report restriction",
callback_data=restriction.id
2023-01-22 05:27:20 -05:00
)
from_user = restriction.from_user
to_user = restriction.to_user
2022-12-01 05:54:32 -05:00
2023-01-22 05:27:20 -05:00
await message.answer((
"Restriction #{}\n"
"from user [{}](tg://user?id={})\n"
"to user [{}](tg://user?id={})\n"
"Note: {}\n"
"{}\n"
2023-02-18 08:16:54 -05:00
).format(
restriction.id,
from_user.first_name,
from_user.user_id,
to_user.first_name,
to_user.user_id,
restriction.text,
restriction.timestamp
), reply_markup=markup.as_markup())
await state.set_state(ReportRestriction.state1)
2023-01-22 05:27:20 -05:00
2022-08-07 04:45:35 -04:00
2023-02-18 08:16:54 -05:00
@dp.callback_query(ReportRestriction.state1)
async def report_restriction(call: types.CallbackQuery, state: FSMContext):
2022-08-07 04:45:35 -04:00
await call.answer(cache_time=60)
2023-01-22 05:27:20 -05:00
callback_data = call.data
2023-02-18 08:16:54 -05:00
restriction_id = int(callback_data)
2022-11-06 03:48:10 -05:00
2023-02-18 08:16:54 -05:00
cancel_markup = ReplyKeyboardBuilder()
cancel_markup.button(text="❌ Cancel")
2022-08-07 04:45:35 -04:00
await state.update_data(restriction_id=restriction_id)
2023-02-18 08:16:54 -05:00
await call.message.answer(
"Please,enter your report",
reply_markup=cancel_markup.as_markup(resize_keyboard=True)
)
2022-11-06 03:48:10 -05:00
2023-02-18 08:16:54 -05:00
await state.set_state(ReportRestriction.state2)
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
2023-02-18 08:16:54 -05:00
@dp.message(ReportRestriction.state2)
2023-01-22 05:27:20 -05:00
async def get_message_report(message: types.Message, state: FSMContext):
2022-08-07 04:45:35 -04:00
answer = message.text
2022-11-06 03:48:10 -05:00
2022-08-07 04:45:35 -04:00
if not ("Cancel" in answer):
2022-11-06 03:48:10 -05:00
data = await state.get_data()
2023-01-22 05:27:20 -05:00
restriction_id = data.get("restriction_id")
2022-12-01 05:54:32 -05:00
restriction = Restriction.get(id=restriction_id)
2022-08-07 04:45:35 -04:00
2023-01-22 05:27:20 -05:00
from_user = restriction.from_user
to_user = restriction.to_user
2022-08-07 04:45:35 -04:00
2023-02-18 08:16:54 -05:00
restriction_timestamp = restriction.timestamp.strftime("%d.%m.%y at %H:%M")
2023-01-22 05:27:20 -05:00
await bot.send_message(config.second_group_id, (
2023-02-18 08:16:54 -05:00
"Report on restriction #{}\n"
"Complaint from: [{}](tg://user?id={})\n"
"Complaint about: [{}](tg://user?id={})\n"
"Sent {}\n"
"{}\n"
"Message: {}"
).format(
restriction_id,
from_user.first_name,
from_user.user_id,
to_user.first_name,
to_user.user_id,
restriction.text,
restriction_timestamp,
answer,
))
2022-12-01 05:54:32 -05:00
2023-01-22 05:27:20 -05:00
await message.answer(
"Report restriction sended",
reply_markup=ReplyKeyboardRemove()
)
2022-08-07 04:45:35 -04:00
else:
2023-01-22 05:27:20 -05:00
await message.answer(
"Operation cancaled",
reply_markup=ReplyKeyboardRemove()
)
2022-08-07 04:45:35 -04:00
2023-02-18 08:16:54 -05:00
await state.clear()