Skip to content

Commit

Permalink
优化执行完成的内容输出
Browse files Browse the repository at this point in the history
  • Loading branch information
smoadrareun committed Mar 17, 2024
1 parent 5ef237b commit 3a9254a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 32 deletions.
14 changes: 10 additions & 4 deletions nonebot_plugin_bilifan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ async def _(matcher: Matcher, event: Event):
# )
# else:
await MessageFactory(forward_msg).send()
await matcher.send("或将此链接复制到手机B站打开:" + login_url)
except Exception:
logger.warning("二维码可能被风控,发送链接")
await matcher.send("或将此链接复制到手机B站打开:" + login_url)
await matcher.send("将此链接复制到手机B站打开:" + login_url)
while True:
a = await verify_login(auth_code, data_path)
if a:
Expand Down Expand Up @@ -128,7 +129,13 @@ async def _(matcher: Matcher, event: Event):
msg_path = Path().joinpath(f"data/bilifan/{event.get_user_id()}/login_info.txt")
if msg_path.is_file():
if event.get_user_id() in config:
await matcher.finish(f"{event.get_user_id()}的定时任务已存在")
users = await read_yaml(Path().joinpath('data/bilifan'))
cron = users.get('CRON', None)
try:
fields = cron.split(" ")
await matcher.finish(f"{event.get_user_id()}的定时任务已存在,将在每天{fields[0]}{fields[1]}分开始执行~")
except AttributeError:
await matcher.finish('定时格式不正确,请删除定时任务后重新设置')
else:
config[event.get_user_id()] = group_id
save_config(config)
Expand Down Expand Up @@ -164,7 +171,6 @@ async def _(matcher: Matcher):
msg_path.unlink()
await matcher.finish("已删除全部定时刷牌子任务")


@driver.on_bot_connect
async def _():
users = await read_yaml(Path().joinpath("data/bilifan"))
Expand All @@ -178,6 +184,7 @@ async def _():
logger.error("定时格式不正确,不启用定时功能")
return
try:
logger.info(f'定时任务已配置,将在每天{fields[0]}{fields[1]}分后自动执行~')
scheduler.add_job(
auto_cup,
"cron",
Expand All @@ -187,4 +194,3 @@ async def _():
)
except Exception:
logger.warning("定时任务已存在")
logger.warning("定时任务已存在")
4 changes: 2 additions & 2 deletions nonebot_plugin_bilifan/src/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ async def sendDanmaku(self):

async def init(self):
if not await self.loginVerify():
log.error("登录失败 可能是 access_key 过期 , 请重新获取")
self.errmsg.append("登录失败 可能是 access_key 过期 , 请重新获取")
log.error(f"登录失败 可能是 access_key{self.access_key} 过期 , 请重新获取")
self.errmsg.append("登录失败 可能是登录已过期 , 请发送【b站登录】重新登录")
await self.session.close()
else:
await self.doSign()
Expand Down
50 changes: 24 additions & 26 deletions nonebot_plugin_bilifan/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,35 @@ async def auto_cup():
tasks = []

for user_id, group_id in config.items(): # noqa: B007
msg_path = Path().joinpath(f"data/bilifan/{user_id}/login_info.txt")
if msg_path.is_file:
pass
msg_path = Path(f"data/bilifan/{user_id}/login_info.txt")
if msg_path.is_file():
task = asyncio.create_task(mains(msg_path.parent))
tasks.append((user_id, group_id, task))
else:
logger.warning("usr_id尚未登录,已忽略")
logger.warning("usr_id尚未登录,已忽略")
continue

task = asyncio.create_task(mains(msg_path.parent))
tasks.append(task)
logger.warning(f"{user_id}尚未登录,已忽略")

messageList = []
for task in asyncio.as_completed(tasks):
messageList.extend(await task)

message_str = "\n".join(messageList)
message_str = "\n".join(messageList)

if count:
for group_id, num in count.items():
for user_id, group_id, task in tasks:
message = await task
messageList.append((user_id, group_id, message))

for user_id, group_id, message in messageList:
messageStr = "".join(message)
logger.info(f"{user_id}用户自动刷牌子任务执行完成,{messageStr}")
if group_id.startswith("group"):
group_num = group_id.split("_")[1]
if group_num in count:
count[group_num] += 1
else:
count[group_num] = 1
await get_bot().send_private_msg(user_id=user_id, message=messageStr)

for group_num, num in count.items():
logger.info(f"{group_num}群组自动刷牌子任务执行完成,共{num}个")
await get_bot().send_group_msg(
group_id=group_id,
message=f"今日已完成{num}个自动刷牌子任务",
group_id=group_num,
message=f"本群今日已完成{num}个自动刷牌子任务",
)
if message_str:
for user_id, group_id in config.items():
if user_id == group_id:
await get_bot().send_private_msg(user_id=user_id, message=message_str)
continue
count_value = count.get(group_id, 0)
count[group_id] = count_value + 1


def render_forward_msg(msg_list: list, uid=2711142767, name="宁宁"):
Expand Down

0 comments on commit 3a9254a

Please sign in to comment.