nonebot-plugin-rollpig 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,18 @@
1
+ import json, random, datetime
2
+ from pathlib import Path
3
+
1
4
  from nonebot import on_command, require
5
+ from nonebot.adapters.onebot.v11 import Event, MessageSegment
6
+ from nonebot.log import logger
2
7
  from nonebot.plugin import PluginMetadata
3
- from nonebot.adapters.onebot.v11 import MessageSegment
4
8
 
5
9
  # 确保依赖插件先被 NoneBot 注册
6
- require("nonebot_plugin_apscheduler")
7
10
  require("nonebot_plugin_htmlrender")
8
11
  require("nonebot_plugin_localstore")
9
12
 
10
- from nonebot_plugin_apscheduler import scheduler
11
13
  from nonebot_plugin_htmlrender import template_to_pic
12
14
  import nonebot_plugin_localstore as store
13
15
 
14
- from nonebot.log import logger
15
- import random
16
- import json
17
- import datetime
18
- from pathlib import Path
19
-
20
16
  # 插件配置页
21
17
  __plugin_meta__ = PluginMetadata(
22
18
  name="今天是什么小猪",
@@ -38,7 +34,7 @@ RES_DIR = PLUGIN_DIR / "resource"
38
34
  # 今日记录
39
35
  TODAY_PATH = store.get_plugin_data_file("today.json")
40
36
 
41
- cmd = on_command("今天是什么小猪", aliases={"今日小猪", "我是什么小猪"})
37
+ cmd = on_command("今天是什么小猪", aliases={"今日小猪"})
42
38
 
43
39
 
44
40
  def load_json(path, default):
@@ -47,7 +43,6 @@ def load_json(path, default):
47
43
  return default
48
44
  return json.loads(path.read_text("utf-8"))
49
45
 
50
-
51
46
  def save_json(path, data):
52
47
  path.write_text(
53
48
  json.dumps(data, ensure_ascii=False, indent=2),
@@ -56,7 +51,6 @@ def save_json(path, data):
56
51
 
57
52
  def find_image_file(pig_id: str) -> Path | None:
58
53
  exts = ["png", "jpg", "jpeg", "webp", "gif"]
59
-
60
54
  for ext in exts:
61
55
  file = IMAGE_DIR / f"{pig_id}.{ext}"
62
56
  if file.exists():
@@ -64,48 +58,46 @@ def find_image_file(pig_id: str) -> Path | None:
64
58
  return None
65
59
 
66
60
 
67
- # 0 点自动清空
68
- @scheduler.scheduled_job("cron", hour=0, minute=0)
69
- def reset_today():
70
- if TODAY_PATH.exists():
71
- TODAY_PATH.unlink()
72
- logger.info("已清空今日记录")
61
+ # 载入小猪信息
62
+ PIG_LIST = load_json(PIGINFO_PATH, [])
63
+ if not PIG_LIST:
64
+ logger.error("小猪信息为空或不存在,请检查资源文件!")
73
65
 
74
66
  # 主函数
75
67
  @cmd.handle()
76
- async def _(bot, event):
68
+ async def _(event: Event):
77
69
  today_str = datetime.date.today().isoformat()
70
+ user_id = str(event.user_id)
78
71
 
79
72
  # 读取今日缓存
80
- today_data = load_json(TODAY_PATH, {})
81
-
82
- # 确保当天有字典存储用户数据
83
- if today_str not in today_data:
84
- today_data[today_str] = {}
85
-
86
- user_id = str(event.user_id) # 使用 QQ 号作为 key
87
-
88
- # 不重复抽
89
- if user_id in today_data[today_str]:
90
- pig = today_data[today_str][user_id]
91
- await send_rendered_pig(event, pig)
73
+ today_cache = load_json(TODAY_PATH, {"date": "", "records": {}})
74
+
75
+ # 检查日期,如果不是今天,则清空记录
76
+ if today_cache.get("date") != today_str:
77
+ today_cache = {"date": today_str, "records": {}}
78
+
79
+ user_records = today_cache["records"]
80
+
81
+ # 如果用户今天已经抽过,直接发送结果
82
+ if user_id in user_records:
83
+ pig = user_records[user_id]
84
+ await send_rendered_pig(pig)
92
85
  return
93
86
 
94
- piglist = load_json(PIGINFO_PATH, [])
95
- if not piglist:
96
- logger.error("pig.json 里没找到小猪信息哦~")
87
+ if not PIG_LIST:
88
+ await cmd.finish("小猪信息加载失败,请检查后台报错!")
97
89
  return
98
90
 
99
- # 随机
100
- pig = random.choice(piglist)
91
+ # 随机抽取
92
+ pig = random.choice(PIG_LIST)
101
93
 
102
94
  # 保存当天该用户的抽取结果
103
- today_data[today_str][user_id] = pig
104
- save_json(TODAY_PATH, today_data)
95
+ user_records[user_id] = pig
96
+ save_json(TODAY_PATH, today_cache)
105
97
 
106
- await send_rendered_pig(event, pig)
98
+ await send_rendered_pig(pig)
107
99
 
108
- async def send_rendered_pig(event, pig_data):
100
+ async def send_rendered_pig(pig_data: dict):
109
101
 
110
102
  # 使用 id 字段作为图片名
111
103
  pig_id = pig_data.get("id", "")
@@ -130,4 +122,4 @@ async def send_rendered_pig(event, pig_data):
130
122
  },
131
123
  )
132
124
 
133
- await cmd.finish(MessageSegment.image(pic))
125
+ await cmd.finish(MessageSegment.image(pic))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nonebot_plugin_rollpig
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: 今天是什么小猪
5
5
  License: MIT
6
6
  License-File: LICENSE
@@ -1,4 +1,4 @@
1
- nonebot_plugin_rollpig/__init__.py,sha256=brN4rRpt41rC3Ow-A2lYEBhuJVl94IqrNFBmnutJhmM,3689
1
+ nonebot_plugin_rollpig/__init__.py,sha256=paIp_Tqb-qBhe3tbvD5kEm2QIAHf3Frr6kDOY-ZexwI,3607
2
2
  nonebot_plugin_rollpig/resource/image/android-pig.png,sha256=WwY2rtsABH8YbDDIoQDR6WdHW8qpStm_lF8eXZ4K-Tc,37845
3
3
  nonebot_plugin_rollpig/resource/image/apple-pig.png,sha256=HSh8SRguD65ZZLnbnTgtNiAqqNzZKuFijtKIC3TLZDU,41533
4
4
  nonebot_plugin_rollpig/resource/image/bacon.png,sha256=7G86_W17C14qCRDKCGuRES4yfidIrIMz2gxOat3Gnn8,55722
@@ -41,7 +41,7 @@ nonebot_plugin_rollpig/resource/image/zhuge-liang.png,sha256=yvF5hNUzBXJIHi-vwg5
41
41
  nonebot_plugin_rollpig/resource/image/zombie-pig.png,sha256=fKQlp_kI3x8wMlVUT6U-rilMLpH0TpT_-ViNVWBSbcM,20797
42
42
  nonebot_plugin_rollpig/resource/pig.json,sha256=PqBF8X5TLqK1dOEtrKBd51hHJ4_h5XY3afGaUKRe3YI,11905
43
43
  nonebot_plugin_rollpig/resource/template.html,sha256=vX7vbT3BmmsXhswiUIHqHNLds_CcUQIuZKdnz5bVthc,1060
44
- nonebot_plugin_rollpig-0.2.0.dist-info/licenses/LICENSE,sha256=zn7XqavdCw11Nln_m8Eu0S7C3rZ9C-VGbqbrVGeSTGg,1087
45
- nonebot_plugin_rollpig-0.2.0.dist-info/METADATA,sha256=vULjnmd6elL8jHvL6n1feRWg_6LMJm7Wm6Q_wuHPlLY,2658
46
- nonebot_plugin_rollpig-0.2.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
47
- nonebot_plugin_rollpig-0.2.0.dist-info/RECORD,,
44
+ nonebot_plugin_rollpig-0.2.1.dist-info/licenses/LICENSE,sha256=zn7XqavdCw11Nln_m8Eu0S7C3rZ9C-VGbqbrVGeSTGg,1087
45
+ nonebot_plugin_rollpig-0.2.1.dist-info/METADATA,sha256=X8THu5giwml8LXvGdIai2epotm-FP4fAtbhKB1WK7Ig,2658
46
+ nonebot_plugin_rollpig-0.2.1.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
47
+ nonebot_plugin_rollpig-0.2.1.dist-info/RECORD,,