filteres.py
from abc import ABC
from aiogram import types
from aiogram.dispatcher.filters import BoundFilter
class IsAdminFilter(BoundFilter, ABC):
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()
bot.py
import config
import logging
import filtres
from aiogram import Bot, Dispatcher, executor, types
from filtres import IsAdminFilter
# log level
logging.basicConfig(level=logging.INFO)
# bot init
bot = Bot(token=config.TOKEN)
dp = Dispatcher(bot)
# active filters
dp.filters_factory.bind(IsAdminFilter)
# simple profanity check
@dp.message_handler()
async def filter_message(message: types.Message):
if "ругательство" in message.text:
# profanity detected, remove
await message.delete()
# remove new user joined message
@dp.message_handler(content_types=["new_chat_members"])
async def on_user_joined(message: types.Message):
await message.delete()
# ban command (admins only!)
@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("Забанили за нарушение правил!!!")
# remove message of new chat members
@dp.message_handler(content_types=["new_chat_members"])
async def on_user_joined(message: types.Message):
await message.delete()
# run long-polling
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)
ошибка:
Traceback (most recent call last):
File "/Users/pravda/Documents/pythonProject/Telegram bot/bot.py", line 36, in <module>
@dp.message_handler(is_admin=True, commands=["ban"], commands_prefix="!/")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^