mirror of
https://github.com/civsocit/olgram.git
synced 2023-07-22 01:29:12 +03:00
Merge pull request #24 from arcxio/multiple_admins
support multiple comma-separated values in ADMIN_ID
This commit is contained in:
commit
ac09e42f94
2
main.py
2
main.py
@ -25,7 +25,7 @@ async def init_database():
|
||||
|
||||
async def init_olgram():
|
||||
from olgram.router import bot, dp
|
||||
dp.setup_middleware(AccessMiddleware(OlgramSettings.admin_id()))
|
||||
dp.setup_middleware(AccessMiddleware(OlgramSettings.admin_ids()))
|
||||
from aiogram.types import BotCommand
|
||||
await bot.set_my_commands(
|
||||
[
|
||||
|
@ -45,9 +45,9 @@ class OlgramSettings(AbstractSettings):
|
||||
|
||||
@classmethod
|
||||
@lru_cache
|
||||
def admin_id(cls):
|
||||
_id = cls._get_env("ADMIN_ID", True)
|
||||
return int(_id) if _id else None
|
||||
def admin_ids(cls):
|
||||
_ids = cls._get_env("ADMIN_ID", True)
|
||||
return set(map(int, _ids.split(","))) if _ids else None
|
||||
|
||||
@classmethod
|
||||
@lru_cache
|
||||
|
@ -1,6 +1,7 @@
|
||||
import aiogram.types as types
|
||||
from aiogram.dispatcher.handler import CancelHandler, current_handler
|
||||
from aiogram.dispatcher.middlewares import BaseMiddleware
|
||||
from collections.abc import Container
|
||||
from locales.locale import _
|
||||
|
||||
|
||||
@ -19,8 +20,8 @@ def public():
|
||||
|
||||
|
||||
class AccessMiddleware(BaseMiddleware):
|
||||
def __init__(self, access_chat_id: int):
|
||||
self._access_chat_id = access_chat_id
|
||||
def __init__(self, access_chat_ids: Container[int]):
|
||||
self._access_chat_ids = access_chat_ids
|
||||
super(AccessMiddleware, self).__init__()
|
||||
|
||||
@classmethod
|
||||
@ -29,25 +30,25 @@ class AccessMiddleware(BaseMiddleware):
|
||||
return handler and getattr(handler, "access_public", False)
|
||||
|
||||
async def on_process_message(self, message: types.Message, data: dict):
|
||||
admin_id = self._access_chat_id
|
||||
if not admin_id:
|
||||
return # Администратор бота вообще не указан
|
||||
admin_ids = self._access_chat_ids
|
||||
if not admin_ids:
|
||||
return # Администраторы бота вообще не указаны
|
||||
|
||||
if self._is_public_command(): # Эта команда разрешена всем пользователям
|
||||
return
|
||||
|
||||
if message.chat.id != admin_id:
|
||||
if message.chat.id not in admin_ids:
|
||||
await message.answer(_("Владелец бота ограничил доступ к этому функционалу 😞"))
|
||||
raise CancelHandler()
|
||||
|
||||
async def on_process_callback_query(self, call: types.CallbackQuery, data: dict):
|
||||
admin_id = self._access_chat_id
|
||||
if not admin_id:
|
||||
return # Администратор бота вообще не указан
|
||||
admin_ids = self._access_chat_ids
|
||||
if not admin_ids:
|
||||
return # Администраторы бота вообще не указаны
|
||||
|
||||
if self._is_public_command(): # Эта команда разрешена всем пользователям
|
||||
return
|
||||
|
||||
if call.message.chat.id != admin_id:
|
||||
if call.message.chat.id not in admin_ids:
|
||||
await call.answer(_("Владелец бота ограничил доступ к этому функционалу😞"))
|
||||
raise CancelHandler()
|
||||
|
Loading…
Reference in New Issue
Block a user