2022-07-26 10:07:31 +00:00
|
|
|
from functools import wraps
|
2022-09-08 01:08:37 +00:00
|
|
|
from typing import Callable, cast
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-08 01:08:37 +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 权限检查"""
|
|
|
|
|
2022-08-06 09:02:01 +00:00
|
|
|
@wraps(func)
|
|
|
|
async def decorator(*args, **kwargs):
|
2022-07-26 10:07:31 +00:00
|
|
|
if len(args) == 3:
|
|
|
|
# self update context
|
2022-09-08 01:08:37 +00:00
|
|
|
_, update, _ = args
|
2022-07-26 10:07:31 +00:00
|
|
|
elif len(args) == 2:
|
|
|
|
# update context
|
2022-09-08 01:08:37 +00:00
|
|
|
update, _ = args
|
2022-07-26 10:07:31 +00:00
|
|
|
else:
|
|
|
|
return await func(*args, **kwargs)
|
|
|
|
if bot_admin_service is None:
|
2022-09-08 01:08:37 +00:00
|
|
|
raise ServiceNotFoundError("BotAdminService")
|
2022-07-26 10:07:31 +00:00
|
|
|
admin_list = await bot_admin_service.get_admin_list()
|
2022-09-08 01:08:37 +00:00
|
|
|
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:
|
2022-09-08 01:08:37 +00:00
|
|
|
await message.reply_text("权限不足")
|
2022-07-26 10:07:31 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
return decorator
|