添加 new_chat_members 相关处理

可用于服务器资源有限 不适于大范围使用的情况
This commit is contained in:
洛水居室 2022-08-06 17:12:18 +08:00
parent 32030cf630
commit 1181e7233e
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
2 changed files with 10 additions and 9 deletions

View File

@ -1,11 +1,10 @@
import datetime import datetime
from typing import Callable
from telegram import Update, ReplyKeyboardRemove from telegram import Update, ReplyKeyboardRemove
from telegram.error import BadRequest from telegram.error import BadRequest
from telegram.ext import CallbackContext, ConversationHandler, filters from telegram.ext import CallbackContext, ConversationHandler, filters
from apps.admin import BotAdminService from apps.admin.services import BotAdminService
from logger import Log from logger import Log
from utils.apps.inject import inject from utils.apps.inject import inject
@ -52,11 +51,12 @@ class BasePlugins:
class NewChatMembersHandler: class NewChatMembersHandler:
def __init__(self, auth_callback: Callable):
self.auth_callback = auth_callback
@inject @inject
async def new_member(self, update: Update, context: CallbackContext, bot_admin_service: BotAdminService) -> None: def __init__(self, bot_admin_service: BotAdminService):
self.bot_admin_service = bot_admin_service
async def new_member(self, update: Update, context: CallbackContext) -> None:
message = update.message message = update.message
chat = message.chat chat = message.chat
from_user = message.from_user from_user = message.from_user
@ -66,7 +66,7 @@ class NewChatMembersHandler:
if user.id == context.bot.id: if user.id == context.bot.id:
if from_user is not None: if from_user is not None:
Log.info(f"用户 {from_user.full_name}[{from_user.id}] 在群 {chat.title}[{chat.id}] 邀请BOT") Log.info(f"用户 {from_user.full_name}[{from_user.id}] 在群 {chat.title}[{chat.id}] 邀请BOT")
admin_list = await bot_admin_service.get_admin_list() admin_list = await self.bot_admin_service.get_admin_list()
if from_user.id in admin_list: if from_user.id in admin_list:
await context.bot.send_message(message.chat_id, await context.bot.send_message(message.chat_id,
'感谢邀请小派蒙到本群!请使用 /help 查看咱已经学会的功能。') '感谢邀请小派蒙到本群!请使用 /help 查看咱已经学会的功能。')
@ -79,5 +79,4 @@ class NewChatMembersHandler:
Log.warning("不是管理员邀请!退出群聊。") Log.warning("不是管理员邀请!退出群聊。")
await context.bot.send_message(message.chat_id, "派蒙不想进去!不是旅行者的邀请!") await context.bot.send_message(message.chat_id, "派蒙不想进去!不是旅行者的邀请!")
await context.bot.leave_chat(chat.id) await context.bot.leave_chat(chat.id)
await self.auth_callback(update, context)

View File

@ -3,6 +3,7 @@ from typing import Optional
from telegram.ext import CommandHandler, MessageHandler, filters, CallbackQueryHandler, Application, InlineQueryHandler from telegram.ext import CommandHandler, MessageHandler, filters, CallbackQueryHandler, Application, InlineQueryHandler
from logger import Log from logger import Log
from plugins.base import NewChatMembersHandler
from plugins.errorhandler import error_handler from plugins.errorhandler import error_handler
from plugins.inline import Inline from plugins.inline import Inline
from plugins.start import start, ping, reply_keyboard_remove, unknown_command from plugins.start import start, ping, reply_keyboard_remove, unknown_command
@ -28,7 +29,6 @@ def register_plugin_handlers(application: Application):
application.add_handler(CallbackQueryHandler(handler, pattern=query, block=block)) application.add_handler(CallbackQueryHandler(handler, pattern=query, block=block))
# 初始化 # 初始化
Log.info("正在加载插件管理器") Log.info("正在加载插件管理器")
plugins_manager = PluginsManager() plugins_manager = PluginsManager()
@ -44,13 +44,15 @@ def register_plugin_handlers(application: Application):
Log.info("正在加载内置插件") Log.info("正在加载内置插件")
inline = Inline() inline = Inline()
new_chat_members_handler = NewChatMembersHandler()
add_handler(start, command="start") add_handler(start, command="start")
add_handler(ping, command="ping") add_handler(ping, command="ping")
# 调试功能 # 调试功能
add_handler(reply_keyboard_remove, command="reply_keyboard_remove") add_handler(reply_keyboard_remove, command="reply_keyboard_remove")
application.add_handler(MessageHandler(filters.StatusUpdate.NEW_CHAT_MEMBERS,
new_chat_members_handler.new_member, block=False))
application.add_handler(InlineQueryHandler(inline.inline_query, block=False)) application.add_handler(InlineQueryHandler(inline.inline_query, block=False))
application.add_handler(MessageHandler(filters.COMMAND & filters.ChatType.PRIVATE, unknown_command)) application.add_handler(MessageHandler(filters.COMMAND & filters.ChatType.PRIVATE, unknown_command))
application.add_error_handler(error_handler, block=False) application.add_error_handler(error_handler, block=False)