修改 plugins.admin 的权限管理装饰器 独立于 Admin 类外

删除 `plugins.auth` 多余参数
This commit is contained in:
洛水.山岭居室 2022-05-26 20:34:10 +08:00
parent d77db74278
commit 103534d8ee
2 changed files with 18 additions and 16 deletions

View File

@ -9,22 +9,24 @@ from plugins.base import BasePlugins
from service import BaseService from service import BaseService
class Admin(BasePlugins): def bot_admins_only(func: Callable) -> Callable: # noqa
def __init__(self, service: BaseService):
super().__init__(service)
def bot_admins_only(func: Callable) -> Callable: # noqa
async def decorator(self, update: Update, context: CallbackContext): async def decorator(self, update: Update, context: CallbackContext):
admin_list = await self.service.admin.get_admin_list() admin_list = await self.service.admin.get_admin_list()
if update.message.from_user.id in admin_list: if update.message.from_user.id in admin_list:
return await func(self, update, context) return await func(self, update, context)
else: else:
await update.message.reply_text("权限不足") await update.message.reply_text("权限不足")
return None
return decorator return decorator
@bot_admins_only # noqa
async def add_admin(self, update: Update, context: CallbackContext): class Admin(BasePlugins):
def __init__(self, service: BaseService):
super().__init__(service)
@bot_admins_only
async def add_admin(self, update: Update, _: CallbackContext):
message = update.message message = update.message
reply_to_message = message.reply_to_message reply_to_message = message.reply_to_message
admin_list = await self.service.admin.get_admin_list() admin_list = await self.service.admin.get_admin_list()
@ -37,8 +39,8 @@ class Admin(BasePlugins):
await self.service.admin.add_admin(reply_to_message.from_user.id) await self.service.admin.add_admin(reply_to_message.from_user.id)
await message.reply_text("添加成功") await message.reply_text("添加成功")
@bot_admins_only # noqa @bot_admins_only
async def del_admin(self, update: Update, context: CallbackContext): async def del_admin(self, update: Update, _: CallbackContext):
message = update.message message = update.message
reply_to_message = message.reply_to_message reply_to_message = message.reply_to_message
admin_list = await self.service.admin.get_admin_list() admin_list = await self.service.admin.get_admin_list()
@ -51,7 +53,7 @@ class Admin(BasePlugins):
else: else:
await message.reply_text("该用户不存在管理员列表") await message.reply_text("该用户不存在管理员列表")
@bot_admins_only # noqa @bot_admins_only
async def leave_chat(self, update: Update, context: CallbackContext): async def leave_chat(self, update: Update, context: CallbackContext):
message = update.message message = update.message
try: try:

View File

@ -40,7 +40,7 @@ class Auth:
self.generator = Generator(MT19937(int(self.send_time))) self.generator = Generator(MT19937(int(self.send_time)))
return int(self.generator.uniform(low, high)) return int(self.generator.uniform(low, high))
async def kick_member(self, context: CallbackContext, chat_id: int, user_id: int, kick_time: int = -1) -> bool: async def kick_member(self, context: CallbackContext, chat_id: int, user_id: int) -> bool:
try: try:
await context.bot.ban_chat_member(chat_id=chat_id, user_id=user_id, await context.bot.ban_chat_member(chat_id=chat_id, user_id=user_id,
until_date=int(time.time()) + self.kick_time) until_date=int(time.time()) + self.kick_time)