koishi-plugin-steam-info-check 1.0.10 → 1.1.1

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 (37) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +75 -56
  3. package/dist/locales/zh-CN.d.ts +3 -0
  4. package/dist/locales/zh-CN.js +3 -0
  5. package/nonebot-plugin-steam-info-main/.github/actions/setup-python/action.yml +21 -0
  6. package/nonebot-plugin-steam-info-main/.github/workflows/release.yml +37 -0
  7. package/nonebot-plugin-steam-info-main/LICENSE +21 -0
  8. package/nonebot-plugin-steam-info-main/README.md +117 -0
  9. package/nonebot-plugin-steam-info-main/fonts/MiSans-Bold.ttf +0 -0
  10. package/nonebot-plugin-steam-info-main/fonts/MiSans-Light.ttf +0 -0
  11. package/nonebot-plugin-steam-info-main/fonts/MiSans-Regular.ttf +0 -0
  12. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/__init__.py +487 -0
  13. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/config.py +19 -0
  14. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/data_source.py +264 -0
  15. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/draw.py +921 -0
  16. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/models.py +82 -0
  17. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/bg_dots.png +0 -0
  18. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/busy.png +0 -0
  19. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/default_achievement_image.png +0 -0
  20. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/default_header_image.jpg +0 -0
  21. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/friends_search.png +0 -0
  22. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/gaming.png +0 -0
  23. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/parent_status.png +0 -0
  24. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/unknown_avatar.jpg +0 -0
  25. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/zzz_gaming.png +0 -0
  26. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/res/zzz_online.png +0 -0
  27. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/steam.py +259 -0
  28. package/nonebot-plugin-steam-info-main/nonebot_plugin_steam_info/utils.py +112 -0
  29. package/nonebot-plugin-steam-info-main/pdm.lock +966 -0
  30. package/nonebot-plugin-steam-info-main/preview.png +0 -0
  31. package/nonebot-plugin-steam-info-main/preview_1.png +0 -0
  32. package/nonebot-plugin-steam-info-main/preview_2.png +0 -0
  33. package/nonebot-plugin-steam-info-main/pyproject.toml +29 -0
  34. package/package.json +1 -1
  35. package/src/index.ts +58 -58
  36. package/src/locales/zh-CN.ts +3 -0
  37. package/src/locales/zh-CN.yml +2 -1
@@ -0,0 +1,487 @@
1
+ import time
2
+ import httpx
3
+ import nonebot
4
+ from io import BytesIO
5
+ from pathlib import Path
6
+ from nonebot.log import logger
7
+ from PIL import Image as PILImage
8
+ from nonebot.params import Depends
9
+ from nonebot.params import CommandArg
10
+ from nonebot import on_command, require
11
+ from typing import Union, Optional, List, Dict
12
+ from nonebot.adapters import Message, Event, Bot
13
+ from nonebot.plugin import PluginMetadata, inherit_supported_adapters
14
+
15
+ require("nonebot_plugin_alconna")
16
+ require("nonebot_plugin_localstore")
17
+ require("nonebot_plugin_apscheduler")
18
+
19
+ import nonebot_plugin_localstore as store
20
+ from nonebot_plugin_apscheduler import scheduler
21
+ from nonebot_plugin_alconna import Text, Image, UniMessage, Target, At, MsgTarget
22
+
23
+ from .config import Config
24
+ from .models import ProcessedPlayer
25
+ from .data_source import BindData, SteamInfoData, ParentData, DisableParentData
26
+ from .steam import (
27
+ get_steam_id,
28
+ get_user_data,
29
+ STEAM_ID_OFFSET,
30
+ get_steam_users_info,
31
+ )
32
+ from .draw import (
33
+ check_font,
34
+ set_font_paths,
35
+ draw_start_gaming,
36
+ draw_player_status,
37
+ draw_friends_status,
38
+ vertically_concatenate_images,
39
+ )
40
+ from .utils import (
41
+ fetch_avatar,
42
+ image_to_bytes,
43
+ simplize_steam_player_data,
44
+ convert_player_name_to_nickname,
45
+ )
46
+
47
+
48
+ __plugin_meta__ = PluginMetadata(
49
+ name="Steam Info",
50
+ description="播报绑定的 Steam 好友状态",
51
+ usage="""
52
+ steamhelp: 查看帮助
53
+ steambind [Steam ID 或 Steam 好友代码]: 绑定 Steam ID
54
+ steamunbind: 解绑 Steam ID
55
+ steaminfo (可选)[@某人 或 Steam ID 或 Steam好友代码]: 查看 Steam 主页
56
+ steamcheck: 查看 Steam 好友状态
57
+ steamenable: 启用 Steam 播报
58
+ steamdisable: 禁用 Steam 播报
59
+ steamupdate [名称] [图片]: 更新群信息
60
+ steamnickname [昵称]: 设置玩家昵称
61
+ """.strip(),
62
+ type="application",
63
+ homepage="https://github.com/zhaomaoniu/nonebot-plugin-steam-info",
64
+ config=Config,
65
+ supported_adapters=inherit_supported_adapters("nonebot_plugin_alconna"),
66
+ )
67
+
68
+
69
+ help = on_command("steamhelp", aliases={"steam帮助"}, priority=10)
70
+ bind = on_command("steambind", aliases={"绑定steam"}, priority=10)
71
+ unbind = on_command("steamunbind", aliases={"解绑steam"}, priority=10)
72
+ info = on_command("steaminfo", aliases={"steam信息"}, priority=10)
73
+ check = on_command("steamcheck", aliases={"查看steam", "查steam"}, priority=10)
74
+ enable = on_command("steamenable", aliases={"启用steam"}, priority=10)
75
+ disable = on_command("steamdisable", aliases={"禁用steam"}, priority=10)
76
+ update_parent_info = on_command("steamupdate", aliases={"更新群信息"}, priority=10)
77
+ set_nickname = on_command("steamnickname", aliases={"steam昵称"}, priority=10)
78
+
79
+
80
+ if hasattr(nonebot, "get_plugin_config"):
81
+ config = nonebot.get_plugin_config(Config)
82
+ else:
83
+ from nonebot import get_driver
84
+
85
+ config = Config.parse_obj(get_driver().config)
86
+
87
+ set_font_paths(
88
+ config.steam_font_regular_path,
89
+ config.steam_font_light_path,
90
+ config.steam_font_bold_path,
91
+ )
92
+
93
+ bind_data_path = store.get_data_file("nonebot_plugin_steam_info", "bind_data.json")
94
+ steam_info_data_path = store.get_data_file(
95
+ "nonebot_plugin_steam_info", "steam_info.json"
96
+ )
97
+ parent_data_path = store.get_data_file("nonebot_plugin_steam_info", "parent_data.json")
98
+ disable_parent_data_path = store.get_data_file(
99
+ "nonebot_plugin_steam_info", "disable_parent_data.json"
100
+ )
101
+ avatar_path = store.get_cache_dir("nonebot_plugin_steam_info")
102
+ cache_path = avatar_path
103
+
104
+ bind_data = BindData(bind_data_path)
105
+ steam_info_data = SteamInfoData(steam_info_data_path)
106
+ parent_data = ParentData(parent_data_path)
107
+ disable_parent_data = DisableParentData(disable_parent_data_path)
108
+
109
+ try:
110
+ check_font()
111
+ except FileNotFoundError as e:
112
+ logger.error(
113
+ f"{e}, nonebot_plugin_steam_info 无法使用,请参照 `https://github.com/zhaomaoniu/nonebot-plugin-steam-info` 配置字体文件"
114
+ )
115
+
116
+
117
+ async def get_target(target: MsgTarget) -> Optional[Target]:
118
+ if target.private:
119
+ # 不支持私聊消息
120
+ return None
121
+
122
+ return target
123
+
124
+
125
+ async def to_image_data(image: Image) -> Union[BytesIO, bytes]:
126
+ if image.raw is not None:
127
+ return image.raw
128
+
129
+ if image.path is not None:
130
+ return Path(image.path).read_bytes()
131
+
132
+ if image.url is not None:
133
+ async with httpx.AsyncClient() as client:
134
+ response = await client.get(image.url)
135
+ if response.status_code != 200:
136
+ raise ValueError(f"无法获取图片数据: {response.status_code}")
137
+ return response.content
138
+
139
+ raise ValueError("无法获取图片数据")
140
+
141
+
142
+ async def broadcast_steam_info(
143
+ parent_id: str,
144
+ old_players: List[ProcessedPlayer],
145
+ new_players: List[ProcessedPlayer],
146
+ ):
147
+ if disable_parent_data.is_disabled(parent_id):
148
+ return None
149
+
150
+ bot = nonebot.get_bot()
151
+
152
+ play_data = steam_info_data.compare(old_players, new_players)
153
+
154
+ msg = []
155
+ for entry in play_data:
156
+ player: ProcessedPlayer = entry["player"]
157
+ old_player: ProcessedPlayer = entry.get("old_player")
158
+
159
+ if entry["type"] == "start":
160
+ msg.append(f"{player['personaname']} 开始玩 {player['gameextrainfo']} 了")
161
+ elif entry["type"] in ["stop", "change"]:
162
+ time_start = old_player["game_start_time"]
163
+ time_stop = time.time()
164
+ hours = int((time_stop - time_start) / 3600)
165
+ minutes = int((time_stop - time_start) % 3600 / 60)
166
+ time_str = (
167
+ f"{hours} 小时 {minutes} 分钟" if hours > 0 else f"{minutes} 分钟"
168
+ )
169
+
170
+ if entry["type"] == "change":
171
+ msg.append(
172
+ f"{player['personaname']} 玩了 {time_str} {old_player['gameextrainfo']} 后,开始玩 {player['gameextrainfo']} 了"
173
+ )
174
+ else:
175
+ msg.append(
176
+ f"{player['personaname']} 玩了 {time_str} {old_player['gameextrainfo']} 后不玩了"
177
+ )
178
+ elif entry["type"] == "error":
179
+ f"出现错误!{player['personaname']}\nNew: {player.get('gameextrainfo')}\nOld: {old_player.get('gameextrainfo')}"
180
+ else:
181
+ logger.error(f"未知的播报类型: {entry['type']}")
182
+
183
+ if msg == []:
184
+ return None
185
+
186
+ if config.steam_broadcast_type == "all":
187
+ steam_status_data = [
188
+ convert_player_name_to_nickname(
189
+ (await simplize_steam_player_data(player, config.proxy, avatar_path)),
190
+ parent_id,
191
+ bind_data,
192
+ )
193
+ for player in new_players
194
+ ]
195
+
196
+ parent_avatar, parent_name = parent_data.get(parent_id)
197
+ image = draw_friends_status(parent_avatar, parent_name, steam_status_data)
198
+ uni_msg = UniMessage([Text("\n".join(msg)), Image(raw=image_to_bytes(image))])
199
+ elif config.steam_broadcast_type == "part":
200
+ images = [
201
+ draw_start_gaming(
202
+ (await fetch_avatar(entry["player"], avatar_path, config.proxy)),
203
+ entry["player"]["personaname"],
204
+ entry["player"]["gameextrainfo"],
205
+ bind_data.get_by_steam_id(parent_id, entry["player"]["steamid"])[
206
+ "nickname"
207
+ ],
208
+ )
209
+ for entry in play_data
210
+ if entry["type"] == "start"
211
+ ]
212
+ if images == []:
213
+ uni_msg = UniMessage([Text("\n".join(msg))])
214
+ else:
215
+ image = (
216
+ vertically_concatenate_images(images) if len(images) > 1 else images[0]
217
+ )
218
+ uni_msg = UniMessage(
219
+ [Text("\n".join(msg)), Image(raw=image_to_bytes(image))]
220
+ )
221
+ elif config.steam_broadcast_type == "none":
222
+ uni_msg = UniMessage([Text("\n".join(msg))])
223
+ else:
224
+ logger.error(f"未知的播报类型: {config.steam_broadcast_type}")
225
+ return None
226
+
227
+ await uni_msg.send(
228
+ Target(parent_id, parent_id, True, False, "", bot.adapter.get_name()), bot
229
+ )
230
+
231
+
232
+ async def update_steam_info():
233
+ steam_ids = bind_data.get_all_steam_id()
234
+
235
+ steam_info = await get_steam_users_info(
236
+ steam_ids, config.steam_api_key, config.proxy
237
+ )
238
+
239
+ old_players_dict: Dict[str, List[ProcessedPlayer]] = {}
240
+
241
+ for parent_id in bind_data.content.keys():
242
+ steam_ids = bind_data.get_all(parent_id)
243
+ old_players_dict[parent_id] = steam_info_data.get_players(steam_ids)
244
+
245
+ if steam_info["response"]["players"] != []:
246
+ steam_info_data.update_by_players(steam_info["response"]["players"])
247
+ steam_info_data.save()
248
+
249
+ return bind_data, old_players_dict
250
+
251
+
252
+ @scheduler.scheduled_job(
253
+ "interval", minutes=config.steam_request_interval / 60, id="update_steam_info"
254
+ )
255
+ async def fetch_and_broadcast_steam_info():
256
+ bind_data, old_players_dict = await update_steam_info()
257
+
258
+ for parent_id in bind_data.content.keys():
259
+ old_players = old_players_dict[parent_id]
260
+ new_players = steam_info_data.get_players(bind_data.get_all(parent_id))
261
+
262
+ await broadcast_steam_info(parent_id, old_players, new_players)
263
+
264
+
265
+ if not config.steam_disable_broadcast_on_startup:
266
+ nonebot.get_driver().on_bot_connect(update_steam_info)
267
+ else:
268
+ logger.info("已禁用启动时的 Steam 播报")
269
+
270
+
271
+ @help.handle()
272
+ async def help_handle():
273
+ await help.finish(__plugin_meta__.usage)
274
+
275
+
276
+ @bind.handle()
277
+ async def bind_handle(
278
+ event: Event, target: Target = Depends(get_target), cmd_arg: Message = CommandArg()
279
+ ):
280
+ parent_id = target.parent_id or target.id
281
+
282
+ arg = cmd_arg.extract_plain_text()
283
+
284
+ if not arg.isdigit():
285
+ await bind.finish(
286
+ "请输入正确的 Steam ID 或 Steam好友代码,格式: steambind [Steam ID 或 Steam好友代码]"
287
+ )
288
+
289
+ steam_id = get_steam_id(arg)
290
+
291
+ if user_data := bind_data.get(parent_id, event.get_user_id()):
292
+ user_data["steam_id"] = steam_id
293
+ bind_data.save()
294
+
295
+ await bind.finish(f"已更新你的 Steam ID 为 {steam_id}")
296
+ else:
297
+ bind_data.add(
298
+ parent_id,
299
+ {"user_id": event.get_user_id(), "steam_id": steam_id, "nickname": None},
300
+ )
301
+ bind_data.save()
302
+
303
+ await bind.finish(f"已绑定你的 Steam ID 为 {steam_id}")
304
+
305
+
306
+ @unbind.handle()
307
+ async def unbind_handle(event: Event, target: Target = Depends(get_target)):
308
+ parent_id = target.parent_id or target.id
309
+ user_id = event.get_user_id()
310
+
311
+ if bind_data.get(parent_id, user_id) is not None:
312
+ bind_data.remove(parent_id, user_id)
313
+ bind_data.save()
314
+
315
+ await unbind.finish("已解绑 Steam ID")
316
+ else:
317
+ await unbind.finish("未绑定 Steam ID")
318
+
319
+
320
+ @info.handle()
321
+ async def info_handle(
322
+ bot: Bot,
323
+ event: Event,
324
+ target: Target = Depends(get_target),
325
+ arg: Message = CommandArg(),
326
+ ):
327
+ parent_id = target.parent_id or target.id
328
+
329
+ uni_arg = await UniMessage.generate(message=arg, event=event, bot=bot)
330
+ at = uni_arg[At]
331
+
332
+ if len(at) != 0:
333
+ user_id: str = at[0].target
334
+ user_data = bind_data.get(parent_id, user_id)
335
+ if user_data is None:
336
+ await info.finish("该用户未绑定 Steam ID")
337
+ steam_id = user_data["steam_id"]
338
+ steam_friend_code = str(int(steam_id) - STEAM_ID_OFFSET)
339
+ elif arg.extract_plain_text().strip() != "":
340
+ steam_id = int(arg.extract_plain_text().strip())
341
+ if steam_id < STEAM_ID_OFFSET:
342
+ steam_friend_code = steam_id
343
+ steam_id += STEAM_ID_OFFSET
344
+ else:
345
+ steam_friend_code = steam_id - STEAM_ID_OFFSET
346
+ else:
347
+ user_data = bind_data.get(parent_id, event.get_user_id())
348
+
349
+ if user_data is None:
350
+ await info.finish(
351
+ "未绑定 Steam ID, 请使用 “steambind [Steam ID 或 Steam好友代码]” 绑定 Steam ID"
352
+ )
353
+
354
+ steam_id = user_data["steam_id"]
355
+ steam_friend_code = str(int(steam_id) - STEAM_ID_OFFSET)
356
+
357
+ player_data = await get_user_data(steam_id, cache_path, config.proxy)
358
+
359
+ draw_data = [
360
+ {
361
+ "game_header": game["game_image"],
362
+ "game_name": game["game_name"],
363
+ "game_time": f"{game['play_time']} 小时",
364
+ "last_play_time": game["last_played"],
365
+ "achievements": game["achievements"],
366
+ "completed_achievement_number": game.get("completed_achievement_number"),
367
+ "total_achievement_number": game.get("total_achievement_number"),
368
+ }
369
+ for game in player_data["game_data"]
370
+ ]
371
+
372
+ image = draw_player_status(
373
+ player_data["background"],
374
+ player_data["avatar"],
375
+ player_data["player_name"],
376
+ str(steam_friend_code),
377
+ player_data["description"],
378
+ player_data["recent_2_week_play_time"],
379
+ draw_data,
380
+ )
381
+
382
+ await info.finish(
383
+ await UniMessage(
384
+ Image(raw=image_to_bytes(image)),
385
+ ).export(bot)
386
+ )
387
+
388
+
389
+ @check.handle()
390
+ async def check_handle(
391
+ target: Target = Depends(get_target), arg: Message = CommandArg()
392
+ ):
393
+ if arg.extract_plain_text().strip() != "":
394
+ return None
395
+
396
+ parent_id = target.parent_id or target.id
397
+
398
+ steam_ids = bind_data.get_all(parent_id)
399
+
400
+ steam_info = await get_steam_users_info(
401
+ steam_ids, config.steam_api_key, config.proxy
402
+ )
403
+ if steam_info["response"]["players"] == []:
404
+ await check.finish("连接 Steam API 失败,请重试")
405
+
406
+ logger.debug(f"{parent_id} Players info: {steam_info}")
407
+
408
+ parent_avatar, parent_name = parent_data.get(parent_id)
409
+
410
+ steam_status_data = [
411
+ convert_player_name_to_nickname(
412
+ (await simplize_steam_player_data(player, config.proxy, avatar_path)),
413
+ parent_id,
414
+ bind_data,
415
+ )
416
+ for player in steam_info["response"]["players"]
417
+ ]
418
+
419
+ image = draw_friends_status(parent_avatar, parent_name, steam_status_data)
420
+
421
+ await target.send(UniMessage(Image(raw=image_to_bytes(image))))
422
+
423
+
424
+ @update_parent_info.handle()
425
+ async def update_parent_info_handle(
426
+ bot: Bot,
427
+ event: Event,
428
+ target: Target = Depends(get_target),
429
+ arg: Message = CommandArg(),
430
+ ):
431
+ msg = await UniMessage.generate(message=arg, event=event, bot=bot)
432
+ info = {}
433
+ for seg in msg:
434
+ if isinstance(seg, Image):
435
+ info["avatar"] = PILImage.open(BytesIO(await to_image_data(seg)))
436
+ elif isinstance(seg, Text) and seg.text != "":
437
+ info["name"] = seg.text
438
+
439
+ if "avatar" not in info or "name" not in info:
440
+ await update_parent_info.finish("文本中应包含图片和文字")
441
+
442
+ parent_data.update(target.parent_id or target.id, info["avatar"], info["name"])
443
+ await update_parent_info.finish("更新成功")
444
+
445
+
446
+ @enable.handle()
447
+ async def enable_handle(target: Target = Depends(get_target)):
448
+ parent_id = target.parent_id or target.id
449
+
450
+ disable_parent_data.remove(parent_id)
451
+ disable_parent_data.save()
452
+
453
+ await enable.finish("已启用 Steam 播报")
454
+
455
+
456
+ @disable.handle()
457
+ async def disable_handle(target: Target = Depends(get_target)):
458
+ parent_id = target.parent_id or target.id
459
+
460
+ disable_parent_data.add(parent_id)
461
+ disable_parent_data.save()
462
+
463
+ await disable.finish("已禁用 Steam 播报")
464
+
465
+
466
+ @set_nickname.handle()
467
+ async def set_nickname_handle(
468
+ event: Event, target: Target = Depends(get_target), cmd_arg: Message = CommandArg()
469
+ ):
470
+ parent_id = target.parent_id or target.id
471
+
472
+ nickname = cmd_arg.extract_plain_text().strip()
473
+
474
+ if nickname == "":
475
+ await set_nickname.finish("请输入昵称,格式: steamnickname [昵称]")
476
+
477
+ user_data = bind_data.get(parent_id, event.get_user_id())
478
+
479
+ if user_data is None:
480
+ await set_nickname.finish(
481
+ "未绑定 Steam ID,请先使用 steambind 绑定 Steam ID 后再设置昵称"
482
+ )
483
+
484
+ user_data["nickname"] = nickname
485
+ bind_data.save()
486
+
487
+ await set_nickname.finish(f"已设置你的昵称为 {nickname},将在 Steam 播报中显示")
@@ -0,0 +1,19 @@
1
+ from typing import Optional, Union, List
2
+ from pydantic import BaseModel, validator
3
+
4
+
5
+ class Config(BaseModel):
6
+ steam_api_key: Union[str, List[str]]
7
+ proxy: Optional[str] = None
8
+ steam_request_interval: int = 300 # seconds
9
+ steam_broadcast_type: str = "part" # all, part, none
10
+ steam_disable_broadcast_on_startup: bool = False
11
+ steam_font_regular_path: Optional[str] = "fonts/MiSans-Regular.ttf"
12
+ steam_font_light_path: Optional[str] = "fonts/MiSans-Light.ttf"
13
+ steam_font_bold_path: Optional[str] = "fonts/MiSans-Bold.ttf"
14
+
15
+ @validator("steam_api_key", pre=True)
16
+ def ensure_list(cls, v):
17
+ if isinstance(v, str):
18
+ return [v]
19
+ return v