Bot-Moderator for Telegram chats. Administrator actions

mezgoodle

Maksym Zavalniuk

Posted on September 6, 2022

Bot-Moderator for Telegram chats. Administrator actions

What will we do? 😊

In this part, there will be no difficult programming information, but we will look at the functionality of the bots.

Let's write code 🖊️

Yes, we'll jump right into writing the code, namely some important handlers. Because we have already received all the necessary knowledge in previous posts.

So let's open the handlers folder and fill some files:

  • ban.py
from aiogram.types import Message, ChatType

from datetime import timedelta

from loader import dp


@dp.message_handler(is_admin=True, commands=['ban'], commands_prefix='!/',
                    chat_type=[ChatType.GROUP, ChatType.SUPERGROUP])
async def ban_user(message: Message):
    if not message.reply_to_message:
        await message.reply('This command needs to be as a reply to message')
        return
    await message.delete()

    await message.bot.ban_chat_member(chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id,
                                      until_date=timedelta(seconds=29))
    return await message.reply_to_message.reply('User has been banned')


@dp.message_handler(is_admin=True, commands=['unban'], commands_prefix='!/')
async def unban_user(message: Message):
    if not message.reply_to_message:
        await message.reply('This command needs to be as a reply to message')
        return
    await message.delete()

    await message.bot.unban_chat_member(chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id,
                                        only_if_banned=True)
    username = message.reply_to_message.from_user.username
    return await message.reply_to_message.reply(f'User @{username} has been unbanned')
Enter fullscreen mode Exit fullscreen mode

Let's understand what is written here. In the decorator, we write filters: our own, which command, the prefix to the command, and the type of chat. Next, in the body of the function, we check whether the message is a reply to another. Then the most interesting thing: ban the user with the aiogram method. And here the question may arise: "Why there is a time delta of 29 seconds?". For this, we need to refer to the Telegram API documentation:

Date when the user will be unbanned, unix time. If the user is banned for more than 366 days or less than 30 seconds from the current time they are considered to be banned forever. Applied for supergroups and channels only.

In our case, banning for 29 seconds means banning the user forever.

  • kick.py
from aiogram.types import Message, ChatType

from loader import dp

from datetime import timedelta


@dp.message_handler(is_admin=True, commands=['kick'], commands_prefix='!/',
                    chat_type=[ChatType.GROUP, ChatType.SUPERGROUP])
async def kick_user(message: Message):
    if not message.reply_to_message:
        return await message.reply('This command needs to be as a reply to message')
    await message.delete()

    user_id = message.reply_to_message.from_user.id
    seconds = 60
    await message.bot.ban_chat_member(chat_id=message.chat.id, user_id=user_id, until_date=timedelta(seconds=seconds))
    return await message.reply_to_message.reply(f'User has been kicked for the {seconds} seconds')
Enter fullscreen mode Exit fullscreen mode

Everything is the same here, but a kick differs from a ban in that the user can return.

  • mute.py
from aiogram.types import Message, chat_permissions, ChatType

from datetime import timedelta

from loader import dp


@dp.message_handler(is_admin=True, commands=['mute'], commands_prefix='!/',
                    chat_type=[ChatType.GROUP, ChatType.SUPERGROUP])
async def mute_user(message: Message):
    if not message.reply_to_message:
        return await message.reply('This command needs to be as a reply on message')
    await message.delete()

    user_id = message.reply_to_message.from_user.id
    seconds = 30

    await message.bot.restrict_chat_member(chat_id=message.chat.id, user_id=user_id,
                                           until_date=timedelta(seconds=seconds),
                                           permissions=chat_permissions.ChatPermissions(can_send_messages=False,
                                                                                        can_send_polls=False,
                                                                                        can_send_other_messages=False,
                                                                                        can_send_media_messages=False))
    return await message.reply_to_message.reply(f'User has been muted for the {seconds} seconds')


@dp.message_handler(is_admin=True, commands=['unmute'], commands_prefix='!/',
                    chat_type=[ChatType.GROUP, ChatType.SUPERGROUP])
async def unmute_user(message: Message):
    if not message.reply_to_message:
        return await message.reply('This command needs to be as a reply on message')
    await message.delete()

    user_id = message.reply_to_message.from_user.id
    await message.bot.restrict_chat_member(chat_id=message.chat.id, user_id=user_id,
                                           permissions=chat_permissions.ChatPermissions(can_send_messages=True,
                                                                                        can_send_polls=True,
                                                                                        can_send_other_messages=True,
                                                                                        can_send_media_messages=True))
    return await message.reply_to_message.reply('User has been unmuted')
Enter fullscreen mode Exit fullscreen mode

Everything is similar here, the only difference is the method we use.

  • admins.py
from aiogram.types import Message, ChatType

from loader import dp


@dp.message_handler(commands=['admins'], chat_type=[ChatType.GROUP, ChatType.SUPERGROUP])
async def admins_command(message: Message) -> Message:
    chat_id = message.chat.id
    admins = await message.bot.get_chat_administrators(chat_id)
    text = ''
    for admin in admins:
        text += f'@{admin.user.username} '
    return await message.answer(text, disable_notification=True)
Enter fullscreen mode Exit fullscreen mode

Here we only get a list of group administrators. Any user can call this command.

What's next? ❓

Next, we'll do some check on new chat members and look at the Finite State Machine.

References 🔗

Thank you for reading! ❤️ ❤️ ❤️

💖 💪 🙅 🚩
mezgoodle
Maksym Zavalniuk

Posted on September 6, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related