From 8cb3ff9ca7ab301db88923cf4b618f29502dd83e Mon Sep 17 00:00:00 2001 From: Karako <70872201+karakoo@users.noreply.github.com> Date: Wed, 31 Aug 2022 16:48:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=20redis=20?= =?UTF-8?q?=E4=B8=AD=E6=97=A0=E6=95=B0=E6=8D=AE=E6=97=B6=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 兼容 python3.8 * 🐛 修复 redis 中无数据时的错误 --- core/wiki/cache.py | 6 +++++- core/wiki/services.py | 4 ++-- plugins/system/auth.py | 8 ++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/core/wiki/cache.py b/core/wiki/cache.py index 752ca697..d9d3c6ed 100644 --- a/core/wiki/cache.py +++ b/core/wiki/cache.py @@ -23,7 +23,11 @@ class WikiCache: async def get(self, key: str) -> dict: qname = f"{self.qname}:{key}" - result = json.loads(await self.client.get(qname)) + # noinspection PyBroadException + try: + result = json.loads(await self.client.get(qname)) + except Exception: + result = [] if isinstance(result, list) and len(result) > 0: for num, item in enumerate(result): result[num] = json.loads(item) diff --git a/core/wiki/services.py b/core/wiki/services.py index 110a00b4..75de4ac4 100644 --- a/core/wiki/services.py +++ b/core/wiki/services.py @@ -71,10 +71,10 @@ class WikiService: if self.first_run: weapon_dict = await self._cache.get("weapon") self._weapon_list = [Weapon.parse_obj(obj) for obj in weapon_dict] - self._weapon_name_list = await Weapon.get_name_list() + self._weapon_name_list = [weapon.name for weapon in self._weapon_list] characters_dict = await self._cache.get("characters") self._character_list = [Character.parse_obj(obj) for obj in characters_dict] - self._character_name_list = await Character.get_name_list() + self._character_name_list = [character.name for character in self._character_list] self.first_run = False diff --git a/plugins/system/auth.py b/plugins/system/auth.py index 70c96f34..6778423d 100644 --- a/plugins/system/auth.py +++ b/plugins/system/auth.py @@ -1,7 +1,7 @@ import asyncio import random import time -from typing import Tuple, Union, Dict +from typing import Tuple, Union, Dict, List from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ChatPermissions, ChatMember from telegram.constants import ParseMode @@ -36,7 +36,7 @@ class GroupJoiningVerification: self.kick_time = 120 self.random = MT19937_Random() self.lock = asyncio.Lock() - self.chat_administrators_cache: Dict[Union[str, int], Tuple[float, list[ChatMember]]] = {} + self.chat_administrators_cache: Dict[Union[str, int], Tuple[float, List[ChatMember]]] = {} self.is_refresh_quiz = False async def refresh_quiz(self): @@ -45,7 +45,7 @@ class GroupJoiningVerification: await self.quiz_service.refresh_quiz() self.is_refresh_quiz = True - async def get_chat_administrators(self, context: CallbackContext, chat_id: Union[str, int]) -> list[ChatMember]: + async def get_chat_administrators(self, context: CallbackContext, chat_id: Union[str, int]) -> List[ChatMember]: async with self.lock: cache_data = self.chat_administrators_cache.get(f"{chat_id}") if cache_data is not None: @@ -57,7 +57,7 @@ class GroupJoiningVerification: return chat_administrators @staticmethod - def is_admin(chat_administrators: list[ChatMember], user_id: int) -> bool: + def is_admin(chat_administrators: List[ChatMember], user_id: int) -> bool: return any(admin.user.id == user_id for admin in chat_administrators) async def kick_member_job(self, context: CallbackContext):