<reponame>vitaliy-ukiru/math-bot<gh_stars>1-10
# Source: https://github.com/aiogram/bot/blob/master/aiogram_bot/handlers/simple_admin.py

import logging

from aiogram import types
from aiogram.utils import exceptions
from babel.dates import format_timedelta

from app.loader import dp
from app.utils.timedelta import parse_timedelta_from_message


logger = logging.getLogger()


@dp.message_handler(
    commands=("ro", "mute"),
    commands_prefix="/!",
    is_reply=True,
    user_can_restrict_members=True,
    bot_can_restrict_members=True,
)
async def cmd_ro(message: types.Message, reply: types.Message):
    duration = await parse_timedelta_from_message(message)
    if not duration:
        return

    try:  # Apply restriction
        await message.chat.restrict(
            reply.from_user.id, can_send_messages=False, until_date=duration
        )
        logger.info(
            "User {user} restricted by {admin} for {duration}".format(
                user=message.reply_to_message.from_user.id,
                admin=message.from_user.id,
                duration=duration,
            )
        )
    except exceptions.BadRequest as e:
        logger.error("Failed to restrict chat member: {error!r}", error=e)
        return False

    await message.reply_to_message.answer(
        "<b>Read-only</b> активирован для пользователя {user}. Длительность: {duration}".format(
            user=message.reply_to_message.from_user.get_mention(),
            duration=format_timedelta(
                duration, locale=message.from_user.locale, granularity="seconds", format="short"
            ),
        )
    )
    return True


@dp.message_handler(
    commands="ban",
    commands_prefix="/!",
    is_reply=True,
    user_can_restrict_members=True,
    bot_can_restrict_members=True,
)
async def cmd_ban(message: types.Message, reply: types.Message):
    duration = await parse_timedelta_from_message(message)
    if not duration:
        return

    try:  # Apply restriction
        await message.bot.ban_chat_member(message.chat.id, reply.from_user.id, until_date=duration)

        logger.info(
            "User {user} baned by {admin} for {duration}".format(
                user=message.reply_to_message.from_user.id,
                admin=message.from_user.id,
                duration=duration,)
        )
    except exceptions.BadRequest as e:
        logger.error("Failed to kick chat member: {error!r}", error=e)
        return False

    await message.reply_to_message.answer(
        "Пользователь {user} <b>забанен</b> на {duration}".format(
            user=message.reply_to_message.from_user.get_mention(),
            duration=format_timedelta(
                duration, locale=message.from_user.locale, granularity="seconds", format="short"
            ),
        )
    )
    return True

# Source: https://github.com/aiogram/bot/blob/master/aiogram_bot/handlers/simple_admin.py

import logging

from aiogram import types
from aiogram.utils import exceptions
from babel.dates import format_timedelta

from app.loader import dp
from app.utils.timedelta import parse_timedelta_from_message


logger = logging.getLogger()


@dp.message_handler(
    commands=("ro", "mute"),
    commands_prefix="/!",
    is_reply=True,
    user_can_restrict_members=True,
    bot_can_restrict_members=True,
)
async def cmd_ro(message: types.Message, reply: types.Message):
    duration = await parse_timedelta_from_message(message)
    if not duration:
        return

    try:  # Apply restriction
        await message.chat.restrict(
            reply.from_user.id, can_send_messages=False, until_date=duration
        )
        logger.info(
            "User {user} restricted by {admin} for {duration}".format(
                user=message.reply_to_message.from_user.id,
                admin=message.from_user.id,
                duration=duration,
            )
        )
    except exceptions.BadRequest as e:
        logger.error("Failed to restrict chat member: {error!r}", error=e)
        return False

    await message.reply_to_message.answer(
        "<b>Read-only</b> активирован для пользователя {user}. Длительность: {duration}".format(
            user=message.reply_to_message.from_user.get_mention(),
            duration=format_timedelta(
                duration, locale=message.from_user.locale, granularity="seconds", format="short"
            ),
        )
    )
    return True


@dp.message_handler(
    commands="ban",
    commands_prefix="/!",
    is_reply=True,
    user_can_restrict_members=True,
    bot_can_restrict_members=True,
)
async def cmd_ban(message: types.Message, reply: types.Message):
    duration = await parse_timedelta_from_message(message)
    if not duration:
        return

    try:  # Apply restriction
        await message.bot.ban_chat_member(message.chat.id, reply.from_user.id, until_date=duration)

        logger.info(
            "User {user} baned by {admin} for {duration}".format(
                user=message.reply_to_message.from_user.id,
                admin=message.from_user.id,
                duration=duration,)
        )
    except exceptions.BadRequest as e:
        logger.error("Failed to kick chat member: {error!r}", error=e)
        return False

    await message.reply_to_message.answer(
        "Пользователь {user} <b>забанен</b> на {duration}".format(
            user=message.reply_to_message.from_user.get_mention(),
            duration=format_timedelta(
                duration, locale=message.from_user.locale, granularity="seconds", format="short"
            ),
        )
    )
    return True
# Source: https://github.com/aiogram/bot/blob/master/aiogram_bot/handlers/simple_admin.py # Apply restriction # Apply restriction