nonebot-plugin-bugu 0.2.0__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.
Files changed (35) hide show
  1. nonebot_plugin_bugu/__init__.py +31 -0
  2. nonebot_plugin_bugu/api.py +282 -0
  3. nonebot_plugin_bugu/collection.py +166 -0
  4. nonebot_plugin_bugu/commands.py +234 -0
  5. nonebot_plugin_bugu/config.py +91 -0
  6. nonebot_plugin_bugu/draw.py +406 -0
  7. nonebot_plugin_bugu/features/__init__.py +0 -0
  8. nonebot_plugin_bugu/features/collection.py +247 -0
  9. nonebot_plugin_bugu/features/common.py +100 -0
  10. nonebot_plugin_bugu/features/flowers/__init__.py +0 -0
  11. nonebot_plugin_bugu/features/flowers/data.py +167 -0
  12. nonebot_plugin_bugu/features/flowers/render.py +300 -0
  13. nonebot_plugin_bugu/features/flowers/reply.py +123 -0
  14. nonebot_plugin_bugu/features/neighbors/__init__.py +0 -0
  15. nonebot_plugin_bugu/features/neighbors/data.py +143 -0
  16. nonebot_plugin_bugu/features/neighbors/render.py +225 -0
  17. nonebot_plugin_bugu/features/neighbors/reply.py +149 -0
  18. nonebot_plugin_bugu/features/recipes/__init__.py +0 -0
  19. nonebot_plugin_bugu/features/recipes/data.py +313 -0
  20. nonebot_plugin_bugu/features/recipes/render.py +371 -0
  21. nonebot_plugin_bugu/features/recipes/reply.py +219 -0
  22. nonebot_plugin_bugu/features/recipes/search.py +151 -0
  23. nonebot_plugin_bugu/features/starsea/__init__.py +0 -0
  24. nonebot_plugin_bugu/features/starsea/data.py +267 -0
  25. nonebot_plugin_bugu/features/starsea/render.py +579 -0
  26. nonebot_plugin_bugu/features/starsea/reply.py +356 -0
  27. nonebot_plugin_bugu/features/starsea/search.py +271 -0
  28. nonebot_plugin_bugu/fonts.py +152 -0
  29. nonebot_plugin_bugu/runtime.py +109 -0
  30. nonebot_plugin_bugu/values.py +87 -0
  31. nonebot_plugin_bugu-0.2.0.dist-info/METADATA +257 -0
  32. nonebot_plugin_bugu-0.2.0.dist-info/RECORD +35 -0
  33. nonebot_plugin_bugu-0.2.0.dist-info/WHEEL +4 -0
  34. nonebot_plugin_bugu-0.2.0.dist-info/entry_points.txt +3 -0
  35. nonebot_plugin_bugu-0.2.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,143 @@
1
+ """友邻备忘(``/api/neighbors``)的数据模型。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, field
6
+ from typing import Any, Dict, List, Optional
7
+
8
+ from ...values import absolute_url, as_str, clean_text
9
+
10
+
11
+ @dataclass
12
+ class NeighborEntry:
13
+ """一条词条:喜爱 / 好感 / 反感 / 关于。"""
14
+
15
+ name: str
16
+ icon: str
17
+ detail: str
18
+ ability: str
19
+ ability_text: str
20
+
21
+
22
+ @dataclass
23
+ class NeighborProfile:
24
+ origin: str
25
+ origin_icon: str
26
+ birthday: str
27
+ job: str
28
+ intro_title: str
29
+ intro_text: str
30
+ likes: List[NeighborEntry] = field(default_factory=list)
31
+ goodwill: List[NeighborEntry] = field(default_factory=list)
32
+ dislikes: List[NeighborEntry] = field(default_factory=list)
33
+ about: List[NeighborEntry] = field(default_factory=list)
34
+
35
+ def section(self, key: str) -> List[NeighborEntry]:
36
+ return {
37
+ "likes": self.likes,
38
+ "goodwill": self.goodwill,
39
+ "dislikes": self.dislikes,
40
+ "about": self.about,
41
+ }.get(key, [])
42
+
43
+
44
+ SECTION_LABELS = {
45
+ "likes": "喜爱",
46
+ "goodwill": "好感",
47
+ "dislikes": "反感",
48
+ "about": "关于",
49
+ }
50
+
51
+
52
+ @dataclass
53
+ class Neighbor:
54
+ name: str
55
+ portrait: str
56
+ avatar: str
57
+ profile: NeighborProfile
58
+
59
+
60
+ @dataclass
61
+ class NeighborBook:
62
+ neighbors: List[Neighbor]
63
+ icons: Dict[str, str] = field(default_factory=dict)
64
+
65
+ def find(self, query: str) -> Optional[Neighbor]:
66
+ text = (query or "").strip()
67
+ if not text:
68
+ return None
69
+ for neighbor in self.neighbors:
70
+ if neighbor.name == text:
71
+ return neighbor
72
+ for neighbor in self.neighbors:
73
+ if text in neighbor.name:
74
+ return neighbor
75
+ return None
76
+
77
+ def origins(self) -> List[str]:
78
+ seen: List[str] = []
79
+ for neighbor in self.neighbors:
80
+ origin = neighbor.profile.origin
81
+ if origin and origin not in seen:
82
+ seen.append(origin)
83
+ return seen
84
+
85
+
86
+ def _parse_entries(raw: Any, base: str) -> List[NeighborEntry]:
87
+ entries: List[NeighborEntry] = []
88
+ for item in raw or []:
89
+ if not isinstance(item, dict):
90
+ continue
91
+ name = clean_text(item.get("name"))
92
+ if not name:
93
+ continue
94
+ entries.append(
95
+ NeighborEntry(
96
+ name=name,
97
+ icon=absolute_url(base, as_str(item.get("icon"))),
98
+ detail=clean_text(item.get("detail")),
99
+ ability=clean_text(item.get("ability")),
100
+ ability_text=clean_text(item.get("abilityText")),
101
+ )
102
+ )
103
+ return entries
104
+
105
+
106
+ def parse_neighbor(raw: Dict[str, Any], base: str) -> Neighbor:
107
+ profile_raw = raw.get("profile") if isinstance(raw.get("profile"), dict) else {}
108
+ profile = NeighborProfile(
109
+ origin=clean_text(profile_raw.get("origin")),
110
+ origin_icon=absolute_url(base, as_str(profile_raw.get("originIcon"))),
111
+ birthday=clean_text(profile_raw.get("birthday")),
112
+ job=clean_text(profile_raw.get("job")),
113
+ intro_title=clean_text(profile_raw.get("introTitle")),
114
+ intro_text=clean_text(profile_raw.get("introText")),
115
+ likes=_parse_entries(profile_raw.get("likes"), base),
116
+ goodwill=_parse_entries(profile_raw.get("goodwill"), base),
117
+ dislikes=_parse_entries(profile_raw.get("dislikes"), base),
118
+ about=_parse_entries(profile_raw.get("about"), base),
119
+ )
120
+ return Neighbor(
121
+ name=clean_text(raw.get("name")),
122
+ portrait=absolute_url(base, as_str(raw.get("portrait"))),
123
+ avatar=absolute_url(base, as_str(raw.get("avatar"))),
124
+ profile=profile,
125
+ )
126
+
127
+
128
+ def parse_neighbors(payload: Dict[str, Any], base: str) -> NeighborBook:
129
+ if not isinstance(payload, dict):
130
+ raise ValueError("接口返回的不是 JSON 对象")
131
+ raw_list = payload.get("neighbors")
132
+ if not isinstance(raw_list, list) or not raw_list:
133
+ raise ValueError("接口没有返回任何友邻")
134
+
135
+ icons = {
136
+ as_str(key): absolute_url(base, as_str(value))
137
+ for key, value in (payload.get("icons") or {}).items()
138
+ if isinstance(value, str)
139
+ }
140
+ return NeighborBook(
141
+ neighbors=[parse_neighbor(item, base) for item in raw_list if isinstance(item, dict)],
142
+ icons=icons,
143
+ )
@@ -0,0 +1,225 @@
1
+ """友邻档案卡:左边立绘,右边资料与介绍,下方是喜爱 / 好感 / 反感 / 关于。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Dict, List, Optional, Sequence, Tuple
6
+
7
+ from ...draw import (
8
+ CREAM,
9
+ DIM,
10
+ FAINT,
11
+ LINE,
12
+ TILE_BG,
13
+ YELLOW,
14
+ Draw,
15
+ bold_text,
16
+ chip,
17
+ cover,
18
+ draw_paragraph,
19
+ finish,
20
+ fit_text,
21
+ open_image,
22
+ require_pillow,
23
+ resize,
24
+ resolve_fontset,
25
+ rounded,
26
+ text_width,
27
+ )
28
+ from ...fonts import FontSet
29
+ from .data import Neighbor, NeighborBook, NeighborEntry
30
+
31
+ SECTION_ORDER: List[Tuple[str, str]] = [
32
+ ("likes", "喜爱"),
33
+ ("goodwill", "好感"),
34
+ ("dislikes", "反感"),
35
+ ]
36
+
37
+
38
+ def _draw_entries(
39
+ canvas,
40
+ draw: Draw,
41
+ entries: Sequence[NeighborEntry],
42
+ images: Dict[str, Optional[bytes]],
43
+ x: int,
44
+ y: int,
45
+ max_width: int,
46
+ fonts: FontSet,
47
+ ) -> int:
48
+ """一排带图标的词条胶囊,按行流式排列,返回占用高度。"""
49
+ font = fonts.get(16)
50
+ radius = 15
51
+ row_h = 38
52
+ gap = 8
53
+ cursor_x = x
54
+ cursor_y = y
55
+ for entry in entries:
56
+ icon = open_image(images.get(entry.icon) if entry.icon else None, "词条图标")
57
+ text_w = text_width(draw, entry.name, font)
58
+ width = int(text_w + 26) + (radius * 2 + 10 if icon is not None else 0)
59
+ if cursor_x > x and cursor_x + width > x + max_width:
60
+ cursor_x = x
61
+ cursor_y += row_h + gap
62
+ draw.rounded_rectangle(
63
+ [cursor_x, cursor_y, cursor_x + width, cursor_y + row_h],
64
+ row_h // 2,
65
+ fill=(40, 52, 88),
66
+ )
67
+ text_x = cursor_x + 13
68
+ if icon is not None:
69
+ disc = resize(icon, (radius * 2, radius * 2))
70
+ mask = rounded(disc, radius)
71
+ canvas.alpha_composite(mask, (cursor_x + 6, cursor_y + 4))
72
+ text_x = cursor_x + 6 + radius * 2 + 8
73
+ draw.text((text_x, cursor_y + row_h // 2), entry.name, font=font, fill=CREAM, anchor="lm")
74
+ cursor_x += width + gap
75
+ return cursor_y - y + row_h
76
+
77
+
78
+ def _draw_about(
79
+ draw: Draw,
80
+ entries: Sequence[NeighborEntry],
81
+ x: int,
82
+ y: int,
83
+ max_width: int,
84
+ fonts: FontSet,
85
+ ) -> int:
86
+ """关于:词条名 + 友邻能力(解锁条件)。"""
87
+ cursor = y
88
+ for entry in entries:
89
+ cursor += draw_paragraph(
90
+ draw, f"· {entry.name}", fonts.get(17, True), x, cursor, max_width,
91
+ line_height=26, fill=CREAM, max_lines=2,
92
+ )
93
+ if entry.ability:
94
+ detail = entry.ability
95
+ if entry.ability_text:
96
+ detail += f":{entry.ability_text}"
97
+ cursor += draw_paragraph(
98
+ draw, detail, fonts.get(15), x + 16, cursor, max_width - 16,
99
+ line_height=23, fill=DIM, max_lines=4,
100
+ )
101
+ cursor += 6
102
+ return cursor - y
103
+
104
+
105
+ def render_neighbor_card(
106
+ book: NeighborBook,
107
+ neighbor: Neighbor,
108
+ images: Dict[str, Optional[bytes]],
109
+ *,
110
+ fonts: Optional[FontSet] = None,
111
+ width: int = 1000,
112
+ collected: Optional[set] = None,
113
+ ) -> bytes:
114
+ require_pillow()
115
+ font_set = resolve_fontset(fonts)
116
+
117
+ from PIL import Image, ImageDraw
118
+
119
+ pad = 36
120
+ portrait_w = 340
121
+ portrait_h = 356
122
+ right_x = pad + portrait_w + 30
123
+ right_w = width - right_x - pad
124
+
125
+ profile = neighbor.profile
126
+ about = profile.about
127
+ estimate = 36 + portrait_h + 40 + 4 * 90 + len(about) * 120 + 200
128
+ content = Image.new("RGBA", (width, estimate), (0, 0, 0, 0))
129
+ draw = ImageDraw.Draw(content)
130
+
131
+ # ---- 立绘
132
+ portrait = open_image(images.get(neighbor.portrait) if neighbor.portrait else None, "友邻立绘")
133
+ if portrait is not None:
134
+ art = cover(portrait, (portrait_w, portrait_h))
135
+ content.alpha_composite(resize(rounded(art, 18), (portrait_w, portrait_h)), (pad, pad))
136
+ draw.rounded_rectangle(
137
+ [pad, pad, pad + portrait_w - 1, pad + portrait_h - 1], 18, outline=LINE, width=2
138
+ )
139
+ else:
140
+ draw.rounded_rectangle(
141
+ [pad, pad, pad + portrait_w - 1, pad + portrait_h - 1],
142
+ 18,
143
+ fill=TILE_BG,
144
+ outline=LINE,
145
+ width=2,
146
+ )
147
+ draw.text(
148
+ (pad + portrait_w / 2, pad + portrait_h / 2),
149
+ neighbor.name,
150
+ font=font_set.get(30, True),
151
+ fill=DIM,
152
+ anchor="mm",
153
+ )
154
+
155
+ # ---- 右侧资料
156
+ cursor = pad + 2
157
+ name_font = font_set.get(42, True)
158
+ bold_text(draw, (right_x, cursor), fit_text(draw, neighbor.name, name_font, right_w), name_font, font_set)
159
+ cursor += 62
160
+
161
+ meta_chips: List[Tuple[str, Tuple[int, int, int], Tuple[int, int, int]]] = []
162
+ if profile.job:
163
+ meta_chips.append((profile.job, YELLOW, (42, 33, 24)))
164
+ if profile.origin:
165
+ meta_chips.append((profile.origin, (44, 58, 96), CREAM))
166
+ if profile.birthday:
167
+ meta_chips.append((profile.birthday, (44, 58, 96), CREAM))
168
+ chip_x = right_x
169
+ for text, fill, ink in meta_chips:
170
+ chip_x += chip(draw, chip_x, cursor, text, font_set.get(15, True), fill=fill, ink=ink, height=30, radius=15) + 8
171
+ cursor += 44
172
+
173
+ if profile.intro_title:
174
+ cursor += draw_paragraph(
175
+ draw, profile.intro_title, font_set.get(21, True), right_x, cursor, right_w,
176
+ line_height=30, fill=YELLOW, max_lines=2,
177
+ )
178
+ cursor += 6
179
+
180
+ if profile.intro_text:
181
+ cursor += draw_paragraph(
182
+ draw, profile.intro_text, font_set.get(16), right_x, cursor, right_w,
183
+ line_height=26, fill=(206, 212, 226), max_lines=9, paragraph_gap=True,
184
+ )
185
+
186
+ # ---- 分隔线
187
+ top_height = max(portrait_h + pad, cursor - pad) + 26
188
+ draw.line([(pad, top_height), (width - pad, top_height)], fill=LINE, width=1)
189
+ cursor = top_height + 24
190
+
191
+ # ---- 喜爱 / 好感 / 反感
192
+ for key, label in SECTION_ORDER:
193
+ entries = profile.section(key)
194
+ if not entries:
195
+ continue
196
+ bold_text(draw, (pad, cursor), label, font_set.get(19, True), font_set, fill=YELLOW)
197
+ draw.text(
198
+ (pad + text_width(draw, label, font_set.get(19, True)) + 10, cursor + 6),
199
+ str(len(entries)),
200
+ font=font_set.get(14),
201
+ fill=FAINT,
202
+ )
203
+ cursor += 32
204
+ cursor += _draw_entries(content, draw, entries, images, pad, cursor, width - pad * 2, font_set)
205
+ cursor += 20
206
+
207
+ # ---- 关于
208
+ if about:
209
+ bold_text(draw, (pad, cursor), "关于", font_set.get(19, True), font_set, fill=YELLOW)
210
+ draw.text(
211
+ (pad + text_width(draw, "关于", font_set.get(19, True)) + 10, cursor + 6),
212
+ str(len(about)),
213
+ font=font_set.get(14),
214
+ fill=FAINT,
215
+ )
216
+ cursor += 32
217
+ cursor += _draw_about(draw, about, pad, cursor, width - pad * 2, font_set)
218
+
219
+ return finish(
220
+ content,
221
+ width,
222
+ fonts=font_set,
223
+ pad=pad,
224
+ right_text=f"{profile.origin} · {profile.job}" if profile.job else profile.origin,
225
+ )
@@ -0,0 +1,149 @@
1
+ """友邻备忘的文字回复与命令逻辑。"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Dict, List
6
+
7
+ from ..common import Reply, ReplyEnv, render_card, usage
8
+ from .data import SECTION_LABELS, Neighbor, NeighborBook, NeighborEntry
9
+ from .render import render_neighbor_card
10
+
11
+ SECTION_ALIASES: Dict[str, str] = {
12
+ "喜爱": "likes",
13
+ "喜欢": "likes",
14
+ "礼物": "likes",
15
+ "好感": "goodwill",
16
+ "反感": "dislikes",
17
+ "讨厌": "dislikes",
18
+ "关于": "about",
19
+ "能力": "about",
20
+ "介绍": "intro",
21
+ "资料": "profile",
22
+ "全部": "all",
23
+ }
24
+
25
+
26
+ def list_text(book: NeighborBook) -> str:
27
+ if not book.neighbors:
28
+ return "布谷工具箱还没有友邻资料。"
29
+ lines = [f"友邻备忘 · {len(book.neighbors)} 位友邻", ""]
30
+ for neighbor in book.neighbors:
31
+ profile = neighbor.profile
32
+ parts = [neighbor.name]
33
+ if profile.origin:
34
+ parts.append(profile.origin)
35
+ if profile.job:
36
+ parts.append(profile.job)
37
+ if profile.birthday:
38
+ parts.append(profile.birthday)
39
+ lines.append("· " + " · ".join(parts))
40
+ lines.append("")
41
+ lines.append(usage("友邻 <名字> 查看档案,例如 友邻 云果"))
42
+ return "\n".join(lines)
43
+
44
+
45
+ def profile_text(neighbor: Neighbor) -> str:
46
+ profile = neighbor.profile
47
+ head = [f"【{neighbor.name}】" + (" · ".join(p for p in (profile.origin, profile.job) if p))]
48
+ meta = []
49
+ if profile.birthday:
50
+ meta.append(f"生日 {profile.birthday}")
51
+ if profile.origin:
52
+ meta.append(f"来源地 {profile.origin}")
53
+ if meta:
54
+ head.append(" · ".join(meta))
55
+ if profile.intro_title:
56
+ head.append(f"「{profile.intro_title}」")
57
+ if profile.intro_text:
58
+ head.append(profile.intro_text)
59
+ return "\n".join(head)
60
+
61
+
62
+ def entries_text(neighbor: Neighbor, section: str) -> str:
63
+ profile = neighbor.profile
64
+ label = SECTION_LABELS.get(section, section)
65
+ entries: List[NeighborEntry] = profile.section(section)
66
+ if not entries:
67
+ return f"{neighbor.name} 的「{label}」还没有内容。"
68
+ lines = [f"【{neighbor.name}】{label} {len(entries)} 条", ""]
69
+ for entry in entries:
70
+ lines.append(f"· {entry.name}")
71
+ if entry.detail:
72
+ for line in entry.detail.split("\n"):
73
+ if line.strip():
74
+ lines.append(f" {line.strip()}")
75
+ if entry.ability:
76
+ ability = entry.ability
77
+ if entry.ability_text:
78
+ ability += f"({entry.ability_text})"
79
+ lines.append(f" 能力:{ability}")
80
+ return "\n".join(lines)
81
+
82
+
83
+ def intro_text(neighbor: Neighbor) -> str:
84
+ profile = neighbor.profile
85
+ lines = [f"【{neighbor.name}】介绍"]
86
+ if profile.job:
87
+ lines.append(f"职业:{profile.job}")
88
+ if profile.intro_title:
89
+ lines.append(f"「{profile.intro_title}」")
90
+ if profile.intro_text:
91
+ lines.append("")
92
+ lines.extend(line for line in profile.intro_text.split("\n") if line.strip())
93
+ return "\n".join(lines)
94
+
95
+
96
+ def all_text(neighbor: Neighbor) -> str:
97
+ parts = [profile_text(neighbor), intro_text(neighbor)]
98
+ for key in ("likes", "goodwill", "dislikes", "about"):
99
+ parts.append(entries_text(neighbor, key))
100
+ return "\n\n".join(parts)
101
+
102
+
103
+ async def _neighbor_reply(book: NeighborBook, neighbor: Neighbor, env: ReplyEnv) -> Reply:
104
+ png = None
105
+ if env.can_render:
106
+ urls = [neighbor.portrait]
107
+ for key in ("likes", "goodwill", "dislikes", "about"):
108
+ urls.extend(entry.icon for entry in neighbor.profile.section(key) if entry.icon)
109
+ images = await env.client.get_images(urls)
110
+ png = await render_card(
111
+ render_neighbor_card,
112
+ book,
113
+ neighbor,
114
+ images,
115
+ fonts=env.fonts,
116
+ label="友邻档案卡",
117
+ )
118
+ return png if png is not None else profile_text(neighbor)
119
+
120
+
121
+ async def reply_neighbors(arg: str, env: ReplyEnv) -> Reply:
122
+ book = await env.client.get_neighbors()
123
+ arg = (arg or "").strip()
124
+ if not arg:
125
+ return list_text(book)
126
+
127
+ parts = arg.split(None, 1)
128
+ neighbor = book.find(parts[0])
129
+ if neighbor is None:
130
+ return (
131
+ f"没找到「{parts[0]}」这位友邻。\n"
132
+ f"现有:" + "、".join(n.name for n in book.neighbors)
133
+ )
134
+
135
+ section = ""
136
+ if len(parts) > 1:
137
+ section = SECTION_ALIASES.get(parts[1].strip(), "")
138
+
139
+ if not section or section == "profile":
140
+ return await _neighbor_reply(book, neighbor, env)
141
+ if section == "intro":
142
+ return intro_text(neighbor)
143
+ if section == "all":
144
+ return all_text(neighbor)
145
+ return entries_text(neighbor, section)
146
+
147
+
148
+ def section_choices() -> List[str]:
149
+ return list(SECTION_ALIASES.keys())
File without changes