添加退出指定群组命令

This commit is contained in:
洛水.山岭居室 2022-05-19 19:04:48 +08:00
parent ed7618f961
commit 434e51e49d
2 changed files with 35 additions and 1 deletions

View File

@ -114,8 +114,9 @@ def main() -> None:
application.add_handler(CommandHandler("del_admin", admin.del_admin, block=False))
weapon = Weapon(service)
application.add_handler(CommandHandler("weapon", weapon.command_start, block=False))
application.add_handler(CommandHandler("reply_keyboard_remove", reply_keyboard_remove, block=False))
application.add_handler(MessageHandler(filters.Regex(r"^武器查询(.*)"), weapon.command_start, block=False))
application.add_handler(CommandHandler("reply_keyboard_remove", reply_keyboard_remove, block=False))
application.add_handler(CommandHandler("leave_chat", admin.leave_chat, block=False))
application.add_handler(quiz_handler)
application.add_handler(cookies_handler)
application.add_handler(get_user_handler)

View File

@ -1,6 +1,8 @@
from telegram import Update
from telegram.error import BadRequest
from telegram.ext import CallbackContext
from logger import Log
from plugins.base import BasePlugins
from service import BaseService
@ -42,3 +44,34 @@ class Admin(BasePlugins):
await message.reply_text("该用户不存在管理员列表")
else:
await message.reply_text("权限不足")
async def leave_chat(self, update: Update, context: CallbackContext):
message = update.message
user = message.from_user
admin_list = await self.service.admin.get_admin_list()
if user.id in admin_list:
try:
args = message.text.split()
if len(args) >= 2:
char_id = int(args[1])
else:
await message.reply_text("输入错误")
return
except ValueError as error:
Log.error(f"获取 char_id 发生错误! 错误信息为 \n", error)
await message.reply_text("输入错误")
return
try:
try:
char = await context.bot.get_chat(char_id)
await message.reply_text(f"正在尝试退出群 {char.title}[{char.id}]")
except BadRequest:
pass
await context.bot.leave_chat(char_id)
except BadRequest as error:
Log.error(f"退出 char_id[{char_id}] 发生错误! 错误信息为 \n", error)
await message.reply_text(f"退出 char_id[{char_id}] 发生错误! 错误信息为 {str(error)}")
return
await message.reply_text(f"退出 char_id[{char_id}] 成功!")
else:
await message.reply_text("权限不足")