K
K
Kremix2021-09-11 13:50:58
Python
Kremix, 2021-09-11 13:50:58

Mistake in writing Telegram bot filters?

When writing a moderator bot in telegrams, there is an error in the filters. I tried many ways and options, but nothing helps.

(Aiogram 3)

bot.py:
import config
import logging

from filter import IsAdminFilter

from aiogram import Bot, Dispatcher, executor, types

logging.basicConfig(level=logging.INFO)

bot = Bot(token=config.TOKEN)
dp = Dispatcher(bot)

dp.filters_factory.bind(IsAdminFilter)

#ban command for admin 
@dp.message(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.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)

filter.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: types.Message):
    member = await message.bot.get_chat_member(message.chat.id, message.from_user.id)
    return member.is_chat_admin()

terminal:
$ python bot.py
Traceback (most recent call last):
File "path/Kas/tt/bot.py", line 4, in
from filter import IsAdminFilter
File "path/Kas/tt/filter.py", line 2, in
from aiogram.dispatcher.filters import BoundFilter
ImportError: cannot import name 'BoundFilter' from 'aiogram.dispatcher.filters'

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question