PamGram/utils/decorators/admins.py

39 lines
1.1 KiB
Python
Raw Normal View History

2022-07-26 10:07:31 +00:00
from functools import wraps
from typing import Callable, cast
2022-07-26 10:07:31 +00:00
from telegram import Update
from core.admin import BotAdminService
from core.bot import bot
from core.error import ServiceNotFoundError
bot_admin_service = bot.services.get(BotAdminService)
2022-07-26 10:07:31 +00:00
def bot_admins_rights_check(func: Callable) -> Callable:
"""BOT ADMIN 权限检查"""
@wraps(func)
async def decorator(*args, **kwargs):
2022-07-26 10:07:31 +00:00
if len(args) == 3:
# self update context
_, update, _ = args
2022-07-26 10:07:31 +00:00
elif len(args) == 2:
# update context
update, _ = args
2022-07-26 10:07:31 +00:00
else:
return await func(*args, **kwargs)
if bot_admin_service is None:
raise ServiceNotFoundError("BotAdminService")
2022-07-26 10:07:31 +00:00
admin_list = await bot_admin_service.get_admin_list()
update = cast(Update, update)
message = update.effective_message
user = update.effective_user
if user.id in admin_list:
2022-07-26 10:07:31 +00:00
return await func(*args, **kwargs)
else:
await message.reply_text("权限不足")
2022-07-26 10:07:31 +00:00
return None
return decorator