diff --git a/core/baseplugin.py b/core/baseplugin.py index 2eeb645..fccae73 100644 --- a/core/baseplugin.py +++ b/core/baseplugin.py @@ -3,29 +3,37 @@ from telegram.error import BadRequest, Forbidden from telegram.ext import CallbackContext, ConversationHandler from core.plugin import handler, conversation +from utils.bot import get_chat from utils.log import logger async def clean_message(context: CallbackContext): job = context.job - logger.debug(f"删除消息 chat_id[{job.chat_id}] 的 message_id[{job.data}]") + message_id = job.data + char_info = f"chat_id[{job.chat_id}]" + try: + chat = await get_chat(job.chat_id) + char_info = f"{chat.full_name}[{chat.id}]" + except (BadRequest, Forbidden) as exc: + logger.warning("获取 chat info 失败 %s", exc.message) + except Exception as exc: + logger.warning("获取 chat info 消息失败 %s", str(exc)) + logger.debug("删除消息 %s message_id[%s]", char_info, message_id) try: # noinspection PyTypeChecker - await context.bot.delete_message(chat_id=job.chat_id, message_id=job.data) + await context.bot.delete_message(chat_id=job.chat_id, message_id=message_id) except BadRequest as exc: - if "not found" in str(exc): - logger.warning(f"删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败 消息不存在") - elif "Message can't be deleted" in str(exc): - logger.warning(f"删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败 消息无法删除 可能是没有授权") + if "not found" in exc.message: + logger.warning("删除消息 %s message_id[%s] 失败 消息不存在", char_info, message_id) + elif "Message can't be deleted" in exc.message: + logger.warning("删除消息 %s message_id[%s] 失败 消息无法删除 可能是没有授权", char_info, message_id) else: - logger.error(f"删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败") - logger.exception(exc) + logger.warning("删除消息 %s message_id[%s] 失败", char_info, message_id, exc_info=exc) except Forbidden as exc: - if "bot was kicked" in str(exc): - logger.warning(f"删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败 已经被踢出群") + if "bot was kicked" in exc.message: + logger.warning("删除消息 %s message_id[%s] 失败 已经被踢出群", char_info, message_id) else: - logger.error(f"删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败") - logger.exception(exc) + logger.warning("删除消息 %s message_id[%s] 失败", char_info, message_id, exc_info=exc) def add_delete_message_job(context: CallbackContext, chat_id: int, message_id: int, delete_seconds: int): diff --git a/utils/bot.py b/utils/bot.py index dc31049..f44dbff 100644 --- a/utils/bot.py +++ b/utils/bot.py @@ -1,7 +1,29 @@ -from typing import List +import json +from typing import List, cast, Union +from telegram import Chat from telegram.ext import CallbackContext +from core.base.redisdb import RedisDB +from core.bot import bot + +redis_db = bot.services.get(RedisDB) +redis_db = cast(RedisDB, redis_db) + + +async def get_chat(chat_id: Union[str, int], ttl: int = 86400) -> Chat: + if redis_db: + qname = f"bot:chat:{chat_id}" + data = await redis_db.client.get(qname) + if data: + json_data = json.loads(data) + return Chat.de_json(json_data, bot.app.bot) + chat_info = await bot.app.bot.get_chat(chat_id) + await redis_db.client.set(qname, chat_info.to_json()) + await redis_db.client.expire(qname, ttl) + return chat_info + return await bot.app.bot.get_chat(chat_id) + def get_all_args(context: CallbackContext) -> List[str]: args = context.args