Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/unvisitor/dicer
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 committed Jul 23, 2024
2 parents b611a0d + 4035cc5 commit 7dbe8da
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 46 deletions.
8 changes: 4 additions & 4 deletions docs/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ next:
link: ./commands/index.md
---

# DicerGirl 使用指南
# 夜骰使用指南

::: warning 注意

Expand All @@ -28,8 +28,8 @@ next:

:::

欢迎使用 Noctisynth DicerGirl!`DicerGirl`插件使用 [Inifni 标准](https://github.com/HydroRoll-Team/infini)它允许不同的跑团模式使用不同的规则包,并允许不同规则构建不同的指令体系。这意味着你在使用新的规则包的时候,你以及使用 DicerGirl 的 KP 或 PL 需要一段时间阅览相关规则包的文档。
欢迎使用夜骰!夜骰(`NoctisDice`插件使用 [Inifni 标准](https://github.com/HydroRoll-Team/infini)它允许遵照规则书创建特定的规则包,并允许不同规则构建不同的指令体系。这意味着你在使用新的规则包的时候,你以及使用夜骰的 KP 或 PL 需要一段时间阅览相关规则包的文档。

值得一提的是,DicerGirl 拥有丰富的指令体系,为使用户快速上手,我们的一些指令参考并兼容了现有的骰系指令。
值得一提的是,夜骰拥有丰富的指令体系,为使用户快速上手,我们的一些指令参考并兼容了现有的骰系指令。

如果你已经准备好开始使用 DicerGirl,请移步骰娘[指令](./commands/index.md)
如果你已经准备好开始使用夜骰4,请移步骰娘[指令](./commands/index.md)
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "dicergirl"
version = "4.0.0-beta.2"
version = "4.0.0-beta.4"
description = "新一代跨平台开源 TRPG 骰娘框架"
authors = [
{name = "苏向夜", email = "[email protected]"},
Expand All @@ -20,8 +20,8 @@ classifiers = [
]

[project.urls]
Homepage = "https://gitee.com/unvisitor/dicer"
"Bug Tracker" = "https://gitee.com/unvisitor/dicer/issues"
Homepage = "https://github.com/noctisynth/dicer"
"Bug Tracker" = "https://github.com/noctisynth/dicer/issues"

[build-system]
requires = ["pdm-backend"]
Expand Down
92 changes: 53 additions & 39 deletions src/dicergirl/on.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from nonebot.plugin import on_message, on_startswith
from nonebot.rule import Rule
from nonebot.adapters import Event
from nonebot.adapters import Event, Bot
from nonebot.matcher import Matcher
from infini.input import Input
from infini.loader import Loader
from infini.injector import Injector
from diceutils.utils import format_msg
from diceutils.parser import CommandParser, Commands, Optional, Bool
from diceutils.status import StatusPool

from .utils import hmr, get_core
from .workflow import put, workflows
import json
import importlib
import sys


class Interceptor:
Expand All @@ -36,38 +37,11 @@ async def __call__(self) -> bool:
return True


injector = Injector()
interceptor = on_message(Rule(Interceptor()), priority=1, block=True)
ipm = on_startswith(".ipm", priority=0, block=True)

packages = ["dicergirl"]

with Loader() as loader:
for package in packages:
loader.load(package)
core = loader.into_core()


def hmr():
global core
importlib.invalidate_caches()

for package in packages:
for name in [name for name in sys.modules.keys() if name.startswith(package)]:
sys.modules[name] = (
importlib.reload(sys.modules[name])
if name in sys.modules
else importlib.import_module(name)
)
sys.modules[package] = (
importlib.reload(sys.modules[package])
if package in sys.modules
else importlib.import_module(package)
)

with Loader() as loader:
for package in packages:
loader.load(package)
core = loader.into_core()
hmr()


@ipm.handle()
Expand All @@ -79,24 +53,41 @@ async def ipm_handler(event: Event, matcher: Matcher):
Bool("hmr"),
Optional("add", str),
Optional("remove", str),
Bool("clear"),
Bool("show"),
]
),
args=args,
auto=True,
).results

status = StatusPool.get("dicergirl")

if commands["hmr"]:
hmr()
return await matcher.send("Infini 热重载完毕")

if commands["add"]:
packages = status.get("bot", "packages") or []
packages.append(commands["add"])
status.set("bot", "packages", packages)
hmr()
return await matcher.send(f"规则包[{commands['add']}]挂载完成")

if commands["clear"]:
status.set("bot", "packages", [])
hmr()
return await matcher.send(f"挂载规则包已清空")

if commands["show"]:
packages = status.get("bot", "packages") or []
return await matcher.send(f"挂载规则包: {[package for package in packages]!r}")

if commands["remove"]:
packages = status.get("bot", "packages") or []
if commands["remove"] in packages:
packages.remove(commands["remove"])
status.set("bot", "packages", packages)
return await matcher.send(f"规则包[{commands['remove']}]卸载完成")
return await matcher.send(f"规则包[{commands['remove']}]未挂载")

Expand All @@ -107,7 +98,7 @@ async def ipm_handler(event: Event, matcher: Matcher):


@interceptor.handle()
async def handler(event: Event, matcher: Matcher):
async def handler(bot: Bot, event: Event, matcher: Matcher):
nb_event_name = event.get_event_name()
nb_event_type = event.get_type()
nb_event_description = event.get_event_description()
Expand All @@ -117,12 +108,27 @@ async def handler(event: Event, matcher: Matcher):
nb_event_json.get("sender", {})
).get("nickname")
user_id = str(event.get_user_id())
self_id = nb_event_json.get("self_id")
self_id = str(nb_event_json.get("self_id"))
group_id = str(event.group_id) if hasattr(event, "group_id") else None
session_id = event.get_session_id()

plain_text = event.get_plaintext()
message = event.get_message()
message = [{"type": msg.type, "data": msg.data} for msg in event.get_message()]
mentions = [
mention["data"]["qq"]
for mention in nb_event_json["original_message"]
if mention["type"] == "at"
]
is_tome = False

if self_id in mentions:
is_tome = True
elif not mentions:
is_tome = True
else:
if mentions:
if nb_event_json["original_message"][0]["type"] != "at":
is_tome = True

input = Input(
plain_text,
Expand All @@ -132,13 +138,21 @@ async def handler(event: Event, matcher: Matcher):
"self_id": self_id,
"group_id": group_id,
"session_id": session_id,
"message": message,
"mentions": mentions,
"is_tome": is_tome,
"nb_event_name": nb_event_name,
"nb_event_type": nb_event_type,
"nb_event_description": nb_event_description,
"nb_event_json": nb_event_json,
"message": message,
"platform": "Nonebot2",
},
)

for output in core.input(input):
await matcher.send(output)
for output in get_core().input(input):
if isinstance(output, str):
await matcher.send(output)
else:
parameters = {"output": output, "bot": bot, "matcher": matcher}
parameters.update(output.variables)
put(injector.inject(workflows.get(output.name), parameters=parameters))
60 changes: 60 additions & 0 deletions src/dicergirl/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from nonebot.adapters import Bot
from pathlib import Path
from diceutils.status import StatusPool
from infini.core import Core
from infini.loader import Loader
from infini.output import Output

import importlib
import sys
import asyncio

status = StatusPool.register("dicergirl")
core: Core


def get_core():
return core


def hmr(output: Output = None):
global core
importlib.invalidate_caches()

packages = status.get("bot", "packages") or []

with Loader() as loader:
for package in packages:
for name in [
name for name in sys.modules.keys() if name.startswith(package)
]:
sys.modules[name] = (
importlib.reload(sys.modules[name])
if name in sys.modules
else importlib.import_module(name)
)
sys.modules[package] = (
importlib.reload(sys.modules[package])
if package in sys.modules
else importlib.import_module(package)
)

loader.load(package)
core = loader.into_core()

if output:
output.status = 0


def file_upload(bot: Bot, filepath: Path, output: Output):
asyncio.run(
bot.call_api(
"upload_group_file",
**{
"group_id": output.variables["group_id"],
"file": str(filepath),
"name": filepath.name,
},
)
)
output.status = 0
10 changes: 10 additions & 0 deletions src/dicergirl/workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from concurrent.futures import ThreadPoolExecutor
from typing import Callable
from .utils import hmr, file_upload

pool = ThreadPoolExecutor(20)
workflows = {"echo.hmr": hmr, "echo.upload": file_upload}


def put(func: Callable):
pool.submit(func)

0 comments on commit 7dbe8da

Please sign in to comment.