🎨 改进部分函数异常处理

This commit is contained in:
洛水居室 2022-09-19 23:31:17 +08:00
parent 1aa0e3d38d
commit 62dbcafa8c
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
2 changed files with 16 additions and 8 deletions

View File

@ -1,5 +1,5 @@
from telegram import Update, ReplyKeyboardRemove
from telegram.error import BadRequest
from telegram.error import BadRequest, Forbidden
from telegram.ext import CallbackContext, ConversationHandler
from core.plugin import handler, conversation
@ -12,14 +12,21 @@ async def clean_message(context: CallbackContext):
try:
# noinspection PyTypeChecker
await context.bot.delete_message(chat_id=job.chat_id, message_id=job.data)
except BadRequest as error:
if "not found" in str(error):
except BadRequest as exc:
if "not found" in str(exc):
logger.warning(f"Auth模块删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败 消息不存在")
elif "Message can't be deleted" in str(error):
elif "Message can't be deleted" in str(exc):
logger.warning(
f"Auth模块删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败 消息无法删除 可能是没有授权")
else:
logger.error(f"Auth模块删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败", error)
logger.error(f"Auth模块删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败")
logger.exception(exc)
except Forbidden as exc:
if "bot was kicked" in str(exc):
logger.warning(f"Auth模块删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败 已经被踢出群")
else:
logger.error(f"Auth模块删除消息 chat_id[{job.chat_id}] message_id[{job.data}]失败")
logger.exception(exc)
def add_delete_message_job(context: CallbackContext, chat_id: int, message_id: int, delete_seconds: int):

View File

@ -66,8 +66,9 @@ class AdminPlugin(Plugin):
except (BadRequest, Forbidden):
pass
await context.bot.leave_chat(chat_id)
except (BadRequest, Forbidden) as error:
logger.error(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 \n", error)
await message.reply_text(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 {str(error)}")
except (BadRequest, Forbidden) as exc:
logger.error(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 \n")
logger.exception(exc)
await message.reply_text(f"退出 chat_id[{chat_id}] 发生错误! 错误信息为 {str(exc)}")
return
await message.reply_text(f"退出 chat_id[{chat_id}] 成功!")