Skip to content

Commit

Permalink
Added possibility to disable reporting and restrictions commands
Browse files Browse the repository at this point in the history
  • Loading branch information
JrooTJunior committed Dec 12, 2021
1 parent 0023d98 commit 68098e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions aiogram_bot/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from aiogram import Dispatcher
from loguru import logger

from aiogram_bot.filters.chat_property import ChatPropertyFilter


def setup(dispatcher: Dispatcher):
logger.info("Configure filters...")
Expand All @@ -18,4 +20,5 @@ def setup(dispatcher: Dispatcher):
dispatcher.filters_factory.bind(IsReplyFilter, event_handlers=text_messages)
dispatcher.filters_factory.bind(HasPermissions, event_handlers=text_messages)
dispatcher.filters_factory.bind(BotHasPermissions, event_handlers=text_messages)
dispatcher.filters_factory.bind(ChatPropertyFilter, event_handlers=text_messages)
dispatcher.filters_factory.bind(IsSuperuserFilter)
17 changes: 17 additions & 0 deletions aiogram_bot/filters/chat_property.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass

from aiogram.dispatcher.filters import BoundFilter
from aiogram.dispatcher.handler import ctx_data

from aiogram_bot.models.chat import Chat


@dataclass
class ChatPropertyFilter(BoundFilter):
key = "chat_property"
chat_property: str

async def check(self, obj) -> bool:
data = ctx_data.get()
chat: Chat = data["chat"]
return getattr(chat, self.chat_property, False)
9 changes: 8 additions & 1 deletion aiogram_bot/handlers/simple_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
commands_prefix="!",
user_can_restrict_members=True,
bot_can_restrict_members=True,
# chat_property="restrict_commands",
)
async def command_ban_sender_chat(message: types.Message, target: Optional[types.Chat] = None):
if target is None:
Expand Down Expand Up @@ -62,6 +63,7 @@ async def command_ban_sender_chat(message: types.Message, target: Optional[types
commands_prefix="!",
user_can_restrict_members=True,
bot_can_restrict_members=True,
chat_property="restrict_commands",
)
async def cmd_ro(message: types.Message, chat: Chat):
duration = await parse_timedelta_from_message(message)
Expand Down Expand Up @@ -99,6 +101,7 @@ async def cmd_ro(message: types.Message, chat: Chat):
commands_prefix="!",
user_can_restrict_members=True,
bot_can_restrict_members=True,
chat_property="restrict_commands",
)
async def cmd_ban(message: types.Message, chat: Chat):
duration = await parse_timedelta_from_message(message)
Expand Down Expand Up @@ -129,13 +132,17 @@ async def cmd_ban(message: types.Message, chat: Chat):


@dp.message_handler(
chat_type=[types.ChatType.GROUP, types.ChatType.SUPERGROUP], text_contains="@admin", state="*"
chat_type=[types.ChatType.GROUP, types.ChatType.SUPERGROUP],
text_contains="@admin",
state="*",
chat_property="report_to_admins",
)
@dp.message_handler(
chat_type=[types.ChatType.GROUP, types.ChatType.SUPERGROUP],
commands=["report"],
commands_prefix="!/",
state="*",
chat_property="report_to_admins",
)
async def text_report_admins(message: types.Message):
logger.info(
Expand Down
2 changes: 2 additions & 0 deletions aiogram_bot/models/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Chat(TimedBaseModel):
join_filter = db.Column(db.Boolean, server_default=expression.false())
ban_channels = db.Column(db.Boolean, server_default=expression.false())
delete_channel_messages = db.Column(db.Boolean, server_default=expression.false())
report_to_admins = db.Column(db.Boolean, server_default=expression.true())
restrict_commands = db.Column(db.Boolean, server_default=expression.true())


class ChatRelatedModel(BaseModel):
Expand Down

0 comments on commit 68098e9

Please sign in to comment.