B
B
Boris Ermakov2021-11-17 11:54:02
Python
Boris Ermakov, 2021-11-17 11:54:02

Why does /ban give an error AttributeError: type object 'type' has no attribute 'Message' when I try to write a command?

When trying to start the bot, it gives an error AttributeError: type object 'type' has no attribute 'Message'

$ python main2.py
Traceback (most recent call last):
  File "main2.py", line 5, in <module>
    from filters import IsAdminFilter
  File "/home/boris/py/ParOk/filters.py", line 5, in <module>
    class IsAdminFilter(BoundFilter):
  File "/home/boris/py/ParOk/filters.py", line 11, in IsAdminFilter
    async def check(self, message: type.Message):
AttributeError: type object 'type' has no attribute 'Message'


main2.py

import config
import logging

from aiogram import Bot, Dispatcher, executor, types
from filters import IsAdminFilter

# log level
logging.basicConfig(level=logging.INFO)

# Инициализация бота
bot = Bot(token=config.TOKEN)
dp = Dispatcher(bot)

# Активация фильтров
dp.filters_factory.bind(IsAdminFilter)


# Удаление уведомления о новом входе
@dp.message_handler(content_types=["new_chat_members"])
async def on_user_joined(message: types.Message):
    await message.delete()


# /Ban
@dp.message_handler(is_admin=True, commands=["ban"], commands_prefix="!/")
async def cmd_ban(message: types.Message):
    if not message.reply_to_message:
        await message.reply("Эта команда должна быть ответом на сообщение!")
        return

    await message.bot.delete_message(chat_id=config.GROUP_ID, message_id=message.message_id)
    await message.bot.kick_chat_member(chat_id=config.GROUP_ID, user_id=message.reply_to_message.from_user.id)

    await message.reply_to_message.reply("Пользавтель заблокирован")


if __name__ == "__main__":
    executor.start_polling(dp, skip_updates=False)


filters.py

from aiogram import types
from aiogram.dispatcher.filters import BoundFilter


class IsAdminFilter(BoundFilter):
    key = "is_admin"

    def __init__(self, is_admin):
        self.is_admin = is_admin

    async def check(self, message: type.Message):
        member = await message.bot.get_chat_member(message.chat.id, message.from_user.id)
        return member.is_chat_admin()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-11-17
@CodeWriterB

type s , not type.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question