nonebot-plugin-game-uid 0.3.0__tar.gz → 0.4.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.3.0 → nonebot_plugin_game_uid-0.4.0}/PKG-INFO +30 -1
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/README.md +29 -0
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/commands.py +55 -13
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/reminders.py +62 -7
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/repository.py +61 -16
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/pyproject.toml +1 -1
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/LICENSE +0 -0
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/__init__.py +0 -0
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/config.py +0 -0
- {nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/games.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nonebot-plugin-game-uid
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: 记录、汇总和定时播报群友的星布谷地、原神、崩坏:星穹铁道与绝区零 UID
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -38,6 +38,7 @@ Description-Content-Type: text/markdown
|
|
|
38
38
|
- 在群聊中通过 `@群友` 查询对方主动绑定的 UID
|
|
39
39
|
- 按“群名片:UID”列出本群已经绑定指定游戏的群友
|
|
40
40
|
- SUPERUSER 可设置定时播报,周期性提示群友使用 UID 列表命令
|
|
41
|
+
- 播报发送失败时按递增间隔自动重试,避免每分钟刷屏
|
|
41
42
|
- 删除自己的指定游戏 UID
|
|
42
43
|
- 一键清空自己的全部 UID,或只清空指定游戏 UID
|
|
43
44
|
- 使用 SQLite 异步持久化,无需外部数据库或游戏 API
|
|
@@ -99,6 +100,25 @@ UID 按 QQ 用户全局保存,不按群分别存储。群友只能查询用户
|
|
|
99
100
|
| `/UID提醒状态` | 所有人 | 群聊 | 查看本群提醒状态 |
|
|
100
101
|
| `/UID帮助` | 所有人 | 群聊/私聊 | 查看命令帮助 |
|
|
101
102
|
|
|
103
|
+
### 命令别名
|
|
104
|
+
|
|
105
|
+
命令支持下列别名,效果与完整命令一致(别名区分大小写,`UID` 全大写和 `uid` 全小写两种写法均已注册):
|
|
106
|
+
|
|
107
|
+
| 命令 | 别名 |
|
|
108
|
+
|---|---|
|
|
109
|
+
| `/绑定UID` | `/绑定uid`、`/绑定游戏UID`、`/绑定游戏uid` |
|
|
110
|
+
| `/游戏UID` | `/游戏uid`、`/UID`、`/uid`、`/我的UID`、`/我的uid` |
|
|
111
|
+
| `/删除UID` | `/删除uid`、`/解绑UID`、`/解绑uid` |
|
|
112
|
+
| `/清空UID` | `/清空uid`、`/清除UID`、`/清除uid`、`/一键清除UID`、`/一键清除uid`、`/一件清除UID`、`/一件清除uid` |
|
|
113
|
+
| `/绑定帮助` | `/绑定UID帮助`、`/绑定uid帮助` |
|
|
114
|
+
| `/群UID` | `/群uid`、`/群友UID`、`/群友uid`、`/UID列表`、`/uid列表` |
|
|
115
|
+
| `/设置UID提醒` | `/设置uid提醒` |
|
|
116
|
+
| `/关闭UID提醒` | `/关闭uid提醒` |
|
|
117
|
+
| `/UID提醒状态` | `/uid提醒状态` |
|
|
118
|
+
| `/UID帮助` | `/uid帮助`、`/游戏UID帮助`、`/游戏uid帮助` |
|
|
119
|
+
|
|
120
|
+
命令前缀默认为 `/`,可在 NoneBot 的 `COMMAND_START` 配置中修改;帮助和播报文案中的 `/` 仅作示例。
|
|
121
|
+
|
|
102
122
|
支持的游戏名及常用别名:
|
|
103
123
|
|
|
104
124
|
- 星布谷地:`星布谷地`、`星布谷底`、`星布`、`谷地`、`谷底`
|
|
@@ -109,6 +129,15 @@ UID 按 QQ 用户全局保存,不按群分别存储。群友只能查询用户
|
|
|
109
129
|
UID 必须为 5~20 位半角数字。
|
|
110
130
|
|
|
111
131
|
提醒间隔支持 `分钟`、`小时`、`天`,范围为 10 分钟到 30 天。每个群保存一条提醒设置,再次设置会覆盖旧设置。机器人重启后设置仍然有效。
|
|
132
|
+
发送失败(机器人离线、被移出群等)时,插件会按 2、4、8…… 分钟的递增间隔重试,单次等待不超过提醒间隔;连续失败 5 次后改为按正常间隔重试,`/UID提醒状态` 会显示最近的失败次数。
|
|
133
|
+
同一群的 `/群UID` 每 10 秒最多触发一次;列表默认最多显示 100 人,超过人数上限或消息过长时会折叠剩余人数。
|
|
134
|
+
|
|
135
|
+
## 数据存储
|
|
136
|
+
|
|
137
|
+
- 默认数据库位于 `nonebot-plugin-localstore` 的插件数据目录,文件名为 `uid.db`:Linux 为 `~/.local/share/nonebot2/nonebot_plugin_game_uid/uid.db`,macOS 为 `~/Library/Application Support/nonebot2/nonebot_plugin_game_uid/uid.db`,Windows 为 `%LOCALAPPDATA%\nonebot2\nonebot_plugin_game_uid\uid.db`。
|
|
138
|
+
- 配置 `GAME_UID_DB_PATH` 可改用自定义路径,相对路径按机器人启动目录解析。
|
|
139
|
+
- 数据库启用 WAL 模式;从 0.3.x 升级到 0.4.0 后首次启动会自动补齐 `retry_count` 列,已有绑定和提醒不会丢失。
|
|
140
|
+
- 备份时请先停止机器人再复制 `uid.db`,或连同同目录的 `uid.db-wal`、`uid.db-shm` 一起复制。
|
|
112
141
|
|
|
113
142
|
## 示例
|
|
114
143
|
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
- 在群聊中通过 `@群友` 查询对方主动绑定的 UID
|
|
11
11
|
- 按“群名片:UID”列出本群已经绑定指定游戏的群友
|
|
12
12
|
- SUPERUSER 可设置定时播报,周期性提示群友使用 UID 列表命令
|
|
13
|
+
- 播报发送失败时按递增间隔自动重试,避免每分钟刷屏
|
|
13
14
|
- 删除自己的指定游戏 UID
|
|
14
15
|
- 一键清空自己的全部 UID,或只清空指定游戏 UID
|
|
15
16
|
- 使用 SQLite 异步持久化,无需外部数据库或游戏 API
|
|
@@ -71,6 +72,25 @@ UID 按 QQ 用户全局保存,不按群分别存储。群友只能查询用户
|
|
|
71
72
|
| `/UID提醒状态` | 所有人 | 群聊 | 查看本群提醒状态 |
|
|
72
73
|
| `/UID帮助` | 所有人 | 群聊/私聊 | 查看命令帮助 |
|
|
73
74
|
|
|
75
|
+
### 命令别名
|
|
76
|
+
|
|
77
|
+
命令支持下列别名,效果与完整命令一致(别名区分大小写,`UID` 全大写和 `uid` 全小写两种写法均已注册):
|
|
78
|
+
|
|
79
|
+
| 命令 | 别名 |
|
|
80
|
+
|---|---|
|
|
81
|
+
| `/绑定UID` | `/绑定uid`、`/绑定游戏UID`、`/绑定游戏uid` |
|
|
82
|
+
| `/游戏UID` | `/游戏uid`、`/UID`、`/uid`、`/我的UID`、`/我的uid` |
|
|
83
|
+
| `/删除UID` | `/删除uid`、`/解绑UID`、`/解绑uid` |
|
|
84
|
+
| `/清空UID` | `/清空uid`、`/清除UID`、`/清除uid`、`/一键清除UID`、`/一键清除uid`、`/一件清除UID`、`/一件清除uid` |
|
|
85
|
+
| `/绑定帮助` | `/绑定UID帮助`、`/绑定uid帮助` |
|
|
86
|
+
| `/群UID` | `/群uid`、`/群友UID`、`/群友uid`、`/UID列表`、`/uid列表` |
|
|
87
|
+
| `/设置UID提醒` | `/设置uid提醒` |
|
|
88
|
+
| `/关闭UID提醒` | `/关闭uid提醒` |
|
|
89
|
+
| `/UID提醒状态` | `/uid提醒状态` |
|
|
90
|
+
| `/UID帮助` | `/uid帮助`、`/游戏UID帮助`、`/游戏uid帮助` |
|
|
91
|
+
|
|
92
|
+
命令前缀默认为 `/`,可在 NoneBot 的 `COMMAND_START` 配置中修改;帮助和播报文案中的 `/` 仅作示例。
|
|
93
|
+
|
|
74
94
|
支持的游戏名及常用别名:
|
|
75
95
|
|
|
76
96
|
- 星布谷地:`星布谷地`、`星布谷底`、`星布`、`谷地`、`谷底`
|
|
@@ -81,6 +101,15 @@ UID 按 QQ 用户全局保存,不按群分别存储。群友只能查询用户
|
|
|
81
101
|
UID 必须为 5~20 位半角数字。
|
|
82
102
|
|
|
83
103
|
提醒间隔支持 `分钟`、`小时`、`天`,范围为 10 分钟到 30 天。每个群保存一条提醒设置,再次设置会覆盖旧设置。机器人重启后设置仍然有效。
|
|
104
|
+
发送失败(机器人离线、被移出群等)时,插件会按 2、4、8…… 分钟的递增间隔重试,单次等待不超过提醒间隔;连续失败 5 次后改为按正常间隔重试,`/UID提醒状态` 会显示最近的失败次数。
|
|
105
|
+
同一群的 `/群UID` 每 10 秒最多触发一次;列表默认最多显示 100 人,超过人数上限或消息过长时会折叠剩余人数。
|
|
106
|
+
|
|
107
|
+
## 数据存储
|
|
108
|
+
|
|
109
|
+
- 默认数据库位于 `nonebot-plugin-localstore` 的插件数据目录,文件名为 `uid.db`:Linux 为 `~/.local/share/nonebot2/nonebot_plugin_game_uid/uid.db`,macOS 为 `~/Library/Application Support/nonebot2/nonebot_plugin_game_uid/uid.db`,Windows 为 `%LOCALAPPDATA%\nonebot2\nonebot_plugin_game_uid\uid.db`。
|
|
110
|
+
- 配置 `GAME_UID_DB_PATH` 可改用自定义路径,相对路径按机器人启动目录解析。
|
|
111
|
+
- 数据库启用 WAL 模式;从 0.3.x 升级到 0.4.0 后首次启动会自动补齐 `retry_count` 列,已有绑定和提醒不会丢失。
|
|
112
|
+
- 备份时请先停止机器人再复制 `uid.db`,或连同同目录的 `uid.db-wal`、`uid.db-shm` 一起复制。
|
|
84
113
|
|
|
85
114
|
## 示例
|
|
86
115
|
|
{nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/commands.py
RENAMED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import math
|
|
4
|
+
import time
|
|
3
5
|
from pathlib import Path
|
|
4
6
|
from typing import Optional
|
|
5
7
|
|
|
@@ -20,6 +22,8 @@ DELETE_USAGE = "用法:/删除UID <游戏>\n例如:/删除UID 绝区零"
|
|
|
20
22
|
CLEAR_USAGE = "用法:/清空UID [游戏]\n不填游戏时清空自己的全部 UID\n例如:/清空UID 或 /清空UID 原神"
|
|
21
23
|
GROUP_LIST_USAGE = "用法:/群UID <游戏>\n例如:/群UID 星布谷地"
|
|
22
24
|
REMINDER_USAGE = "用法:/设置UID提醒 <间隔> <游戏>\n例如:/设置UID提醒 6小时 原神\n间隔范围:10分钟~30天"
|
|
25
|
+
GROUP_LIST_MAX_CHARS = 1500
|
|
26
|
+
GROUP_LIST_COOLDOWN_SECONDS = 10
|
|
23
27
|
BIND_HELP_TEXT = (
|
|
24
28
|
"UID 绑定帮助\n"
|
|
25
29
|
"绑定:/绑定UID <游戏> <UID>\n"
|
|
@@ -96,20 +100,31 @@ reminder_status = on_command(
|
|
|
96
100
|
)
|
|
97
101
|
|
|
98
102
|
_repository: Optional[UIDRepository] = None
|
|
99
|
-
|
|
103
|
+
_group_list_cooldowns: dict[str, float] = {}
|
|
100
104
|
|
|
101
105
|
|
|
102
106
|
def get_repository() -> UIDRepository:
|
|
103
|
-
global _repository
|
|
107
|
+
global _repository
|
|
104
108
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
109
|
+
if _repository is None:
|
|
110
|
+
configured_path = get_plugin_config(Config).game_uid_db_path
|
|
111
|
+
path = Path(configured_path).expanduser() if configured_path else localstore.get_plugin_data_file("uid.db")
|
|
108
112
|
_repository = UIDRepository(path)
|
|
109
|
-
_repository_path = path
|
|
110
113
|
return _repository
|
|
111
114
|
|
|
112
115
|
|
|
116
|
+
def group_list_cooldown_remaining(group_id: str) -> int:
|
|
117
|
+
"""返回本群 `/群UID` 的剩余冷却秒数,0 表示可以立即使用。"""
|
|
118
|
+
|
|
119
|
+
last_used = _group_list_cooldowns.get(group_id)
|
|
120
|
+
if last_used is None:
|
|
121
|
+
return 0
|
|
122
|
+
remaining = GROUP_LIST_COOLDOWN_SECONDS - (time.monotonic() - last_used)
|
|
123
|
+
if remaining <= 0:
|
|
124
|
+
return 0
|
|
125
|
+
return math.ceil(remaining)
|
|
126
|
+
|
|
127
|
+
|
|
113
128
|
def get_mentioned_user(message: Message) -> Optional[str]:
|
|
114
129
|
"""提取消息中的第一个 QQ @,忽略 @全体成员。"""
|
|
115
130
|
|
|
@@ -150,6 +165,7 @@ def format_group_uid_list(
|
|
|
150
165
|
bindings: dict[str, str],
|
|
151
166
|
*,
|
|
152
167
|
limit: int,
|
|
168
|
+
max_chars: int = GROUP_LIST_MAX_CHARS,
|
|
153
169
|
) -> str:
|
|
154
170
|
lines: list[str] = []
|
|
155
171
|
for member in members:
|
|
@@ -162,10 +178,24 @@ def format_group_uid_list(
|
|
|
162
178
|
|
|
163
179
|
if not lines:
|
|
164
180
|
return f"本群还没有群友绑定{game.name} UID。"
|
|
165
|
-
shown = lines[:limit]
|
|
166
181
|
header = f"本群{game.name} UID 列表({len(lines)}人):"
|
|
167
|
-
|
|
168
|
-
|
|
182
|
+
|
|
183
|
+
shown: list[str] = []
|
|
184
|
+
used_chars = len(header)
|
|
185
|
+
for line in lines[:limit]:
|
|
186
|
+
cost = len(line) + 1
|
|
187
|
+
# 至少保留一行,避免超长群名片把整条消息吞掉
|
|
188
|
+
if shown and used_chars + cost > max_chars:
|
|
189
|
+
break
|
|
190
|
+
shown.append(line)
|
|
191
|
+
used_chars += cost
|
|
192
|
+
|
|
193
|
+
hidden = len(lines) - len(shown)
|
|
194
|
+
if hidden:
|
|
195
|
+
if len(lines) > limit:
|
|
196
|
+
shown.append(f"……另有 {hidden} 人未显示,请提高 game_uid_list_limit 配置。")
|
|
197
|
+
else:
|
|
198
|
+
shown.append(f"……另有 {hidden} 人未显示(消息长度限制)。")
|
|
169
199
|
return "\n".join([header, *shown])
|
|
170
200
|
|
|
171
201
|
|
|
@@ -262,6 +292,12 @@ async def handle_group_uid_list(bot: Bot, event: MessageEvent, args: Message = C
|
|
|
262
292
|
if game is None:
|
|
263
293
|
await group_uid_list.finish(f"不支持游戏“{parts[0]}”。\n支持:星布谷地、原神、星铁、绝区零")
|
|
264
294
|
|
|
295
|
+
group_id = str(event.group_id)
|
|
296
|
+
remaining = group_list_cooldown_remaining(group_id)
|
|
297
|
+
if remaining:
|
|
298
|
+
await group_uid_list.finish(f"查询太频繁,请 {remaining} 秒后再试。")
|
|
299
|
+
_group_list_cooldowns[group_id] = time.monotonic()
|
|
300
|
+
|
|
265
301
|
try:
|
|
266
302
|
members = await bot.get_group_member_list(group_id=event.group_id)
|
|
267
303
|
except Exception as error:
|
|
@@ -318,10 +354,16 @@ async def handle_reminder_status(event: MessageEvent) -> None:
|
|
|
318
354
|
reminder = await get_repository().get_reminder(str(event.group_id))
|
|
319
355
|
if reminder is None:
|
|
320
356
|
await reminder_status.finish("本群尚未开启 UID 定时提醒。")
|
|
321
|
-
game = GAME_BY_KEY
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
357
|
+
game = GAME_BY_KEY.get(reminder.game)
|
|
358
|
+
game_name = game.name if game else reminder.game
|
|
359
|
+
lines = [
|
|
360
|
+
"本群 UID 提醒:已开启",
|
|
361
|
+
f"游戏:{game_name}",
|
|
362
|
+
f"间隔:{format_duration(reminder.interval_minutes)}",
|
|
363
|
+
]
|
|
364
|
+
if reminder.retry_count:
|
|
365
|
+
lines.append(f"最近发送失败 {reminder.retry_count} 次,将自动重试。")
|
|
366
|
+
await reminder_status.finish("\n".join(lines))
|
|
325
367
|
|
|
326
368
|
|
|
327
369
|
@uid_help.handle()
|
{nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/reminders.py
RENAMED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import re
|
|
4
|
+
import time
|
|
4
5
|
from typing import Optional
|
|
5
6
|
|
|
6
7
|
from nonebot import get_bots, logger
|
|
7
8
|
|
|
8
9
|
from .games import GAME_BY_KEY
|
|
10
|
+
from .repository import GroupReminder, UIDRepository
|
|
9
11
|
|
|
10
12
|
DURATION_PATTERN = re.compile(r"^(\d+)(分钟|分|小时|时|天)$")
|
|
11
13
|
MIN_INTERVAL_MINUTES = 10
|
|
12
14
|
MAX_INTERVAL_MINUTES = 30 * 24 * 60
|
|
15
|
+
MAX_REMINDER_RETRIES = 5
|
|
13
16
|
|
|
14
17
|
|
|
15
18
|
def parse_duration(value: str) -> Optional[int]:
|
|
@@ -36,10 +39,11 @@ def format_duration(minutes: int) -> str:
|
|
|
36
39
|
|
|
37
40
|
|
|
38
41
|
def build_reminder_message(game_key: str) -> str:
|
|
39
|
-
game = GAME_BY_KEY
|
|
42
|
+
game = GAME_BY_KEY.get(game_key)
|
|
43
|
+
game_name = game.name if game else game_key
|
|
40
44
|
return (
|
|
41
|
-
f"想查看本群群友的{
|
|
42
|
-
f"发送 /群UID {
|
|
45
|
+
f"想查看本群群友的{game_name} UID 并添加游戏好友?\n"
|
|
46
|
+
f"发送 /群UID {game_name},即可查看群友主动绑定的 UID(群名片:UID)。\n\n"
|
|
43
47
|
"全部命令:\n"
|
|
44
48
|
"/绑定UID <游戏> <UID> — 绑定或更新自己的 UID\n"
|
|
45
49
|
"/游戏UID [游戏] [@群友] — 查询自己或群友的 UID\n"
|
|
@@ -54,18 +58,64 @@ def build_reminder_message(game_key: str) -> str:
|
|
|
54
58
|
)
|
|
55
59
|
|
|
56
60
|
|
|
61
|
+
def retry_delay_minutes(interval_minutes: int, failures: int) -> int:
|
|
62
|
+
"""发送失败后按 2、4、8… 分钟退避,最长不超过正常提醒间隔。"""
|
|
63
|
+
|
|
64
|
+
return max(1, min(2**min(failures, 10), interval_minutes))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def _record_failure(
|
|
68
|
+
repository: UIDRepository,
|
|
69
|
+
reminder: GroupReminder,
|
|
70
|
+
*,
|
|
71
|
+
now: float,
|
|
72
|
+
reason: str,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""记录一次发送失败并安排下一次尝试,避免每分钟无限重试。"""
|
|
75
|
+
|
|
76
|
+
failures = reminder.retry_count + 1
|
|
77
|
+
if reminder.retry_count >= MAX_REMINDER_RETRIES:
|
|
78
|
+
# 退避次数已用尽,之后按正常提醒间隔继续尝试
|
|
79
|
+
delay = reminder.interval_minutes
|
|
80
|
+
logger.debug(f"[game_uid] 群 {reminder.group_id} UID 提醒仍无法发送({reason})")
|
|
81
|
+
else:
|
|
82
|
+
delay = retry_delay_minutes(reminder.interval_minutes, failures)
|
|
83
|
+
if failures < MAX_REMINDER_RETRIES:
|
|
84
|
+
logger.warning(
|
|
85
|
+
f"[game_uid] 群 {reminder.group_id} UID 提醒发送失败({reason}),"
|
|
86
|
+
f"{delay} 分钟后重试(第 {failures} 次)"
|
|
87
|
+
)
|
|
88
|
+
else:
|
|
89
|
+
logger.warning(
|
|
90
|
+
f"[game_uid] 群 {reminder.group_id} UID 提醒已连续失败 {failures} 次({reason}),"
|
|
91
|
+
f"后续改为按正常间隔 {format_duration(reminder.interval_minutes)} 重试"
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
await repository.update_reminder_attempt(
|
|
95
|
+
reminder.group_id,
|
|
96
|
+
next_run_at=now + delay * 60,
|
|
97
|
+
retry_count=min(failures, MAX_REMINDER_RETRIES),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
57
101
|
async def dispatch_due_reminders(*, now: Optional[float] = None) -> int:
|
|
58
102
|
"""发送已到期提醒,返回成功发送数量。"""
|
|
59
103
|
|
|
60
104
|
from .commands import get_repository
|
|
61
105
|
|
|
62
106
|
repository = get_repository()
|
|
107
|
+
current_time = time.time() if now is None else now
|
|
63
108
|
bots = get_bots()
|
|
64
109
|
sent = 0
|
|
65
|
-
for reminder in await repository.get_due_reminders(now=
|
|
110
|
+
for reminder in await repository.get_due_reminders(now=current_time):
|
|
66
111
|
bot = bots.get(reminder.bot_id)
|
|
67
112
|
if bot is None:
|
|
68
|
-
|
|
113
|
+
await _record_failure(
|
|
114
|
+
repository,
|
|
115
|
+
reminder,
|
|
116
|
+
now=current_time,
|
|
117
|
+
reason=f"机器人 {reminder.bot_id} 不在线",
|
|
118
|
+
)
|
|
69
119
|
continue
|
|
70
120
|
try:
|
|
71
121
|
await bot.call_api(
|
|
@@ -74,8 +124,13 @@ async def dispatch_due_reminders(*, now: Optional[float] = None) -> int:
|
|
|
74
124
|
message=build_reminder_message(reminder.game),
|
|
75
125
|
)
|
|
76
126
|
except Exception as error:
|
|
77
|
-
|
|
127
|
+
await _record_failure(
|
|
128
|
+
repository,
|
|
129
|
+
reminder,
|
|
130
|
+
now=current_time,
|
|
131
|
+
reason=str(error) or type(error).__name__,
|
|
132
|
+
)
|
|
78
133
|
continue
|
|
79
|
-
await repository.mark_reminder_sent(reminder.group_id, now=
|
|
134
|
+
await repository.mark_reminder_sent(reminder.group_id, now=current_time)
|
|
80
135
|
sent += 1
|
|
81
136
|
return sent
|
|
@@ -8,6 +8,8 @@ from typing import Optional, Union
|
|
|
8
8
|
|
|
9
9
|
import aiosqlite
|
|
10
10
|
|
|
11
|
+
SQLITE_BUSY_TIMEOUT_SECONDS = 5.0
|
|
12
|
+
|
|
11
13
|
|
|
12
14
|
@dataclass(frozen=True)
|
|
13
15
|
class GroupReminder:
|
|
@@ -16,6 +18,7 @@ class GroupReminder:
|
|
|
16
18
|
game: str
|
|
17
19
|
interval_minutes: int
|
|
18
20
|
next_run_at: float
|
|
21
|
+
retry_count: int = 0
|
|
19
22
|
|
|
20
23
|
|
|
21
24
|
class UIDRepository:
|
|
@@ -26,6 +29,11 @@ class UIDRepository:
|
|
|
26
29
|
self._initialized = False
|
|
27
30
|
self._init_lock = asyncio.Lock()
|
|
28
31
|
|
|
32
|
+
def _connect(self) -> aiosqlite.Connection:
|
|
33
|
+
"""连接数据库;busy timeout 让并发写入排队而不是立刻报锁。"""
|
|
34
|
+
|
|
35
|
+
return aiosqlite.connect(self.db_path, timeout=SQLITE_BUSY_TIMEOUT_SECONDS)
|
|
36
|
+
|
|
29
37
|
async def initialize(self) -> None:
|
|
30
38
|
if self._initialized:
|
|
31
39
|
return
|
|
@@ -34,7 +42,8 @@ class UIDRepository:
|
|
|
34
42
|
if self._initialized:
|
|
35
43
|
return
|
|
36
44
|
await asyncio.to_thread(self.db_path.parent.mkdir, parents=True, exist_ok=True)
|
|
37
|
-
async with
|
|
45
|
+
async with self._connect() as db:
|
|
46
|
+
await db.execute("PRAGMA journal_mode=WAL")
|
|
38
47
|
await db.execute(
|
|
39
48
|
"""
|
|
40
49
|
CREATE TABLE IF NOT EXISTS uid_bindings (
|
|
@@ -53,18 +62,32 @@ class UIDRepository:
|
|
|
53
62
|
bot_id TEXT NOT NULL,
|
|
54
63
|
game TEXT NOT NULL,
|
|
55
64
|
interval_minutes INTEGER NOT NULL,
|
|
56
|
-
next_run_at REAL NOT NULL
|
|
65
|
+
next_run_at REAL NOT NULL,
|
|
66
|
+
retry_count INTEGER NOT NULL DEFAULT 0
|
|
57
67
|
)
|
|
58
68
|
"""
|
|
59
69
|
)
|
|
70
|
+
await self._migrate(db)
|
|
60
71
|
await db.commit()
|
|
61
72
|
self._initialized = True
|
|
62
73
|
|
|
74
|
+
@staticmethod
|
|
75
|
+
async def _migrate(db: aiosqlite.Connection) -> None:
|
|
76
|
+
"""为旧版本建的表补齐新增列。"""
|
|
77
|
+
|
|
78
|
+
cursor = await db.execute("PRAGMA table_info(group_reminders)")
|
|
79
|
+
columns = {str(row[1]) for row in await cursor.fetchall()}
|
|
80
|
+
await cursor.close()
|
|
81
|
+
if "retry_count" not in columns:
|
|
82
|
+
await db.execute(
|
|
83
|
+
"ALTER TABLE group_reminders ADD COLUMN retry_count INTEGER NOT NULL DEFAULT 0"
|
|
84
|
+
)
|
|
85
|
+
|
|
63
86
|
async def set_uid(self, platform_user_id: str, game: str, uid: str) -> bool:
|
|
64
87
|
"""新增或更新 UID;返回 True 表示替换了旧绑定。"""
|
|
65
88
|
|
|
66
89
|
await self.initialize()
|
|
67
|
-
async with
|
|
90
|
+
async with self._connect() as db:
|
|
68
91
|
cursor = await db.execute(
|
|
69
92
|
"SELECT 1 FROM uid_bindings WHERE platform_user_id = ? AND game = ?",
|
|
70
93
|
(platform_user_id, game),
|
|
@@ -86,7 +109,7 @@ class UIDRepository:
|
|
|
86
109
|
|
|
87
110
|
async def get_uid(self, platform_user_id: str, game: str) -> Optional[str]:
|
|
88
111
|
await self.initialize()
|
|
89
|
-
async with
|
|
112
|
+
async with self._connect() as db:
|
|
90
113
|
cursor = await db.execute(
|
|
91
114
|
"SELECT uid FROM uid_bindings WHERE platform_user_id = ? AND game = ?",
|
|
92
115
|
(platform_user_id, game),
|
|
@@ -97,7 +120,7 @@ class UIDRepository:
|
|
|
97
120
|
|
|
98
121
|
async def get_all(self, platform_user_id: str) -> dict[str, str]:
|
|
99
122
|
await self.initialize()
|
|
100
|
-
async with
|
|
123
|
+
async with self._connect() as db:
|
|
101
124
|
cursor = await db.execute(
|
|
102
125
|
"SELECT game, uid FROM uid_bindings WHERE platform_user_id = ?",
|
|
103
126
|
(platform_user_id,),
|
|
@@ -114,7 +137,7 @@ class UIDRepository:
|
|
|
114
137
|
return {}
|
|
115
138
|
|
|
116
139
|
result: dict[str, str] = {}
|
|
117
|
-
async with
|
|
140
|
+
async with self._connect() as db:
|
|
118
141
|
for start in range(0, len(platform_user_ids), 900):
|
|
119
142
|
chunk = platform_user_ids[start : start + 900]
|
|
120
143
|
placeholders = ",".join("?" for _ in chunk)
|
|
@@ -133,7 +156,7 @@ class UIDRepository:
|
|
|
133
156
|
|
|
134
157
|
async def delete_uid(self, platform_user_id: str, game: str) -> bool:
|
|
135
158
|
await self.initialize()
|
|
136
|
-
async with
|
|
159
|
+
async with self._connect() as db:
|
|
137
160
|
cursor = await db.execute(
|
|
138
161
|
"DELETE FROM uid_bindings WHERE platform_user_id = ? AND game = ?",
|
|
139
162
|
(platform_user_id, game),
|
|
@@ -147,7 +170,7 @@ class UIDRepository:
|
|
|
147
170
|
"""删除用户的全部 UID;返回实际删除数量。"""
|
|
148
171
|
|
|
149
172
|
await self.initialize()
|
|
150
|
-
async with
|
|
173
|
+
async with self._connect() as db:
|
|
151
174
|
cursor = await db.execute(
|
|
152
175
|
"DELETE FROM uid_bindings WHERE platform_user_id = ?",
|
|
153
176
|
(platform_user_id,),
|
|
@@ -168,7 +191,7 @@ class UIDRepository:
|
|
|
168
191
|
) -> None:
|
|
169
192
|
await self.initialize()
|
|
170
193
|
current_time = time.time() if now is None else now
|
|
171
|
-
async with
|
|
194
|
+
async with self._connect() as db:
|
|
172
195
|
await db.execute(
|
|
173
196
|
"""
|
|
174
197
|
INSERT INTO group_reminders (group_id, bot_id, game, interval_minutes, next_run_at)
|
|
@@ -185,10 +208,10 @@ class UIDRepository:
|
|
|
185
208
|
|
|
186
209
|
async def get_reminder(self, group_id: str) -> Optional[GroupReminder]:
|
|
187
210
|
await self.initialize()
|
|
188
|
-
async with
|
|
211
|
+
async with self._connect() as db:
|
|
189
212
|
cursor = await db.execute(
|
|
190
213
|
"""
|
|
191
|
-
SELECT group_id, bot_id, game, interval_minutes, next_run_at
|
|
214
|
+
SELECT group_id, bot_id, game, interval_minutes, next_run_at, retry_count
|
|
192
215
|
FROM group_reminders
|
|
193
216
|
WHERE group_id = ?
|
|
194
217
|
""",
|
|
@@ -201,10 +224,10 @@ class UIDRepository:
|
|
|
201
224
|
async def get_due_reminders(self, *, now: Optional[float] = None) -> list[GroupReminder]:
|
|
202
225
|
await self.initialize()
|
|
203
226
|
current_time = time.time() if now is None else now
|
|
204
|
-
async with
|
|
227
|
+
async with self._connect() as db:
|
|
205
228
|
cursor = await db.execute(
|
|
206
229
|
"""
|
|
207
|
-
SELECT group_id, bot_id, game, interval_minutes, next_run_at
|
|
230
|
+
SELECT group_id, bot_id, game, interval_minutes, next_run_at, retry_count
|
|
208
231
|
FROM group_reminders
|
|
209
232
|
WHERE next_run_at <= ?
|
|
210
233
|
ORDER BY next_run_at
|
|
@@ -218,20 +241,42 @@ class UIDRepository:
|
|
|
218
241
|
async def mark_reminder_sent(self, group_id: str, *, now: Optional[float] = None) -> None:
|
|
219
242
|
await self.initialize()
|
|
220
243
|
current_time = time.time() if now is None else now
|
|
221
|
-
async with
|
|
244
|
+
async with self._connect() as db:
|
|
222
245
|
await db.execute(
|
|
223
246
|
"""
|
|
224
247
|
UPDATE group_reminders
|
|
225
|
-
SET next_run_at = ? + interval_minutes * 60
|
|
248
|
+
SET next_run_at = ? + interval_minutes * 60,
|
|
249
|
+
retry_count = 0
|
|
226
250
|
WHERE group_id = ?
|
|
227
251
|
""",
|
|
228
252
|
(current_time, group_id),
|
|
229
253
|
)
|
|
230
254
|
await db.commit()
|
|
231
255
|
|
|
256
|
+
async def update_reminder_attempt(
|
|
257
|
+
self,
|
|
258
|
+
group_id: str,
|
|
259
|
+
*,
|
|
260
|
+
next_run_at: float,
|
|
261
|
+
retry_count: int,
|
|
262
|
+
) -> None:
|
|
263
|
+
"""记录一次发送失败后的重试状态。"""
|
|
264
|
+
|
|
265
|
+
await self.initialize()
|
|
266
|
+
async with self._connect() as db:
|
|
267
|
+
await db.execute(
|
|
268
|
+
"""
|
|
269
|
+
UPDATE group_reminders
|
|
270
|
+
SET next_run_at = ?, retry_count = ?
|
|
271
|
+
WHERE group_id = ?
|
|
272
|
+
""",
|
|
273
|
+
(next_run_at, retry_count, group_id),
|
|
274
|
+
)
|
|
275
|
+
await db.commit()
|
|
276
|
+
|
|
232
277
|
async def delete_reminder(self, group_id: str) -> bool:
|
|
233
278
|
await self.initialize()
|
|
234
|
-
async with
|
|
279
|
+
async with self._connect() as db:
|
|
235
280
|
cursor = await db.execute("DELETE FROM group_reminders WHERE group_id = ?", (group_id,))
|
|
236
281
|
deleted = cursor.rowcount > 0
|
|
237
282
|
await cursor.close()
|
|
File without changes
|
{nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/__init__.py
RENAMED
|
File without changes
|
{nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/config.py
RENAMED
|
File without changes
|
{nonebot_plugin_game_uid-0.3.0 → nonebot_plugin_game_uid-0.4.0}/nonebot_plugin_game_uid/games.py
RENAMED
|
File without changes
|