nonebot-plugin-game-uid 0.2.0__tar.gz
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.
- nonebot_plugin_game_uid-0.2.0/LICENSE +21 -0
- nonebot_plugin_game_uid-0.2.0/PKG-INFO +147 -0
- nonebot_plugin_game_uid-0.2.0/README.md +118 -0
- nonebot_plugin_game_uid-0.2.0/nonebot_plugin_game_uid/__init__.py +40 -0
- nonebot_plugin_game_uid-0.2.0/nonebot_plugin_game_uid/commands.py +288 -0
- nonebot_plugin_game_uid-0.2.0/nonebot_plugin_game_uid/config.py +12 -0
- nonebot_plugin_game_uid-0.2.0/nonebot_plugin_game_uid/games.py +41 -0
- nonebot_plugin_game_uid-0.2.0/nonebot_plugin_game_uid/reminders.py +70 -0
- nonebot_plugin_game_uid-0.2.0/nonebot_plugin_game_uid/repository.py +225 -0
- nonebot_plugin_game_uid-0.2.0/pyproject.toml +60 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TonyLiangP2010405
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nonebot-plugin-game-uid
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: 记录、汇总和定时播报群友的星布谷地、原神、崩坏:星穹铁道与绝区零 UID
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: nonebot,nonebot2,uid,mihoyo,onebot
|
|
8
|
+
Author: TonyLiangP2010405
|
|
9
|
+
Requires-Python: >=3.9,<4.0
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Communications :: Chat
|
|
20
|
+
Requires-Dist: aiosqlite (>=0.19.0)
|
|
21
|
+
Requires-Dist: nonebot-adapter-onebot (>=2.4.0)
|
|
22
|
+
Requires-Dist: nonebot-plugin-apscheduler (>=0.5.0)
|
|
23
|
+
Requires-Dist: nonebot-plugin-localstore (>=0.7.0)
|
|
24
|
+
Requires-Dist: nonebot2 (>=2.2.0)
|
|
25
|
+
Project-URL: Homepage, https://github.com/TonyLiangP2010405/nonebot-plugin-game-uid
|
|
26
|
+
Project-URL: Repository, https://github.com/TonyLiangP2010405/nonebot-plugin-game-uid
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# nonebot-plugin-game-uid
|
|
30
|
+
|
|
31
|
+
一个用于记录和查询群友游戏 UID 的 NoneBot2 插件,支持星布谷地、原神、崩坏:星穹铁道和绝区零。
|
|
32
|
+
|
|
33
|
+
## 功能
|
|
34
|
+
|
|
35
|
+
- 每位 QQ 用户可分别绑定四款游戏的 UID
|
|
36
|
+
- 重复绑定同一游戏时自动更新 UID
|
|
37
|
+
- 查询自己的全部 UID 或指定游戏 UID
|
|
38
|
+
- 在群聊中通过 `@群友` 查询对方主动绑定的 UID
|
|
39
|
+
- 按“群名片:UID”列出本群已经绑定指定游戏的群友
|
|
40
|
+
- SUPERUSER 可设置定时播报,周期性提示群友使用 UID 列表命令
|
|
41
|
+
- 删除自己的指定游戏 UID
|
|
42
|
+
- 使用 SQLite 异步持久化,无需外部数据库或游戏 API
|
|
43
|
+
- 兼容“星布谷底”“星铁”“铁道”“崩铁”等常用别名
|
|
44
|
+
|
|
45
|
+
## 安装
|
|
46
|
+
|
|
47
|
+
### 使用 nb-cli 安装
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
nb plugin install nonebot-plugin-game-uid
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 使用 pip 安装
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
pip install nonebot-plugin-game-uid
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 使用 Poetry 安装
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
poetry add nonebot-plugin-game-uid
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
插件仅支持 OneBot V11 适配器。安装后请在 NoneBot 配置中加载 `nonebot_plugin_game_uid`。
|
|
66
|
+
|
|
67
|
+
## 配置
|
|
68
|
+
|
|
69
|
+
所有配置项都有默认值,插件可以零配置加载。数据库默认由 `nonebot-plugin-localstore` 保存到插件数据目录。如需自定义数据库位置,在 `.env` 中添加:
|
|
70
|
+
|
|
71
|
+
```env
|
|
72
|
+
GAME_UID_DB_PATH=data/game_uid/uid.db
|
|
73
|
+
GAME_UID_LIST_LIMIT=100
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
| 配置项 | 默认值 | 说明 |
|
|
77
|
+
|---|---|---|
|
|
78
|
+
| `game_uid_db_path` | 空(localstore 插件数据目录下的 `uid.db`) | 可选的自定义 SQLite 数据库文件路径 |
|
|
79
|
+
| `game_uid_list_limit` | `100` | 群 UID 列表单次最多显示人数 |
|
|
80
|
+
|
|
81
|
+
UID 按 QQ 用户全局保存,不按群分别存储。群友只能查询用户主动绑定的 UID。
|
|
82
|
+
|
|
83
|
+
## 使用方法
|
|
84
|
+
|
|
85
|
+
| 指令 | 权限 | 范围 | 说明 |
|
|
86
|
+
|---|---|---|---|
|
|
87
|
+
| `/绑定UID <游戏> <UID>` | 所有人 | 群聊/私聊 | 绑定或更新自己的 UID |
|
|
88
|
+
| `/游戏UID` | 所有人 | 群聊/私聊 | 查看自己的全部 UID |
|
|
89
|
+
| `/游戏UID [游戏]` | 所有人 | 群聊/私聊 | 查看自己的指定 UID |
|
|
90
|
+
| `/游戏UID [游戏] @群友` | 所有人 | 群聊 | 查看群友的指定 UID |
|
|
91
|
+
| `/游戏UID @群友` | 所有人 | 群聊 | 查看群友的全部 UID |
|
|
92
|
+
| `/删除UID <游戏>` | 所有人 | 群聊/私聊 | 删除自己的指定 UID |
|
|
93
|
+
| `/群UID <游戏>` | 所有人 | 群聊 | 按“群名片:UID”查看本群列表 |
|
|
94
|
+
| `/设置UID提醒 <间隔> <游戏>` | SUPERUSER | 群聊 | 开启或更新定时播报 |
|
|
95
|
+
| `/关闭UID提醒` | SUPERUSER | 群聊 | 关闭定时播报 |
|
|
96
|
+
| `/UID提醒状态` | 所有人 | 群聊 | 查看本群提醒状态 |
|
|
97
|
+
| `/UID帮助` | 所有人 | 群聊/私聊 | 查看命令帮助 |
|
|
98
|
+
|
|
99
|
+
支持的游戏名及常用别名:
|
|
100
|
+
|
|
101
|
+
- 星布谷地:`星布谷地`、`星布谷底`、`星布`、`谷地`、`谷底`
|
|
102
|
+
- 原神:`原神`、`ys`
|
|
103
|
+
- 崩坏:星穹铁道:`星铁`、`铁道`、`崩铁`、`星穹铁道`、`hsr`
|
|
104
|
+
- 绝区零:`绝区零`、`绝区`、`zzz`
|
|
105
|
+
|
|
106
|
+
UID 必须为 5~20 位半角数字。
|
|
107
|
+
|
|
108
|
+
提醒间隔支持 `分钟`、`小时`、`天`,范围为 10 分钟到 30 天。每个群保存一条提醒设置,再次设置会覆盖旧设置。机器人重启后设置仍然有效。
|
|
109
|
+
|
|
110
|
+
## 示例
|
|
111
|
+
|
|
112
|
+
```text
|
|
113
|
+
用户:/绑定UID 原神 100123456
|
|
114
|
+
机器人:已绑定原神 UID:100123456
|
|
115
|
+
|
|
116
|
+
用户:/游戏UID 原神
|
|
117
|
+
机器人:你的 UID:
|
|
118
|
+
原神:100123456
|
|
119
|
+
|
|
120
|
+
用户:/删除UID 原神
|
|
121
|
+
机器人:已删除原神 UID。
|
|
122
|
+
|
|
123
|
+
用户:/群UID 原神
|
|
124
|
+
机器人:本群原神 UID 列表(2人):
|
|
125
|
+
提瓦特旅行者:100123456
|
|
126
|
+
应急食品:100654321
|
|
127
|
+
|
|
128
|
+
SUPERUSER:/设置UID提醒 6小时 原神
|
|
129
|
+
机器人:已设置原神 UID 定时提醒,每 6小时发送一次。
|
|
130
|
+
提醒内容:
|
|
131
|
+
想查看本群群友的原神 UID 并添加游戏好友?
|
|
132
|
+
发送 /群UID 原神,即可查看群友主动绑定的 UID(群名片:UID)。
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## 开发
|
|
136
|
+
|
|
137
|
+
```bash
|
|
138
|
+
poetry install
|
|
139
|
+
poetry run pytest
|
|
140
|
+
poetry run ruff check .
|
|
141
|
+
poetry run python -m compileall nonebot_plugin_game_uid
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## 许可证
|
|
145
|
+
|
|
146
|
+
MIT
|
|
147
|
+
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# nonebot-plugin-game-uid
|
|
2
|
+
|
|
3
|
+
一个用于记录和查询群友游戏 UID 的 NoneBot2 插件,支持星布谷地、原神、崩坏:星穹铁道和绝区零。
|
|
4
|
+
|
|
5
|
+
## 功能
|
|
6
|
+
|
|
7
|
+
- 每位 QQ 用户可分别绑定四款游戏的 UID
|
|
8
|
+
- 重复绑定同一游戏时自动更新 UID
|
|
9
|
+
- 查询自己的全部 UID 或指定游戏 UID
|
|
10
|
+
- 在群聊中通过 `@群友` 查询对方主动绑定的 UID
|
|
11
|
+
- 按“群名片:UID”列出本群已经绑定指定游戏的群友
|
|
12
|
+
- SUPERUSER 可设置定时播报,周期性提示群友使用 UID 列表命令
|
|
13
|
+
- 删除自己的指定游戏 UID
|
|
14
|
+
- 使用 SQLite 异步持久化,无需外部数据库或游戏 API
|
|
15
|
+
- 兼容“星布谷底”“星铁”“铁道”“崩铁”等常用别名
|
|
16
|
+
|
|
17
|
+
## 安装
|
|
18
|
+
|
|
19
|
+
### 使用 nb-cli 安装
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
nb plugin install nonebot-plugin-game-uid
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### 使用 pip 安装
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install nonebot-plugin-game-uid
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### 使用 Poetry 安装
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
poetry add nonebot-plugin-game-uid
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
插件仅支持 OneBot V11 适配器。安装后请在 NoneBot 配置中加载 `nonebot_plugin_game_uid`。
|
|
38
|
+
|
|
39
|
+
## 配置
|
|
40
|
+
|
|
41
|
+
所有配置项都有默认值,插件可以零配置加载。数据库默认由 `nonebot-plugin-localstore` 保存到插件数据目录。如需自定义数据库位置,在 `.env` 中添加:
|
|
42
|
+
|
|
43
|
+
```env
|
|
44
|
+
GAME_UID_DB_PATH=data/game_uid/uid.db
|
|
45
|
+
GAME_UID_LIST_LIMIT=100
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
| 配置项 | 默认值 | 说明 |
|
|
49
|
+
|---|---|---|
|
|
50
|
+
| `game_uid_db_path` | 空(localstore 插件数据目录下的 `uid.db`) | 可选的自定义 SQLite 数据库文件路径 |
|
|
51
|
+
| `game_uid_list_limit` | `100` | 群 UID 列表单次最多显示人数 |
|
|
52
|
+
|
|
53
|
+
UID 按 QQ 用户全局保存,不按群分别存储。群友只能查询用户主动绑定的 UID。
|
|
54
|
+
|
|
55
|
+
## 使用方法
|
|
56
|
+
|
|
57
|
+
| 指令 | 权限 | 范围 | 说明 |
|
|
58
|
+
|---|---|---|---|
|
|
59
|
+
| `/绑定UID <游戏> <UID>` | 所有人 | 群聊/私聊 | 绑定或更新自己的 UID |
|
|
60
|
+
| `/游戏UID` | 所有人 | 群聊/私聊 | 查看自己的全部 UID |
|
|
61
|
+
| `/游戏UID [游戏]` | 所有人 | 群聊/私聊 | 查看自己的指定 UID |
|
|
62
|
+
| `/游戏UID [游戏] @群友` | 所有人 | 群聊 | 查看群友的指定 UID |
|
|
63
|
+
| `/游戏UID @群友` | 所有人 | 群聊 | 查看群友的全部 UID |
|
|
64
|
+
| `/删除UID <游戏>` | 所有人 | 群聊/私聊 | 删除自己的指定 UID |
|
|
65
|
+
| `/群UID <游戏>` | 所有人 | 群聊 | 按“群名片:UID”查看本群列表 |
|
|
66
|
+
| `/设置UID提醒 <间隔> <游戏>` | SUPERUSER | 群聊 | 开启或更新定时播报 |
|
|
67
|
+
| `/关闭UID提醒` | SUPERUSER | 群聊 | 关闭定时播报 |
|
|
68
|
+
| `/UID提醒状态` | 所有人 | 群聊 | 查看本群提醒状态 |
|
|
69
|
+
| `/UID帮助` | 所有人 | 群聊/私聊 | 查看命令帮助 |
|
|
70
|
+
|
|
71
|
+
支持的游戏名及常用别名:
|
|
72
|
+
|
|
73
|
+
- 星布谷地:`星布谷地`、`星布谷底`、`星布`、`谷地`、`谷底`
|
|
74
|
+
- 原神:`原神`、`ys`
|
|
75
|
+
- 崩坏:星穹铁道:`星铁`、`铁道`、`崩铁`、`星穹铁道`、`hsr`
|
|
76
|
+
- 绝区零:`绝区零`、`绝区`、`zzz`
|
|
77
|
+
|
|
78
|
+
UID 必须为 5~20 位半角数字。
|
|
79
|
+
|
|
80
|
+
提醒间隔支持 `分钟`、`小时`、`天`,范围为 10 分钟到 30 天。每个群保存一条提醒设置,再次设置会覆盖旧设置。机器人重启后设置仍然有效。
|
|
81
|
+
|
|
82
|
+
## 示例
|
|
83
|
+
|
|
84
|
+
```text
|
|
85
|
+
用户:/绑定UID 原神 100123456
|
|
86
|
+
机器人:已绑定原神 UID:100123456
|
|
87
|
+
|
|
88
|
+
用户:/游戏UID 原神
|
|
89
|
+
机器人:你的 UID:
|
|
90
|
+
原神:100123456
|
|
91
|
+
|
|
92
|
+
用户:/删除UID 原神
|
|
93
|
+
机器人:已删除原神 UID。
|
|
94
|
+
|
|
95
|
+
用户:/群UID 原神
|
|
96
|
+
机器人:本群原神 UID 列表(2人):
|
|
97
|
+
提瓦特旅行者:100123456
|
|
98
|
+
应急食品:100654321
|
|
99
|
+
|
|
100
|
+
SUPERUSER:/设置UID提醒 6小时 原神
|
|
101
|
+
机器人:已设置原神 UID 定时提醒,每 6小时发送一次。
|
|
102
|
+
提醒内容:
|
|
103
|
+
想查看本群群友的原神 UID 并添加游戏好友?
|
|
104
|
+
发送 /群UID 原神,即可查看群友主动绑定的 UID(群名片:UID)。
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## 开发
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
poetry install
|
|
111
|
+
poetry run pytest
|
|
112
|
+
poetry run ruff check .
|
|
113
|
+
poetry run python -m compileall nonebot_plugin_game_uid
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## 许可证
|
|
117
|
+
|
|
118
|
+
MIT
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from nonebot import require
|
|
2
|
+
from nonebot.plugin import PluginMetadata
|
|
3
|
+
|
|
4
|
+
from .config import Config
|
|
5
|
+
|
|
6
|
+
__plugin_meta__ = PluginMetadata(
|
|
7
|
+
name="群友游戏 UID",
|
|
8
|
+
description="记录、汇总和定时播报群友的星布谷地、原神、崩坏:星穹铁道与绝区零 UID",
|
|
9
|
+
usage=(
|
|
10
|
+
"/绑定UID <游戏> <UID>\n"
|
|
11
|
+
"/游戏UID [游戏] [@群友]\n"
|
|
12
|
+
"/删除UID <游戏>\n"
|
|
13
|
+
"/群UID <游戏>\n"
|
|
14
|
+
"/设置UID提醒 <间隔> <游戏>\n"
|
|
15
|
+
"/UID帮助"
|
|
16
|
+
),
|
|
17
|
+
type="application",
|
|
18
|
+
homepage="https://github.com/TonyLiangP2010405/nonebot-plugin-game-uid",
|
|
19
|
+
config=Config,
|
|
20
|
+
supported_adapters={"~onebot.v11"},
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
require("nonebot_plugin_apscheduler")
|
|
24
|
+
require("nonebot_plugin_localstore")
|
|
25
|
+
|
|
26
|
+
from nonebot_plugin_apscheduler import scheduler # noqa: E402
|
|
27
|
+
|
|
28
|
+
from . import commands as commands # noqa: E402,F401
|
|
29
|
+
from .reminders import dispatch_due_reminders # noqa: E402
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@scheduler.scheduled_job(
|
|
33
|
+
"interval",
|
|
34
|
+
minutes=1,
|
|
35
|
+
id="game_uid_dispatch_reminders",
|
|
36
|
+
coalesce=True,
|
|
37
|
+
max_instances=1,
|
|
38
|
+
)
|
|
39
|
+
async def _dispatch_reminders() -> None:
|
|
40
|
+
await dispatch_due_reminders()
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
import nonebot_plugin_localstore as localstore
|
|
7
|
+
from nonebot import get_plugin_config, logger, on_command
|
|
8
|
+
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, Message, MessageEvent, MessageSegment
|
|
9
|
+
from nonebot.params import CommandArg
|
|
10
|
+
from nonebot.permission import SUPERUSER
|
|
11
|
+
|
|
12
|
+
from .config import Config
|
|
13
|
+
from .games import GAME_BY_KEY, GAMES, Game, resolve_game, validate_uid
|
|
14
|
+
from .reminders import build_reminder_message, format_duration, parse_duration
|
|
15
|
+
from .repository import UIDRepository
|
|
16
|
+
|
|
17
|
+
BIND_USAGE = "用法:/绑定UID <游戏> <UID>\n例如:/绑定UID 原神 100123456"
|
|
18
|
+
QUERY_USAGE = "用法:/游戏UID [游戏] [@群友]\n例如:/游戏UID 星铁 @某位群友"
|
|
19
|
+
DELETE_USAGE = "用法:/删除UID <游戏>\n例如:/删除UID 绝区零"
|
|
20
|
+
GROUP_LIST_USAGE = "用法:/群UID <游戏>\n例如:/群UID 星布谷地"
|
|
21
|
+
REMINDER_USAGE = "用法:/设置UID提醒 <间隔> <游戏>\n例如:/设置UID提醒 6小时 原神\n间隔范围:10分钟~30天"
|
|
22
|
+
|
|
23
|
+
uid_bind = on_command(
|
|
24
|
+
"绑定UID",
|
|
25
|
+
aliases={"绑定uid", "绑定游戏UID", "绑定游戏uid"},
|
|
26
|
+
priority=10,
|
|
27
|
+
block=True,
|
|
28
|
+
)
|
|
29
|
+
uid_query = on_command(
|
|
30
|
+
"游戏UID",
|
|
31
|
+
aliases={"游戏uid", "UID", "uid", "我的UID", "我的uid"},
|
|
32
|
+
priority=10,
|
|
33
|
+
block=True,
|
|
34
|
+
)
|
|
35
|
+
uid_delete = on_command(
|
|
36
|
+
"删除UID",
|
|
37
|
+
aliases={"删除uid", "解绑UID", "解绑uid"},
|
|
38
|
+
priority=10,
|
|
39
|
+
block=True,
|
|
40
|
+
)
|
|
41
|
+
uid_help = on_command(
|
|
42
|
+
"UID帮助",
|
|
43
|
+
aliases={"uid帮助", "游戏UID帮助", "游戏uid帮助"},
|
|
44
|
+
priority=10,
|
|
45
|
+
block=True,
|
|
46
|
+
)
|
|
47
|
+
group_uid_list = on_command(
|
|
48
|
+
"群UID",
|
|
49
|
+
aliases={"群uid", "群友UID", "群友uid", "UID列表", "uid列表"},
|
|
50
|
+
priority=10,
|
|
51
|
+
block=True,
|
|
52
|
+
)
|
|
53
|
+
reminder_set = on_command(
|
|
54
|
+
"设置UID提醒",
|
|
55
|
+
aliases={"设置uid提醒"},
|
|
56
|
+
permission=SUPERUSER,
|
|
57
|
+
priority=10,
|
|
58
|
+
block=True,
|
|
59
|
+
)
|
|
60
|
+
reminder_close = on_command(
|
|
61
|
+
"关闭UID提醒",
|
|
62
|
+
aliases={"关闭uid提醒"},
|
|
63
|
+
permission=SUPERUSER,
|
|
64
|
+
priority=10,
|
|
65
|
+
block=True,
|
|
66
|
+
)
|
|
67
|
+
reminder_status = on_command(
|
|
68
|
+
"UID提醒状态",
|
|
69
|
+
aliases={"uid提醒状态"},
|
|
70
|
+
priority=10,
|
|
71
|
+
block=True,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
_repository: Optional[UIDRepository] = None
|
|
75
|
+
_repository_path: Optional[Path] = None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def get_repository() -> UIDRepository:
|
|
79
|
+
global _repository, _repository_path
|
|
80
|
+
|
|
81
|
+
configured_path = get_plugin_config(Config).game_uid_db_path
|
|
82
|
+
path = Path(configured_path).expanduser() if configured_path else localstore.get_plugin_data_file("uid.db")
|
|
83
|
+
if _repository is None or _repository_path != path:
|
|
84
|
+
_repository = UIDRepository(path)
|
|
85
|
+
_repository_path = path
|
|
86
|
+
return _repository
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def get_mentioned_user(message: Message) -> Optional[str]:
|
|
90
|
+
"""提取消息中的第一个 QQ @,忽略 @全体成员。"""
|
|
91
|
+
|
|
92
|
+
for segment in message:
|
|
93
|
+
if segment.type == "at":
|
|
94
|
+
qq = str(segment.data.get("qq", ""))
|
|
95
|
+
if qq and qq != "all":
|
|
96
|
+
return qq
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def parse_game_argument(text: str) -> tuple[Optional[Game], Optional[str]]:
|
|
101
|
+
"""解析可选的单个游戏参数,并返回面向用户的错误。"""
|
|
102
|
+
|
|
103
|
+
parts = text.split()
|
|
104
|
+
if not parts:
|
|
105
|
+
return None, None
|
|
106
|
+
if len(parts) > 1:
|
|
107
|
+
return None, QUERY_USAGE
|
|
108
|
+
game = resolve_game(parts[0])
|
|
109
|
+
if game is None:
|
|
110
|
+
return None, f"不支持游戏“{parts[0]}”。\n支持:星布谷地、原神、星铁、绝区零"
|
|
111
|
+
return game, None
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def format_bindings(bindings: dict[str, str], game: Optional[Game] = None) -> str:
|
|
115
|
+
if game is not None:
|
|
116
|
+
uid = bindings.get(game.key)
|
|
117
|
+
return f"{game.name}:{uid}" if uid else f"尚未绑定{game.name} UID。"
|
|
118
|
+
|
|
119
|
+
lines = [f"{item.name}:{bindings[item.key]}" for item in GAMES if item.key in bindings]
|
|
120
|
+
return "\n".join(lines) if lines else "尚未绑定任何游戏 UID。"
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def format_group_uid_list(
|
|
124
|
+
game: Game,
|
|
125
|
+
members: list[dict],
|
|
126
|
+
bindings: dict[str, str],
|
|
127
|
+
*,
|
|
128
|
+
limit: int,
|
|
129
|
+
) -> str:
|
|
130
|
+
lines: list[str] = []
|
|
131
|
+
for member in members:
|
|
132
|
+
user_id = str(member.get("user_id", ""))
|
|
133
|
+
uid = bindings.get(user_id)
|
|
134
|
+
if uid is None:
|
|
135
|
+
continue
|
|
136
|
+
group_card = str(member.get("card") or member.get("nickname") or user_id)
|
|
137
|
+
lines.append(f"{group_card}:{uid}")
|
|
138
|
+
|
|
139
|
+
if not lines:
|
|
140
|
+
return f"本群还没有群友绑定{game.name} UID。"
|
|
141
|
+
shown = lines[:limit]
|
|
142
|
+
header = f"本群{game.name} UID 列表({len(lines)}人):"
|
|
143
|
+
if len(lines) > limit:
|
|
144
|
+
shown.append(f"……另有 {len(lines) - limit} 人未显示,请提高 game_uid_list_limit 配置。")
|
|
145
|
+
return "\n".join([header, *shown])
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
@uid_bind.handle()
|
|
149
|
+
async def handle_bind(event: MessageEvent, args: Message = CommandArg()) -> None:
|
|
150
|
+
parts = args.extract_plain_text().strip().split()
|
|
151
|
+
if len(parts) != 2:
|
|
152
|
+
await uid_bind.finish(BIND_USAGE)
|
|
153
|
+
|
|
154
|
+
game = resolve_game(parts[0])
|
|
155
|
+
if game is None:
|
|
156
|
+
await uid_bind.finish(f"不支持游戏“{parts[0]}”。\n支持:星布谷地、原神、星铁、绝区零")
|
|
157
|
+
|
|
158
|
+
uid = validate_uid(parts[1])
|
|
159
|
+
if uid is None:
|
|
160
|
+
await uid_bind.finish("UID 必须是 5~20 位半角数字。")
|
|
161
|
+
|
|
162
|
+
replaced = await get_repository().set_uid(event.get_user_id(), game.key, uid)
|
|
163
|
+
action = "更新" if replaced else "绑定"
|
|
164
|
+
await uid_bind.finish(f"已{action}{game.name} UID:{uid}")
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
@uid_query.handle()
|
|
168
|
+
async def handle_query(event: MessageEvent, args: Message = CommandArg()) -> None:
|
|
169
|
+
game, error = parse_game_argument(args.extract_plain_text().strip())
|
|
170
|
+
if error:
|
|
171
|
+
await uid_query.finish(error)
|
|
172
|
+
|
|
173
|
+
current_user = event.get_user_id()
|
|
174
|
+
target_user = get_mentioned_user(args) or current_user
|
|
175
|
+
bindings = await get_repository().get_all(target_user)
|
|
176
|
+
result = format_bindings(bindings, game)
|
|
177
|
+
|
|
178
|
+
if target_user == current_user:
|
|
179
|
+
await uid_query.finish(f"你的 UID:\n{result}")
|
|
180
|
+
|
|
181
|
+
message = Message([MessageSegment.at(target_user), MessageSegment.text(f" 的 UID:\n{result}")])
|
|
182
|
+
await uid_query.finish(message)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@uid_delete.handle()
|
|
186
|
+
async def handle_delete(event: MessageEvent, args: Message = CommandArg()) -> None:
|
|
187
|
+
parts = args.extract_plain_text().strip().split()
|
|
188
|
+
if len(parts) != 1:
|
|
189
|
+
await uid_delete.finish(DELETE_USAGE)
|
|
190
|
+
|
|
191
|
+
game = resolve_game(parts[0])
|
|
192
|
+
if game is None:
|
|
193
|
+
await uid_delete.finish(f"不支持游戏“{parts[0]}”。\n支持:星布谷地、原神、星铁、绝区零")
|
|
194
|
+
|
|
195
|
+
deleted = await get_repository().delete_uid(event.get_user_id(), game.key)
|
|
196
|
+
if not deleted:
|
|
197
|
+
await uid_delete.finish(f"你还没有绑定{game.name} UID。")
|
|
198
|
+
await uid_delete.finish(f"已删除{game.name} UID。")
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@group_uid_list.handle()
|
|
202
|
+
async def handle_group_uid_list(bot: Bot, event: MessageEvent, args: Message = CommandArg()) -> None:
|
|
203
|
+
if not isinstance(event, GroupMessageEvent):
|
|
204
|
+
await group_uid_list.finish("该命令只能在群聊中使用。")
|
|
205
|
+
|
|
206
|
+
parts = args.extract_plain_text().strip().split()
|
|
207
|
+
if len(parts) != 1:
|
|
208
|
+
await group_uid_list.finish(GROUP_LIST_USAGE)
|
|
209
|
+
game = resolve_game(parts[0])
|
|
210
|
+
if game is None:
|
|
211
|
+
await group_uid_list.finish(f"不支持游戏“{parts[0]}”。\n支持:星布谷地、原神、星铁、绝区零")
|
|
212
|
+
|
|
213
|
+
try:
|
|
214
|
+
members = await bot.get_group_member_list(group_id=event.group_id)
|
|
215
|
+
except Exception as error:
|
|
216
|
+
logger.warning(f"[game_uid] 获取群 {event.group_id} 成员列表失败:{error}")
|
|
217
|
+
await group_uid_list.finish("暂时无法获取本群成员列表,请稍后再试。")
|
|
218
|
+
|
|
219
|
+
user_ids = [str(member.get("user_id", "")) for member in members]
|
|
220
|
+
bindings = await get_repository().get_uids_for_users(user_ids, game.key)
|
|
221
|
+
limit = max(1, get_plugin_config(Config).game_uid_list_limit)
|
|
222
|
+
await group_uid_list.finish(format_group_uid_list(game, members, bindings, limit=limit))
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
@reminder_set.handle()
|
|
226
|
+
async def handle_reminder_set(bot: Bot, event: MessageEvent, args: Message = CommandArg()) -> None:
|
|
227
|
+
if not isinstance(event, GroupMessageEvent):
|
|
228
|
+
await reminder_set.finish("该命令只能在群聊中使用。")
|
|
229
|
+
|
|
230
|
+
parts = args.extract_plain_text().strip().split()
|
|
231
|
+
if len(parts) != 2:
|
|
232
|
+
await reminder_set.finish(REMINDER_USAGE)
|
|
233
|
+
interval_minutes = parse_duration(parts[0])
|
|
234
|
+
game = resolve_game(parts[1])
|
|
235
|
+
if interval_minutes is None:
|
|
236
|
+
await reminder_set.finish("时间格式不正确或超出范围。\n" + REMINDER_USAGE)
|
|
237
|
+
if game is None:
|
|
238
|
+
await reminder_set.finish(f"不支持游戏“{parts[1]}”。\n支持:星布谷地、原神、星铁、绝区零")
|
|
239
|
+
|
|
240
|
+
await get_repository().set_reminder(
|
|
241
|
+
str(event.group_id),
|
|
242
|
+
bot.self_id,
|
|
243
|
+
game.key,
|
|
244
|
+
interval_minutes,
|
|
245
|
+
)
|
|
246
|
+
await reminder_set.finish(
|
|
247
|
+
f"已设置{game.name} UID 定时提醒,每 {format_duration(interval_minutes)}发送一次。\n"
|
|
248
|
+
f"提醒内容:\n{build_reminder_message(game.key)}"
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
@reminder_close.handle()
|
|
253
|
+
async def handle_reminder_close(event: MessageEvent) -> None:
|
|
254
|
+
if not isinstance(event, GroupMessageEvent):
|
|
255
|
+
await reminder_close.finish("该命令只能在群聊中使用。")
|
|
256
|
+
deleted = await get_repository().delete_reminder(str(event.group_id))
|
|
257
|
+
if not deleted:
|
|
258
|
+
await reminder_close.finish("本群尚未开启 UID 定时提醒。")
|
|
259
|
+
await reminder_close.finish("已关闭本群 UID 定时提醒。")
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
@reminder_status.handle()
|
|
263
|
+
async def handle_reminder_status(event: MessageEvent) -> None:
|
|
264
|
+
if not isinstance(event, GroupMessageEvent):
|
|
265
|
+
await reminder_status.finish("该命令只能在群聊中使用。")
|
|
266
|
+
reminder = await get_repository().get_reminder(str(event.group_id))
|
|
267
|
+
if reminder is None:
|
|
268
|
+
await reminder_status.finish("本群尚未开启 UID 定时提醒。")
|
|
269
|
+
game = GAME_BY_KEY[reminder.game]
|
|
270
|
+
await reminder_status.finish(
|
|
271
|
+
f"本群 UID 提醒:已开启\n游戏:{game.name}\n间隔:{format_duration(reminder.interval_minutes)}"
|
|
272
|
+
)
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@uid_help.handle()
|
|
276
|
+
async def handle_help() -> None:
|
|
277
|
+
await uid_help.finish(
|
|
278
|
+
"游戏 UID 记录\n"
|
|
279
|
+
"/绑定UID <游戏> <UID> — 绑定或更新自己的 UID\n"
|
|
280
|
+
"/游戏UID [游戏] [@群友] — 查询 UID\n"
|
|
281
|
+
"/删除UID <游戏> — 删除自己的绑定\n"
|
|
282
|
+
"/群UID <游戏> — 按“群名片:UID”查看本群列表\n"
|
|
283
|
+
"/设置UID提醒 <间隔> <游戏> — SUPERUSER 开启定时提醒\n"
|
|
284
|
+
"/关闭UID提醒 — SUPERUSER 关闭定时提醒\n"
|
|
285
|
+
"/UID提醒状态 — 查看本群提醒设置\n"
|
|
286
|
+
"/UID帮助 — 查看帮助\n"
|
|
287
|
+
"支持游戏:星布谷地、原神、星铁、绝区零"
|
|
288
|
+
)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass(frozen=True)
|
|
8
|
+
class Game:
|
|
9
|
+
key: str
|
|
10
|
+
name: str
|
|
11
|
+
aliases: tuple[str, ...]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
GAMES: tuple[Game, ...] = (
|
|
15
|
+
Game("starry", "星布谷地", ("星布谷地", "星布谷底", "星布", "谷地", "谷底")),
|
|
16
|
+
Game("genshin", "原神", ("原神", "ys")),
|
|
17
|
+
Game(
|
|
18
|
+
"starrail",
|
|
19
|
+
"崩坏:星穹铁道",
|
|
20
|
+
("崩坏:星穹铁道", "崩坏星穹铁道", "星穹铁道", "星铁", "铁道", "崩铁", "hsr"),
|
|
21
|
+
),
|
|
22
|
+
Game("zzz", "绝区零", ("绝区零", "绝区", "zzz")),
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
GAME_BY_KEY = {game.key: game for game in GAMES}
|
|
26
|
+
GAME_BY_ALIAS = {alias.casefold(): game for game in GAMES for alias in game.aliases}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def resolve_game(value: str) -> Optional[Game]:
|
|
30
|
+
"""将游戏名或别名规范化为受支持的游戏。"""
|
|
31
|
+
|
|
32
|
+
return GAME_BY_ALIAS.get(value.strip().casefold())
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def validate_uid(value: str) -> Optional[str]:
|
|
36
|
+
"""验证 UID,成功时返回清理后的 UID。"""
|
|
37
|
+
|
|
38
|
+
uid = value.strip()
|
|
39
|
+
if not uid.isascii() or not uid.isdecimal() or not 5 <= len(uid) <= 20:
|
|
40
|
+
return None
|
|
41
|
+
return uid
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from nonebot import get_bots, logger
|
|
7
|
+
|
|
8
|
+
from .games import GAME_BY_KEY
|
|
9
|
+
|
|
10
|
+
DURATION_PATTERN = re.compile(r"^(\d+)(分钟|分|小时|时|天)$")
|
|
11
|
+
MIN_INTERVAL_MINUTES = 10
|
|
12
|
+
MAX_INTERVAL_MINUTES = 30 * 24 * 60
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def parse_duration(value: str) -> Optional[int]:
|
|
16
|
+
"""解析 10分钟、6小时、1天一类的时间间隔。"""
|
|
17
|
+
|
|
18
|
+
match = DURATION_PATTERN.fullmatch(value.strip())
|
|
19
|
+
if not match:
|
|
20
|
+
return None
|
|
21
|
+
amount = int(match.group(1))
|
|
22
|
+
unit = match.group(2)
|
|
23
|
+
multiplier = 1 if unit in {"分钟", "分"} else 60 if unit in {"小时", "时"} else 24 * 60
|
|
24
|
+
minutes = amount * multiplier
|
|
25
|
+
if not MIN_INTERVAL_MINUTES <= minutes <= MAX_INTERVAL_MINUTES:
|
|
26
|
+
return None
|
|
27
|
+
return minutes
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def format_duration(minutes: int) -> str:
|
|
31
|
+
if minutes % (24 * 60) == 0:
|
|
32
|
+
return f"{minutes // (24 * 60)}天"
|
|
33
|
+
if minutes % 60 == 0:
|
|
34
|
+
return f"{minutes // 60}小时"
|
|
35
|
+
return f"{minutes}分钟"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def build_reminder_message(game_key: str) -> str:
|
|
39
|
+
game = GAME_BY_KEY[game_key]
|
|
40
|
+
return (
|
|
41
|
+
f"想查看本群群友的{game.name} UID 并添加游戏好友?\n"
|
|
42
|
+
f"发送 /群UID {game.name},即可查看群友主动绑定的 UID(群名片:UID)。"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
async def dispatch_due_reminders(*, now: Optional[float] = None) -> int:
|
|
47
|
+
"""发送已到期提醒,返回成功发送数量。"""
|
|
48
|
+
|
|
49
|
+
from .commands import get_repository
|
|
50
|
+
|
|
51
|
+
repository = get_repository()
|
|
52
|
+
bots = get_bots()
|
|
53
|
+
sent = 0
|
|
54
|
+
for reminder in await repository.get_due_reminders(now=now):
|
|
55
|
+
bot = bots.get(reminder.bot_id)
|
|
56
|
+
if bot is None:
|
|
57
|
+
logger.warning(f"[game_uid] 提醒群 {reminder.group_id} 时机器人 {reminder.bot_id} 不在线")
|
|
58
|
+
continue
|
|
59
|
+
try:
|
|
60
|
+
await bot.call_api(
|
|
61
|
+
"send_group_msg",
|
|
62
|
+
group_id=int(reminder.group_id),
|
|
63
|
+
message=build_reminder_message(reminder.game),
|
|
64
|
+
)
|
|
65
|
+
except Exception as error:
|
|
66
|
+
logger.warning(f"[game_uid] 群 {reminder.group_id} UID 提醒发送失败:{error}")
|
|
67
|
+
continue
|
|
68
|
+
await repository.mark_reminder_sent(reminder.group_id, now=now)
|
|
69
|
+
sent += 1
|
|
70
|
+
return sent
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import time
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Optional, Union
|
|
8
|
+
|
|
9
|
+
import aiosqlite
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class GroupReminder:
|
|
14
|
+
group_id: str
|
|
15
|
+
bot_id: str
|
|
16
|
+
game: str
|
|
17
|
+
interval_minutes: int
|
|
18
|
+
next_run_at: float
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class UIDRepository:
|
|
22
|
+
"""基于 SQLite 的 UID 存储。"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, db_path: Union[str, Path]) -> None:
|
|
25
|
+
self.db_path = Path(db_path)
|
|
26
|
+
self._initialized = False
|
|
27
|
+
self._init_lock = asyncio.Lock()
|
|
28
|
+
|
|
29
|
+
async def initialize(self) -> None:
|
|
30
|
+
if self._initialized:
|
|
31
|
+
return
|
|
32
|
+
|
|
33
|
+
async with self._init_lock:
|
|
34
|
+
if self._initialized:
|
|
35
|
+
return
|
|
36
|
+
await asyncio.to_thread(self.db_path.parent.mkdir, parents=True, exist_ok=True)
|
|
37
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
38
|
+
await db.execute(
|
|
39
|
+
"""
|
|
40
|
+
CREATE TABLE IF NOT EXISTS uid_bindings (
|
|
41
|
+
platform_user_id TEXT NOT NULL,
|
|
42
|
+
game TEXT NOT NULL,
|
|
43
|
+
uid TEXT NOT NULL,
|
|
44
|
+
updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
45
|
+
PRIMARY KEY (platform_user_id, game)
|
|
46
|
+
)
|
|
47
|
+
"""
|
|
48
|
+
)
|
|
49
|
+
await db.execute(
|
|
50
|
+
"""
|
|
51
|
+
CREATE TABLE IF NOT EXISTS group_reminders (
|
|
52
|
+
group_id TEXT PRIMARY KEY,
|
|
53
|
+
bot_id TEXT NOT NULL,
|
|
54
|
+
game TEXT NOT NULL,
|
|
55
|
+
interval_minutes INTEGER NOT NULL,
|
|
56
|
+
next_run_at REAL NOT NULL
|
|
57
|
+
)
|
|
58
|
+
"""
|
|
59
|
+
)
|
|
60
|
+
await db.commit()
|
|
61
|
+
self._initialized = True
|
|
62
|
+
|
|
63
|
+
async def set_uid(self, platform_user_id: str, game: str, uid: str) -> bool:
|
|
64
|
+
"""新增或更新 UID;返回 True 表示替换了旧绑定。"""
|
|
65
|
+
|
|
66
|
+
await self.initialize()
|
|
67
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
68
|
+
cursor = await db.execute(
|
|
69
|
+
"SELECT 1 FROM uid_bindings WHERE platform_user_id = ? AND game = ?",
|
|
70
|
+
(platform_user_id, game),
|
|
71
|
+
)
|
|
72
|
+
existed = await cursor.fetchone() is not None
|
|
73
|
+
await cursor.close()
|
|
74
|
+
await db.execute(
|
|
75
|
+
"""
|
|
76
|
+
INSERT INTO uid_bindings (platform_user_id, game, uid)
|
|
77
|
+
VALUES (?, ?, ?)
|
|
78
|
+
ON CONFLICT(platform_user_id, game) DO UPDATE SET
|
|
79
|
+
uid = excluded.uid,
|
|
80
|
+
updated_at = CURRENT_TIMESTAMP
|
|
81
|
+
""",
|
|
82
|
+
(platform_user_id, game, uid),
|
|
83
|
+
)
|
|
84
|
+
await db.commit()
|
|
85
|
+
return existed
|
|
86
|
+
|
|
87
|
+
async def get_uid(self, platform_user_id: str, game: str) -> Optional[str]:
|
|
88
|
+
await self.initialize()
|
|
89
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
90
|
+
cursor = await db.execute(
|
|
91
|
+
"SELECT uid FROM uid_bindings WHERE platform_user_id = ? AND game = ?",
|
|
92
|
+
(platform_user_id, game),
|
|
93
|
+
)
|
|
94
|
+
row = await cursor.fetchone()
|
|
95
|
+
await cursor.close()
|
|
96
|
+
return str(row[0]) if row else None
|
|
97
|
+
|
|
98
|
+
async def get_all(self, platform_user_id: str) -> dict[str, str]:
|
|
99
|
+
await self.initialize()
|
|
100
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
101
|
+
cursor = await db.execute(
|
|
102
|
+
"SELECT game, uid FROM uid_bindings WHERE platform_user_id = ?",
|
|
103
|
+
(platform_user_id,),
|
|
104
|
+
)
|
|
105
|
+
rows = await cursor.fetchall()
|
|
106
|
+
await cursor.close()
|
|
107
|
+
return {str(game): str(uid) for game, uid in rows}
|
|
108
|
+
|
|
109
|
+
async def get_uids_for_users(self, platform_user_ids: list[str], game: str) -> dict[str, str]:
|
|
110
|
+
"""批量获取一组用户的指定游戏 UID。"""
|
|
111
|
+
|
|
112
|
+
await self.initialize()
|
|
113
|
+
if not platform_user_ids:
|
|
114
|
+
return {}
|
|
115
|
+
|
|
116
|
+
result: dict[str, str] = {}
|
|
117
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
118
|
+
for start in range(0, len(platform_user_ids), 900):
|
|
119
|
+
chunk = platform_user_ids[start : start + 900]
|
|
120
|
+
placeholders = ",".join("?" for _ in chunk)
|
|
121
|
+
cursor = await db.execute(
|
|
122
|
+
f"""
|
|
123
|
+
SELECT platform_user_id, uid
|
|
124
|
+
FROM uid_bindings
|
|
125
|
+
WHERE game = ? AND platform_user_id IN ({placeholders})
|
|
126
|
+
""", # noqa: S608 - placeholders are generated locally, values remain parameterized
|
|
127
|
+
(game, *chunk),
|
|
128
|
+
)
|
|
129
|
+
rows = await cursor.fetchall()
|
|
130
|
+
await cursor.close()
|
|
131
|
+
result.update({str(user_id): str(uid) for user_id, uid in rows})
|
|
132
|
+
return result
|
|
133
|
+
|
|
134
|
+
async def delete_uid(self, platform_user_id: str, game: str) -> bool:
|
|
135
|
+
await self.initialize()
|
|
136
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
137
|
+
cursor = await db.execute(
|
|
138
|
+
"DELETE FROM uid_bindings WHERE platform_user_id = ? AND game = ?",
|
|
139
|
+
(platform_user_id, game),
|
|
140
|
+
)
|
|
141
|
+
deleted = cursor.rowcount > 0
|
|
142
|
+
await cursor.close()
|
|
143
|
+
await db.commit()
|
|
144
|
+
return deleted
|
|
145
|
+
|
|
146
|
+
async def set_reminder(
|
|
147
|
+
self,
|
|
148
|
+
group_id: str,
|
|
149
|
+
bot_id: str,
|
|
150
|
+
game: str,
|
|
151
|
+
interval_minutes: int,
|
|
152
|
+
*,
|
|
153
|
+
now: Optional[float] = None,
|
|
154
|
+
) -> None:
|
|
155
|
+
await self.initialize()
|
|
156
|
+
current_time = time.time() if now is None else now
|
|
157
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
158
|
+
await db.execute(
|
|
159
|
+
"""
|
|
160
|
+
INSERT INTO group_reminders (group_id, bot_id, game, interval_minutes, next_run_at)
|
|
161
|
+
VALUES (?, ?, ?, ?, ?)
|
|
162
|
+
ON CONFLICT(group_id) DO UPDATE SET
|
|
163
|
+
bot_id = excluded.bot_id,
|
|
164
|
+
game = excluded.game,
|
|
165
|
+
interval_minutes = excluded.interval_minutes,
|
|
166
|
+
next_run_at = excluded.next_run_at
|
|
167
|
+
""",
|
|
168
|
+
(group_id, bot_id, game, interval_minutes, current_time + interval_minutes * 60),
|
|
169
|
+
)
|
|
170
|
+
await db.commit()
|
|
171
|
+
|
|
172
|
+
async def get_reminder(self, group_id: str) -> Optional[GroupReminder]:
|
|
173
|
+
await self.initialize()
|
|
174
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
175
|
+
cursor = await db.execute(
|
|
176
|
+
"""
|
|
177
|
+
SELECT group_id, bot_id, game, interval_minutes, next_run_at
|
|
178
|
+
FROM group_reminders
|
|
179
|
+
WHERE group_id = ?
|
|
180
|
+
""",
|
|
181
|
+
(group_id,),
|
|
182
|
+
)
|
|
183
|
+
row = await cursor.fetchone()
|
|
184
|
+
await cursor.close()
|
|
185
|
+
return GroupReminder(*row) if row else None
|
|
186
|
+
|
|
187
|
+
async def get_due_reminders(self, *, now: Optional[float] = None) -> list[GroupReminder]:
|
|
188
|
+
await self.initialize()
|
|
189
|
+
current_time = time.time() if now is None else now
|
|
190
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
191
|
+
cursor = await db.execute(
|
|
192
|
+
"""
|
|
193
|
+
SELECT group_id, bot_id, game, interval_minutes, next_run_at
|
|
194
|
+
FROM group_reminders
|
|
195
|
+
WHERE next_run_at <= ?
|
|
196
|
+
ORDER BY next_run_at
|
|
197
|
+
""",
|
|
198
|
+
(current_time,),
|
|
199
|
+
)
|
|
200
|
+
rows = await cursor.fetchall()
|
|
201
|
+
await cursor.close()
|
|
202
|
+
return [GroupReminder(*row) for row in rows]
|
|
203
|
+
|
|
204
|
+
async def mark_reminder_sent(self, group_id: str, *, now: Optional[float] = None) -> None:
|
|
205
|
+
await self.initialize()
|
|
206
|
+
current_time = time.time() if now is None else now
|
|
207
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
208
|
+
await db.execute(
|
|
209
|
+
"""
|
|
210
|
+
UPDATE group_reminders
|
|
211
|
+
SET next_run_at = ? + interval_minutes * 60
|
|
212
|
+
WHERE group_id = ?
|
|
213
|
+
""",
|
|
214
|
+
(current_time, group_id),
|
|
215
|
+
)
|
|
216
|
+
await db.commit()
|
|
217
|
+
|
|
218
|
+
async def delete_reminder(self, group_id: str) -> bool:
|
|
219
|
+
await self.initialize()
|
|
220
|
+
async with aiosqlite.connect(self.db_path) as db:
|
|
221
|
+
cursor = await db.execute("DELETE FROM group_reminders WHERE group_id = ?", (group_id,))
|
|
222
|
+
deleted = cursor.rowcount > 0
|
|
223
|
+
await cursor.close()
|
|
224
|
+
await db.commit()
|
|
225
|
+
return deleted
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "nonebot-plugin-game-uid"
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "记录、汇总和定时播报群友的星布谷地、原神、崩坏:星穹铁道与绝区零 UID"
|
|
5
|
+
authors = [{ name = "TonyLiangP2010405" }]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
requires-python = ">=3.9,<4.0"
|
|
9
|
+
keywords = ["nonebot", "nonebot2", "uid", "mihoyo", "onebot"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Development Status :: 3 - Alpha",
|
|
12
|
+
"Operating System :: OS Independent",
|
|
13
|
+
"Programming Language :: Python :: 3",
|
|
14
|
+
"Programming Language :: Python :: 3.9",
|
|
15
|
+
"Programming Language :: Python :: 3.10",
|
|
16
|
+
"Programming Language :: Python :: 3.11",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Topic :: Communications :: Chat",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"nonebot2>=2.2.0",
|
|
24
|
+
"nonebot-adapter-onebot>=2.4.0",
|
|
25
|
+
"nonebot-plugin-apscheduler>=0.5.0",
|
|
26
|
+
"nonebot-plugin-localstore>=0.7.0",
|
|
27
|
+
"aiosqlite>=0.19.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/TonyLiangP2010405/nonebot-plugin-game-uid"
|
|
32
|
+
Repository = "https://github.com/TonyLiangP2010405/nonebot-plugin-game-uid"
|
|
33
|
+
|
|
34
|
+
[tool.poetry]
|
|
35
|
+
packages = [{ include = "nonebot_plugin_game_uid" }]
|
|
36
|
+
|
|
37
|
+
[tool.poetry.group.dev.dependencies]
|
|
38
|
+
pytest = ">=7.0"
|
|
39
|
+
pytest-asyncio = ">=0.21"
|
|
40
|
+
nonebug = ">=0.3"
|
|
41
|
+
ruff = ">=0.1"
|
|
42
|
+
|
|
43
|
+
[build-system]
|
|
44
|
+
requires = ["poetry-core>=2.0"]
|
|
45
|
+
build-backend = "poetry.core.masonry.api"
|
|
46
|
+
|
|
47
|
+
[tool.nonebot]
|
|
48
|
+
plugins = ["nonebot_plugin_game_uid"]
|
|
49
|
+
|
|
50
|
+
[tool.ruff]
|
|
51
|
+
line-length = 120
|
|
52
|
+
target-version = "py39"
|
|
53
|
+
|
|
54
|
+
[tool.ruff.lint]
|
|
55
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
56
|
+
ignore = ["B008", "UP007", "UP045"]
|
|
57
|
+
|
|
58
|
+
[tool.pytest.ini_options]
|
|
59
|
+
testpaths = ["tests"]
|
|
60
|
+
asyncio_mode = "auto"
|