mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-21 21:58:04 +00:00
🐛 批量修复代码BUG
⚙️ 获取 Redis cookies 池函数从 `zrevrange` 替代为 `zrange` 🐛 修复 fakeredis 无效 📝优化文本描述 🎨 提高代码质量 Co-authored-by: 洛水居室 <luoshuijs@outlook.com>
This commit is contained in:
parent
340741543a
commit
0c62cc8b27
@ -35,8 +35,8 @@ class RedisDB(Service):
|
||||
await self.ping()
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
pass
|
||||
except BaseException as exc:
|
||||
logger.warning("尝试连接 [red]Redis[/] 失败,使用 [red]fakeredis[/] 模拟", exc, extra={'markup': True})
|
||||
except Exception as exc:
|
||||
logger.exception("尝试连接 [red]Redis[/] 失败,使用 [red]fakeredis[/] 模拟", exc_info=exc, extra={'markup': True})
|
||||
self.client = fakeredis.aioredis.FakeRedis()
|
||||
await self.ping()
|
||||
|
||||
|
@ -18,9 +18,9 @@ class PublicCookiesCache:
|
||||
|
||||
def get_public_cookies_queue_name(self, region: RegionEnum):
|
||||
if region == RegionEnum.HYPERION:
|
||||
return self.score_qname + ":yuanshen"
|
||||
return f"{self.score_qname}:yuanshen"
|
||||
elif region == RegionEnum.HOYOLAB:
|
||||
return self.score_qname + ":genshin"
|
||||
return f"{self.score_qname}:genshin"
|
||||
else:
|
||||
raise RegionNotFoundError(region.name)
|
||||
|
||||
@ -47,11 +47,9 @@ class PublicCookiesCache:
|
||||
if isinstance(uid, int):
|
||||
score_maps = {f"{uid}": 0}
|
||||
elif isinstance(uid, list):
|
||||
score_maps = {}
|
||||
for i in uid:
|
||||
score_maps[f"{i}"] = 0
|
||||
score_maps = {f"{i}": 0 for i in uid}
|
||||
else:
|
||||
raise TypeError(f"uid variable type error")
|
||||
raise TypeError("uid variable type error")
|
||||
async with self.client.pipeline(transaction=True) as pipe:
|
||||
# nx:只添加新元素。不要更新已经存在的元素
|
||||
await pipe.zadd(qname, score_maps, nx=True)
|
||||
@ -65,16 +63,11 @@ class PublicCookiesCache:
|
||||
:return:
|
||||
"""
|
||||
qname = self.get_public_cookies_queue_name(region)
|
||||
scores = await self.client.zrevrange(qname, 0, self.end, withscores=True, score_cast_func=int)
|
||||
if len(scores) > 0:
|
||||
def take_score(elem):
|
||||
return elem[1]
|
||||
|
||||
scores.sort(key=take_score)
|
||||
key = scores[0][0]
|
||||
score = scores[0][1]
|
||||
else:
|
||||
scores = await self.client.zrange(qname, 0, self.end, withscores=True, score_cast_func=int)
|
||||
if len(scores) <= 0:
|
||||
raise CookiesCachePoolExhausted
|
||||
key = scores[0][0]
|
||||
score = scores[0][1]
|
||||
async with self.client.pipeline(transaction=True) as pipe:
|
||||
await pipe.zincrby(qname, 1, key)
|
||||
await pipe.execute()
|
||||
@ -95,7 +88,7 @@ class PublicCookiesCache:
|
||||
return await pipe.execute()
|
||||
|
||||
async def incr_by_user_times(self, user_id: Union[List[int], int]):
|
||||
qname = self.user_times_qname + f":{user_id}"
|
||||
qname = f"{self.user_times_qname}:{user_id}"
|
||||
times = await self.client.incrby(qname)
|
||||
if times <= 1:
|
||||
await self.client.expire(qname, self.user_times_ttl)
|
||||
|
@ -50,7 +50,7 @@ class SetUserUid(Plugin.Conversation, BasePlugin.Conversation):
|
||||
cookies_command_data = AddUserCommandData()
|
||||
context.chat_data["add_uid_command_data"] = cookies_command_data
|
||||
text = f'你好 {user.mention_markdown_v2()} ' \
|
||||
f'{escape_markdown("!请输入通行证UID,BOT将会通过通行证UID查找游戏UID。请选择要绑定的服务器!或回复退出取消操作")}'
|
||||
f'{escape_markdown("!请输入通行证UID(非游戏UID),BOT将会通过通行证UID查找游戏UID。请选择要绑定的服务器!或回复退出取消操作")}'
|
||||
reply_keyboard = [['米游社', 'HoYoLab'], ["退出"]]
|
||||
await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True))
|
||||
return CHECK_SERVER
|
||||
@ -83,9 +83,9 @@ class SetUserUid(Plugin.Conversation, BasePlugin.Conversation):
|
||||
except CookiesNotFoundError:
|
||||
pass
|
||||
else:
|
||||
await message.reply_text("你已经绑定Cookie,无法继续下一步")
|
||||
await message.reply_text("你已经通过 Cookie 绑定了账号,无法继续下一步")
|
||||
return ConversationHandler.END
|
||||
await message.reply_text("请输入你的通行证UID", reply_markup=ReplyKeyboardRemove())
|
||||
await message.reply_text("请输入你的通行证UID(非游戏UID)", reply_markup=ReplyKeyboardRemove())
|
||||
return CHECK_UID
|
||||
|
||||
@conversation.state(state=CHECK_UID)
|
||||
@ -102,7 +102,7 @@ class SetUserUid(Plugin.Conversation, BasePlugin.Conversation):
|
||||
try:
|
||||
hoyolab_uid = int(message.text)
|
||||
except ValueError:
|
||||
await message.reply_text("Cookies格式有误,请检查", reply_markup=ReplyKeyboardRemove())
|
||||
await message.reply_text("UID 格式有误,请检查", reply_markup=ReplyKeyboardRemove())
|
||||
return ConversationHandler.END
|
||||
try:
|
||||
cookies = await self.public_cookies_service.get_cookies(user.id, region)
|
||||
|
@ -61,8 +61,11 @@ class ErrorHandler(Plugin):
|
||||
if 'make sure that only one bot instance is running' in tb_string:
|
||||
logger.error("其他机器人在运行,请停止!")
|
||||
return
|
||||
if 'Message is not modified' in tb_string:
|
||||
logger.error("消息未修改")
|
||||
return
|
||||
await context.bot.send_document(chat_id=notice_chat_id, document=open(log_file, "rb"),
|
||||
caption="Error report.")
|
||||
caption=f"Error: \"{context.error.__class__.__name__}\"")
|
||||
except (BadRequest, Forbidden) as exc:
|
||||
logger.error("发送日记失败")
|
||||
logger.exception(exc)
|
||||
|
@ -1,5 +1,6 @@
|
||||
import logging
|
||||
|
||||
import aiofiles
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from flaky import flaky
|
||||
@ -35,8 +36,8 @@ class TestArtifactOcrRate:
|
||||
@staticmethod
|
||||
@flaky(3, 1)
|
||||
async def test_ocr_artifact(artifact_rate):
|
||||
with open("tests/data/test_artifact.jpg", "rb") as f:
|
||||
photo = f.read()
|
||||
async with aiofiles.open("tests/data/test_artifact.jpg", mode="rb") as f:
|
||||
photo = await f.read()
|
||||
data = await artifact_rate.get_artifact_attr(photo)
|
||||
LOGGER.info(data.text)
|
||||
assert data.status_code == 200
|
||||
|
Loading…
Reference in New Issue
Block a user