PaiGram/plugins/admin.py

95 lines
3.6 KiB
Python
Raw Normal View History

2022-05-20 08:34:03 +00:00
from typing import Callable
2022-04-14 07:18:45 +00:00
from telegram import Update
from telegram.error import BadRequest, Forbidden
2022-06-26 06:17:43 +00:00
from telegram.ext import CallbackContext, CommandHandler
2022-04-14 07:18:45 +00:00
2022-05-19 11:04:48 +00:00
from logger import Log
2022-06-26 06:17:43 +00:00
from manager import listener_plugins_class
from utils.base import PaimonContext
2022-04-14 07:18:45 +00:00
def bot_admins_only(func: Callable) -> Callable: # noqa
async def decorator(self, update: Update, context: PaimonContext):
service = context.service
admin_list = await service.admin.get_admin_list()
if update.message.from_user.id in admin_list:
return await func(self, update, context)
else:
await update.message.reply_text("权限不足")
return None
return decorator
2022-06-26 06:17:43 +00:00
@listener_plugins_class()
class Admin:
"""有关BOT ADMIN处理
2022-06-09 12:52:59 +00:00
"""
2022-04-14 07:18:45 +00:00
@classmethod
def create_handlers(cls) -> list:
admin = cls()
2022-06-26 06:17:43 +00:00
return [
CommandHandler("add_admin", admin.add_admin, block=False),
CommandHandler("del_admin", admin.del_admin, block=False),
CommandHandler("leave_chat", admin.leave_chat, block=False),
]
@bot_admins_only
async def add_admin(self, update: Update, context: PaimonContext):
2022-04-14 07:18:45 +00:00
message = update.message
reply_to_message = message.reply_to_message
service = context.service
admin_list = await service.admin.get_admin_list()
2022-05-20 08:34:03 +00:00
if reply_to_message is None:
await message.reply_text("请回复对应消息")
2022-04-14 07:18:45 +00:00
else:
2022-05-20 08:34:03 +00:00
if reply_to_message.from_user.id in admin_list:
await message.reply_text("该用户已经存在管理员列表")
else:
await service.admin.add_admin(reply_to_message.from_user.id)
2022-05-20 08:34:03 +00:00
await message.reply_text("添加成功")
2022-04-14 07:18:45 +00:00
@bot_admins_only
async def del_admin(self, update: Update, context: PaimonContext):
2022-04-14 07:18:45 +00:00
message = update.message
reply_to_message = message.reply_to_message
service = context.service
admin_list = await service.admin.get_admin_list()
2022-05-20 08:34:03 +00:00
if reply_to_message is None:
await message.reply_text("请回复对应消息")
2022-04-14 07:18:45 +00:00
else:
2022-05-20 08:34:03 +00:00
if reply_to_message.from_user.id in admin_list:
await service.admin.delete_admin(reply_to_message.from_user.id)
2022-05-20 08:34:03 +00:00
await message.reply_text("删除成功")
else:
await message.reply_text("该用户不存在管理员列表")
2022-05-19 11:04:48 +00:00
@bot_admins_only
2022-05-19 11:04:48 +00:00
async def leave_chat(self, update: Update, context: CallbackContext):
message = update.message
2022-05-20 08:34:03 +00:00
try:
args = message.text.split()
if len(args) >= 2:
2022-06-09 15:09:10 +00:00
chat_id = int(args[1])
2022-05-20 08:34:03 +00:00
else:
2022-05-19 11:04:48 +00:00
await message.reply_text("输入错误")
return
2022-05-20 08:34:03 +00:00
except ValueError as error:
2022-06-09 15:09:10 +00:00
Log.error("获取 chat_id 发生错误! 错误信息为 \n", error)
2022-05-20 08:34:03 +00:00
await message.reply_text("输入错误")
return
try:
2022-05-19 11:04:48 +00:00
try:
2022-06-09 15:09:10 +00:00
char = await context.bot.get_chat(chat_id)
2022-05-20 08:34:03 +00:00
await message.reply_text(f"正在尝试退出群 {char.title}[{char.id}]")
except (BadRequest, Forbidden):
pass
2022-06-09 15:09:10 +00:00
await context.bot.leave_chat(chat_id)
2022-05-20 08:34:03 +00:00
except (BadRequest, Forbidden) as error:
2022-06-09 15:09:10 +00:00
Log.error(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 \n", error)
await message.reply_text(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 {str(error)}")
2022-05-20 08:34:03 +00:00
return
2022-06-09 15:09:10 +00:00
await message.reply_text(f"退出 chat_id[{chat_id}] 成功!")