🎨 Refactor chat verification

- Changed `config.verify_groups` to a `Set[int]` type for optimized lookup.
- Used set membership checking instead of looping through `config.verify_groups` for verifying chats.

This results in cleaner code and potentially faster chat verification.
This commit is contained in:
洛水居室 2023-10-08 14:58:25 +08:00
parent 54d19baac9
commit b129a791df
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
2 changed files with 8 additions and 18 deletions

@ -1 +1 @@
Subproject commit a03ba48c06f3b464546632f0cad60c42ef9d398b Subproject commit f5994842531164370fef6a34400d04b955c68d11

View File

@ -48,6 +48,8 @@ class GroupCaptcha(Plugin):
self.mtp = mtp.client self.mtp = mtp.client
self.cache = redis.client self.cache = redis.client
self.ttl = 60 * 60 self.ttl = 60 * 60
self.verify_groups = config.verify_groups
self.user_mismatch = config.notice.user_mismatch
async def initialize(self): async def initialize(self):
logger.info("群验证模块正在刷新问题列表") logger.info("群验证模块正在刷新问题列表")
@ -143,7 +145,7 @@ class GroupCaptcha(Plugin):
chat_administrators = await self.get_chat_administrators(context, chat_id=chat.id) chat_administrators = await self.get_chat_administrators(context, chat_id=chat.id)
if not self.is_admin(chat_administrators, user.id): if not self.is_admin(chat_administrators, user.id):
logger.debug("用户 %s[%s] 在群 %s[%s] 非群管理", user.full_name, user.id, chat.title, chat.id) logger.debug("用户 %s[%s] 在群 %s[%s] 非群管理", user.full_name, user.id, chat.title, chat.id)
await callback_query.answer(text="你不是管理!\n" + config.notice.user_mismatch, show_alert=True) await callback_query.answer(text="你不是管理!\n" + self.user_mismatch, show_alert=True)
return return
result, user_id = await admin_callback(callback_query.data) result, user_id = await admin_callback(callback_query.data)
try: try:
@ -247,7 +249,7 @@ class GroupCaptcha(Plugin):
user_id, result, question, answer = await query_callback(callback_query.data) user_id, result, question, answer = await query_callback(callback_query.data)
logger.info("用户 %s[%s] 在群 %s[%s] 点击Auth认证命令", user.full_name, user.id, chat.title, chat.id) logger.info("用户 %s[%s] 在群 %s[%s] 点击Auth认证命令", user.full_name, user.id, chat.title, chat.id)
if user.id != user_id: if user.id != user_id:
await callback_query.answer(text="这不是你的验证!\n" + config.notice.user_mismatch, show_alert=True) await callback_query.answer(text="这不是你的验证!\n" + self.user_mismatch, show_alert=True)
return return
logger.info( logger.info(
"用户 %s[%s] 在群 %s[%s] 认证结果为 %s", user.full_name, user.id, chat.title, chat.id, "通过" if result else "失败" "用户 %s[%s] 在群 %s[%s] 认证结果为 %s", user.full_name, user.id, chat.title, chat.id, "通过" if result else "失败"
@ -296,13 +298,7 @@ class GroupCaptcha(Plugin):
async def new_mem(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None: async def new_mem(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None:
message = update.effective_message message = update.effective_message
chat = message.chat chat = message.chat
if len(config.verify_groups) >= 1: if chat.id not in self.verify_groups:
for verify_group in config.verify_groups:
if verify_group == chat.id:
break
else:
return
else:
return return
for user in message.new_chat_members: for user in message.new_chat_members:
if user.id == context.bot.id: if user.id == context.bot.id:
@ -317,13 +313,7 @@ class GroupCaptcha(Plugin):
@handler.chat_member(chat_member_types=ChatMemberHandler.CHAT_MEMBER, block=False) @handler.chat_member(chat_member_types=ChatMemberHandler.CHAT_MEMBER, block=False)
async def track_users(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None: async def track_users(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> None:
chat = update.effective_chat chat = update.effective_chat
if len(config.verify_groups) >= 1: if chat.id not in self.verify_groups:
for verify_group in config.verify_groups:
if verify_group == chat.id:
break
else:
return
else:
return return
new_chat_member = update.chat_member.new_chat_member new_chat_member = update.chat_member.new_chat_member
from_user = update.chat_member.from_user from_user = update.chat_member.from_user
@ -422,8 +412,8 @@ class GroupCaptcha(Plugin):
user_id=user.id, user_id=user.id,
job_kwargs={"replace_existing": True, "id": f"{chat.id}|{user.id}|auth_clean_question_message"}, job_kwargs={"replace_existing": True, "id": f"{chat.id}|{user.id}|auth_clean_question_message"},
) )
new_chat_members_message = await self.get_new_chat_members_message(user, context)
if PYROGRAM_AVAILABLE and self.mtp: if PYROGRAM_AVAILABLE and self.mtp:
new_chat_members_message = await self.get_new_chat_members_message(user, context)
try: try:
if new_chat_members_message: if new_chat_members_message:
if question_message.id - new_chat_members_message.id - 1: if question_message.id - new_chat_members_message.id - 1: