aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author简律纯 <i@jyunko.cn>2023-11-12 06:05:16 +0800
committerGitHub <noreply@github.com>2023-11-12 06:05:16 +0800
commit766f633e486513fab9d44bb0078ba83ae77390d7 (patch)
treede1146e25c1aa5b01fcd3d8578e9a2bb0b03fa72
parent7959c6e7190cb7ee6ed355894bf40616e5e8201b (diff)
downloadHydroRoll-766f633e486513fab9d44bb0078ba83ae77390d7.tar.gz
HydroRoll-766f633e486513fab9d44bb0078ba83ae77390d7.zip
feat: Input stream detective for builtin command (#75)
* chore: make shiki happy * chore: wtf,撤回上面的commit * fix: 修复错误的get()传参 * feat(cmd): add `Get` and `Set` command base * chore: update line code * chore: sync code * chore: add `root` config * feat: succeed built a input stream system * 'Refactored by Sourcery' (#77) Co-authored-by: Sourcery AI <> * Delete .github/workflows/maintain-one-comment.yml --------- Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
-rw-r--r--.github/workflows/maintain-one-comment.yml22
-rw-r--r--example/plugins/HydroRoll/__init__.py76
-rw-r--r--example/plugins/HydroRoll/command.py9
-rw-r--r--example/plugins/HydroRoll/config.py3
-rw-r--r--example/plugins/HydroRoll/utils.py19
5 files changed, 73 insertions, 56 deletions
diff --git a/.github/workflows/maintain-one-comment.yml b/.github/workflows/maintain-one-comment.yml
deleted file mode 100644
index beed0ce..0000000
--- a/.github/workflows/maintain-one-comment.yml
+++ /dev/null
@@ -1,22 +0,0 @@
-name: Maintain One Comment
-
-on:
- issues:
- types: [opened, edited]
- issue_comment:
- types: [created, edited]
- pull_request:
- types: [assigned, opened, synchronize, edited]
-
-jobs:
- comment:
- runs-on: ubuntu-latest
- steps:
- - name: maintain-comment
- uses: actions-cool/maintain-one-comment@v3
- with:
- token: ${{ secrets.GITHUB_TOKEN }}
- body: |
- Hi 😀
- emojis: '+1, laugh'
- body-include: '<!-- Created by actions-cool/maintain-one-comment -->'
diff --git a/example/plugins/HydroRoll/__init__.py b/example/plugins/HydroRoll/__init__.py
index 5edc2a7..151a58a 100644
--- a/example/plugins/HydroRoll/__init__.py
+++ b/example/plugins/HydroRoll/__init__.py
@@ -10,6 +10,7 @@ from iamai.log import logger
from .config import Directory, GlobalConfig, Models
from .utils import *
from .models.Transformer import query
+from .command import Set, Get
from iamai.exceptions import GetEventTimeout
BASE_DIR = dirname(abspath("__file__"))
@@ -48,27 +49,46 @@ class HydroRoll(Plugin):
@BODY: HydroRollCore actives the rule-packages.
"""
args = self.event.get_plain_text().split(" ")
- try:
- event = await self.event.adapter.get(
- lambda x: x.type == "message", timeout=10
- )
- except GetEventTimeout:
- return
- else:
- if args[0] == ".core":
- ...
- if args[0].startswith(".set"):
- ...
- elif args[0].startswith(".get"):
- ...
- elif args[0].startswith(".test"):
- try:
- result = eval(
- self.event.message.get_plain_text()[5:]
- ) # literal_eval(self.event.message.get_plain_text()[5:])
- await self.event.reply(str(result))
- except Exception as error:
- await self.event.reply(f"{error!r}")
+ command_list = [".root", ".roots", ".core", ".set", ".get", ".test"]
+ for cmd in command_list:
+ if cmd.startswith(args[0]):
+ logger.info(cmd)
+ if args[0] != cmd:
+ try:
+ flag = True
+ current_cmd = args[0]
+ while flag:
+ flag = False
+ event = await self.ask(ask_text=None, timeout=3)
+ current_cmd = current_cmd + event.get_plain_text()
+ if cmd.startswith(current_cmd):
+ if current_cmd != cmd:
+ flag = True
+ else:
+ await self.event.reply(f"{cmd}")
+ flag = False
+ except GetEventTimeout:
+ continue
+ else:
+ await self.event.reply(cmd)
+ # if args[0] in [".root", ".roots"]:
+ # import requests
+
+ # data = requests.get("https://vercel-hitokoto.vercel.app/api/roots").json()
+ # await self.event.reply(data["line"])
+ # else:
+ # if args[0] == ".core":
+ # ...
+ # if args[0].startswith(".set"):
+ # resolve = Set(args[1:]) # TODO: handle multiple sets
+ # elif args[0].startswith(".get"):
+ # resolve = Get(args[1:]) # TODO: handle multiple gets
+ # elif args[0].startswith(".test"):
+ # try:
+ # result = eval(self.event.message.get_plain_text()[5:])
+ # await self.event.reply(str(result))
+ # except Exception as error:
+ # await self.event.reply(f"{error!r}")
async def rule(self) -> bool:
"""
@@ -124,3 +144,17 @@ class HydroRoll(Plugin):
def load_models(self):
self.models = self._load_models(self.model_path_list, self.model_dict)
+
+ async def ask(self, ask_text: str | None, timeout: int = 10) -> None:
+ if ask_text:
+ await self.event.reply(ask_text)
+ try:
+ event = await self.event.adapter.get(
+ lambda x: x.type == "message"
+ and x.group_id == self.event.group_id
+ and x.user_id == self.event.user_id,
+ timeout=timeout,
+ )
+ return event
+ except GetEventTimeout as e:
+ raise GetEventTimeout from e
diff --git a/example/plugins/HydroRoll/command.py b/example/plugins/HydroRoll/command.py
new file mode 100644
index 0000000..5a31534
--- /dev/null
+++ b/example/plugins/HydroRoll/command.py
@@ -0,0 +1,9 @@
+from .utils import CommandPluginBase
+
+class Get(CommandPluginBase):
+ ...
+
+
+
+class Set(CommandPluginBase):
+ ... \ No newline at end of file
diff --git a/example/plugins/HydroRoll/config.py b/example/plugins/HydroRoll/config.py
index 194a956..788c27a 100644
--- a/example/plugins/HydroRoll/config.py
+++ b/example/plugins/HydroRoll/config.py
@@ -41,7 +41,7 @@ class RegexPluginConfig(BasePluginConfig):
class CommandPluginConfig(RegexPluginConfig):
- command_prefix: Set[str] = {":", "你妈", "👅", "约瑟夫妥斯妥耶夫斯基戴安那只鸡🐔"}
+ command_prefix: Set[str] = {":"}
"""命令前缀。"""
command: Set[str] = {}
"""命令文本。"""
@@ -146,3 +146,4 @@ class Models:
def get_models_dict(self) -> dict:
return self.builtin_models
+
diff --git a/example/plugins/HydroRoll/utils.py b/example/plugins/HydroRoll/utils.py
index 1f5e8bc..2665580 100644
--- a/example/plugins/HydroRoll/utils.py
+++ b/example/plugins/HydroRoll/utils.py
@@ -20,7 +20,7 @@ class BasePlugin(
ABC,
Generic[T_State, T_Config],
):
- Config: Type[T_Config] = BasePluginConfig
+ Config: Type[T_Config] = BasePluginConfig # type: ignore
def format_str(self, format_str: str, message_str: str = "") -> str:
return format_str.format(
@@ -60,9 +60,6 @@ class BasePlugin(
or self.event.group_id in self.config.accept_group
):
return self.str_match(match_str)
- elif self.config.handle_group_message:
- if self.event.message_type == "guild":
- return self.str_match(match_str)
return False
@abstractmethod
@@ -153,14 +150,12 @@ class HydroDice:
if streamline:
return str(total)
- else:
- if len(rolls) > int(threshold):
- return str(total)
- rolls_str = " + ".join(str(r) for r in rolls)
- result_str = (
- f"{total} = {rolls_str}" if is_reversed else f"{rolls_str} = {total}"
- )
- return result_str
+ if len(rolls) > int(threshold):
+ return str(total)
+ rolls_str = " + ".join(str(r) for r in rolls)
+ return (
+ f"{total} = {rolls_str}" if is_reversed else f"{rolls_str} = {total}"
+ )
def find_max_similarity(input_string, string_list):