2022-09-08 01:08:37 +00:00
|
|
|
|
import json
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from functools import wraps
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from typing import Callable, cast
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
from aiohttp import ClientConnectorError
|
|
|
|
|
from genshin import InvalidCookies, GenshinException, TooManyRequests
|
|
|
|
|
from httpx import ConnectTimeout
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from telegram import Update, ReplyKeyboardRemove
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from telegram.error import BadRequest, TimedOut, Forbidden
|
|
|
|
|
from telegram.ext import CallbackContext, ConversationHandler
|
|
|
|
|
|
2022-09-01 01:51:01 +00:00
|
|
|
|
from utils.error import UrlResourcesNotFoundError
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def send_user_notification(update: Update, _: CallbackContext, text: str):
|
|
|
|
|
effective_user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-07-26 10:07:31 +00:00
|
|
|
|
if message is None:
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("错误的消息类型\n" + json.dumps(update_str, indent=2, ensure_ascii=False))
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return
|
|
|
|
|
chat = message.chat
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.info(f"尝试通知用户 {effective_user.full_name}[{effective_user.id}] "
|
|
|
|
|
f"在 {chat.full_name}[{chat.id}]"
|
|
|
|
|
f"的 错误信息[{text}]")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
try:
|
|
|
|
|
await message.reply_text(text, reply_markup=ReplyKeyboardRemove(), allow_sending_without_reply=True)
|
|
|
|
|
except BadRequest as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
|
|
|
|
|
logger.exception(exc)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except Forbidden as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
|
|
|
|
|
logger.exception(exc)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except BaseException as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为")
|
|
|
|
|
logger.exception(exc)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
finally:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def error_callable(func: Callable) -> Callable:
|
|
|
|
|
"""Plugins 错误处理修饰器
|
|
|
|
|
|
|
|
|
|
非常感谢 @Bibo-Joshi 提出的建议
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
|
async def decorator(*args, **kwargs):
|
|
|
|
|
if len(args) == 3:
|
|
|
|
|
# self update context
|
|
|
|
|
_, update, context = args
|
|
|
|
|
elif len(args) == 2:
|
|
|
|
|
# update context
|
|
|
|
|
update, context = args
|
|
|
|
|
else:
|
|
|
|
|
return await func(*args, **kwargs)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
update = cast(Update, update)
|
|
|
|
|
context = cast(CallbackContext, context)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
try:
|
|
|
|
|
return await func(*args, **kwargs)
|
|
|
|
|
except ClientConnectorError:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("aiohttp 模块连接服务器 ClientConnectorError")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except ConnectTimeout:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("httpx 模块连接服务器 ConnectTimeout")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except TimedOut:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("python-telegram-bot 模块连接服务器 TimedOut")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
|
|
|
|
return ConversationHandler.END
|
2022-09-01 01:51:01 +00:00
|
|
|
|
except UrlResourcesNotFoundError as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("URL数据资源未找到")
|
|
|
|
|
logger.exception(exc)
|
2022-09-01 01:51:01 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 资源未找到 ~ ")
|
|
|
|
|
return ConversationHandler.END
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except InvalidCookies as exc:
|
|
|
|
|
if "[10001]" in str(exc):
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ Cookies无效,请尝试重新绑定账户")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
elif "[-100]" in str(exc):
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ Cookies无效,请尝试重新绑定账户")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
elif "[10103]" in str(exc):
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ Cookie有效,但没有绑定到游戏帐户,"
|
|
|
|
|
"请尝试重新绑定邮游戏账户")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("Cookie错误")
|
|
|
|
|
logger.exception(exc)
|
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ Cookies无效,具体原因未知")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except TooManyRequests as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("查询次数太多(操作频繁)", exc)
|
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 当天查询次数已经超过30次,请次日再进行查询")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except GenshinException as exc:
|
|
|
|
|
if "[-130]" in str(exc):
|
2022-09-08 01:08:37 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 未设置默认角色,请尝试重新绑定默认角色")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("GenshinException", exc)
|
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 获取账号信息发生错误")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except BadRequest as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("python-telegram-bot 请求错误")
|
|
|
|
|
logger.exception(exc)
|
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ telegram-bot-api请求错误")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except Forbidden as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("python-telegram-bot返回 Forbidden")
|
|
|
|
|
logger.exception(exc)
|
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ telegram-bot-api请求错误")
|
2022-07-26 10:07:31 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
|
return decorator
|