Skip to content

Commit b8e4068

Browse files
authored
feat: support key-value storage for plugins (#4048)
* feat: support key-value storage for plugins * fix: remove unnecessary initialization method from Main class
1 parent 0916177 commit b8e4068

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

astrbot/core/star/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
from astrbot.core.provider import Provider
33
from astrbot.core.star.star_tools import StarTools
44
from astrbot.core.utils.command_parser import CommandParserMixin
5+
from astrbot.core.utils.plugin_kv_store import PluginKVStoreMixin
56

67
from .context import Context
78
from .star import StarMetadata, star_map, star_registry
89
from .star_manager import PluginManager
910

1011

11-
class Star(CommandParserMixin):
12+
class Star(CommandParserMixin, PluginKVStoreMixin):
1213
"""所有插件(Star)的父类,所有插件都应该继承于这个类"""
1314

15+
author: str
16+
name: str
17+
1418
def __init__(self, context: Context, config: dict | None = None):
1519
StarTools.initialize(context)
1620
self.context = context

astrbot/core/star/star_manager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,18 @@ async def load(self, specified_module_path=None, specified_dir_name=None):
467467
metadata.star_cls = metadata.star_cls_type(
468468
context=self.context,
469469
)
470+
471+
p_name = (metadata.name or "unknown").lower().replace("/", "_")
472+
p_author = (
473+
(metadata.author or "unknown").lower().replace("/", "_")
474+
)
475+
setattr(metadata.star_cls, "name", p_name)
476+
setattr(metadata.star_cls, "author", p_author)
477+
setattr(
478+
metadata.star_cls,
479+
"plugin_id",
480+
f"{p_author}/{p_name}",
481+
)
470482
else:
471483
logger.info(f"插件 {metadata.name} 已被禁用。")
472484

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import TypeVar
2+
3+
from astrbot.core import sp
4+
5+
SUPPORTED_VALUE_TYPES = int | float | str | bytes | bool | dict | list | None
6+
_VT = TypeVar("_VT")
7+
8+
9+
class PluginKVStoreMixin:
10+
"""为插件提供键值存储功能的 Mixin 类"""
11+
12+
plugin_id: str
13+
14+
async def put_kv_data(
15+
self,
16+
key: str,
17+
value: SUPPORTED_VALUE_TYPES,
18+
) -> None:
19+
"""为指定插件存储一个键值对"""
20+
await sp.put_async("plugin", self.plugin_id, key, value)
21+
22+
async def get_kv_data(self, key: str, default: _VT) -> _VT | None:
23+
"""获取指定插件存储的键值对"""
24+
return await sp.get_async("plugin", self.plugin_id, key, default)
25+
26+
async def delete_kv_data(self, key: str) -> None:
27+
"""删除指定插件存储的键值对"""
28+
await sp.remove_async("plugin", self.plugin_id, key)

0 commit comments

Comments
 (0)