From 89c3aa38a65fb49529061dd2745c4cc1b504a747 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Fri, 30 Oct 2020 16:26:04 +0800 Subject: [PATCH 1/8] :art: allow multi value for keyword rule --- docs/api/plugin.md | 44 ++++++++++++++++++++++++++++++++++++++++++++ docs/api/rule.md | 8 +++++--- nonebot/plugin.py | 37 ++++++++++++++++++++++++++----------- nonebot/plugin.pyi | 20 ++++++++++++++++---- nonebot/rule.py | 10 ++++++---- 5 files changed, 97 insertions(+), 22 deletions(-) diff --git a/docs/api/plugin.md b/docs/api/plugin.md index 7aeeabc9..42cdf84f 100644 --- a/docs/api/plugin.md +++ b/docs/api/plugin.md @@ -346,6 +346,50 @@ sidebarDepth: 0 +## `on_keyword(keywords, rule=None, **kwargs)` + + +* **说明** + + 注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。 + + + +* **参数** + + + * `keywords: Set[str]`: 关键词列表 + + + * `rule: Optional[Union[Rule, RuleChecker]]`: 事件响应规则 + + + * `permission: Optional[Permission]`: 事件响应权限 + + + * `handlers: Optional[List[Handler]]`: 事件处理函数列表 + + + * `temp: bool`: 是否为临时事件响应器(仅执行一次) + + + * `priority: int`: 事件响应器优先级 + + + * `block: bool`: 是否阻止事件向更低优先级传递 + + + * `state: Optional[dict]`: 默认的 state + + + +* **返回** + + + * `Type[Matcher]` + + + ## `on_command(cmd, rule=None, aliases=None, **kwargs)` diff --git a/docs/api/rule.md b/docs/api/rule.md index 9142ce2e..dade2a5b 100644 --- a/docs/api/rule.md +++ b/docs/api/rule.md @@ -123,7 +123,7 @@ Rule(async_function, run_sync(sync_function)) -## `keyword(msg)` +## `keyword(*keywords)` * **说明** @@ -135,7 +135,7 @@ Rule(async_function, run_sync(sync_function)) * **参数** - * `msg: str`: 关键词 + * `*keywords: str`: 关键词 @@ -175,7 +175,9 @@ Rule(async_function, run_sync(sync_function)) * **说明** - 根据正则表达式进行匹配 + 根据正则表达式进行匹配。 + + 可以通过 `state["_matched"]` 获取正则表达式匹配成功的文本。 diff --git a/nonebot/plugin.py b/nonebot/plugin.py index b0196bd2..32be4dbc 100644 --- a/nonebot/plugin.py +++ b/nonebot/plugin.py @@ -16,7 +16,7 @@ from nonebot.log import logger from nonebot.matcher import Matcher from nonebot.permission import Permission from nonebot.typing import Handler, RuleChecker -from nonebot.rule import Rule, startswith, endswith, command, regex +from nonebot.rule import Rule, startswith, endswith, keyword, command, regex from nonebot.typing import Any, Set, List, Dict, Type, Tuple, Union, Optional, ModuleType plugins: Dict[str, "Plugin"] = {} @@ -232,8 +232,7 @@ def on_startswith(msg: str, :返回: - ``Type[Matcher]`` """ - return on_message(startswith(msg) & rule, **kwargs) if rule else on_message( - startswith(msg), **kwargs) + return on_message(startswith(msg) & rule, **kwargs) def on_endswith(msg: str, @@ -254,8 +253,28 @@ def on_endswith(msg: str, :返回: - ``Type[Matcher]`` """ - return on_message(endswith(msg) & rule, **kwargs) if rule else on_message( - startswith(msg), **kwargs) + return on_message(endswith(msg) & rule, **kwargs) + + +def on_keyword(keywords: Set[str], + rule: Optional[Union[Rule, RuleChecker]] = None, + **kwargs) -> Type[Matcher]: + """ + :说明: + 注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。 + :参数: + * ``keywords: Set[str]``: 关键词列表 + * ``rule: Optional[Union[Rule, RuleChecker]]``: 事件响应规则 + * ``permission: Optional[Permission]``: 事件响应权限 + * ``handlers: Optional[List[Handler]]``: 事件处理函数列表 + * ``temp: bool``: 是否为临时事件响应器(仅执行一次) + * ``priority: int``: 事件响应器优先级 + * ``block: bool``: 是否阻止事件向更低优先级传递 + * ``state: Optional[dict]``: 默认的 state + :返回: + - ``Type[Matcher]`` + """ + return on_message(keyword(*keywords) & rule, **kwargs) def on_command(cmd: Union[str, Tuple[str, ...]], @@ -290,9 +309,7 @@ def on_command(cmd: Union[str, Tuple[str, ...]], handlers.insert(0, _strip_cmd) commands = set([cmd]) | (aliases or set()) - return on_message(command(*commands) & rule, handlers=handlers, ** - kwargs) if rule else on_message( - command(*commands), handlers=handlers, **kwargs) + return on_message(command(*commands) & rule, handlers=handlers, **kwargs) def on_regex(pattern: str, @@ -317,9 +334,7 @@ def on_regex(pattern: str, :返回: - ``Type[Matcher]`` """ - return on_message(regex(pattern, flags) & - rule, **kwargs) if rule else on_message( - regex(pattern, flags), **kwargs) + return on_message(regex(pattern, flags) & rule, **kwargs) class CommandGroup: diff --git a/nonebot/plugin.pyi b/nonebot/plugin.pyi index dbd96365..6cf93b97 100644 --- a/nonebot/plugin.pyi +++ b/nonebot/plugin.pyi @@ -69,8 +69,8 @@ def on_request(rule: Optional[Union[Rule, RuleChecker]] = ..., def on_startswith(msg: str, rule: Optional[Optional[Union[Rule, RuleChecker]]] = ..., - permission: Optional[Permission] = ..., *, + permission: Optional[Permission] = ..., handlers: Optional[List[Handler]] = ..., temp: bool = ..., priority: int = ..., @@ -81,8 +81,8 @@ def on_startswith(msg: str, def on_endswith(msg: str, rule: Optional[Optional[Union[Rule, RuleChecker]]] = ..., - permission: Optional[Permission] = ..., *, + permission: Optional[Permission] = ..., handlers: Optional[List[Handler]] = ..., temp: bool = ..., priority: int = ..., @@ -91,11 +91,23 @@ def on_endswith(msg: str, ... +def on_keyword(keywords: Set[str], + rule: Optional[Optional[Union[Rule, RuleChecker]]] = ..., + *, + permission: Optional[Permission] = ..., + handlers: Optional[List[Handler]] = ..., + temp: bool = ..., + priority: int = ..., + block: bool = ..., + state: Optional[dict] = ...) -> Type[Matcher]: + ... + + def on_command(cmd: Union[str, Tuple[str, ...]], rule: Optional[Union[Rule, RuleChecker]] = ..., aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., - permission: Optional[Permission] = ..., *, + permission: Optional[Permission] = ..., handlers: Optional[List[Handler]] = ..., temp: bool = ..., priority: int = ..., @@ -107,8 +119,8 @@ def on_command(cmd: Union[str, Tuple[str, ...]], def on_regex(pattern: str, flags: Union[int, re.RegexFlag] = 0, rule: Optional[Rule] = ..., - permission: Optional[Permission] = ..., *, + permission: Optional[Permission] = ..., handlers: Optional[List[Handler]] = ..., temp: bool = ..., priority: int = ..., diff --git a/nonebot/rule.py b/nonebot/rule.py index c950c449..faefbd76 100644 --- a/nonebot/rule.py +++ b/nonebot/rule.py @@ -182,16 +182,17 @@ def endswith(msg: str) -> Rule: return Rule(_endswith) -def keyword(msg: str) -> Rule: +def keyword(*keywords: str) -> Rule: """ :说明: 匹配消息关键词 :参数: - * ``msg: str``: 关键词 + * ``*keywords: str``: 关键词 """ async def _keyword(bot: Bot, event: Event, state: dict) -> bool: - return bool(event.plain_text and msg in event.plain_text) + return bool(event.plain_text and + any(keyword in event.plain_text for keyword in keywords)) return Rule(_keyword) @@ -240,7 +241,7 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule: """ :说明: 根据正则表达式进行匹配。 - + 可以通过 ``state["_matched"]`` 获取正则表达式匹配成功的文本。 :参数: * ``regex: str``: 正则表达式 @@ -261,6 +262,7 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule: else: state["_matched"] = None return False + return Rule(_regex) From 70c7927006320a24ad38acd48787249f3a72e6bf Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Fri, 30 Oct 2020 16:49:31 +0800 Subject: [PATCH 2/8] :bulb: add docstring for cqhttp message --- docs/api/adapters/cqhttp.md | 4 ++++ nonebot/adapters/cqhttp.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/docs/api/adapters/cqhttp.md b/docs/api/adapters/cqhttp.md index aae78f86..73b4e044 100644 --- a/docs/api/adapters/cqhttp.md +++ b/docs/api/adapters/cqhttp.md @@ -405,7 +405,11 @@ CQHTTP 协议 Event 适配。继承属性参考 [BaseEvent](./#class-baseevent) 基类:[`nonebot.adapters.BaseMessageSegment`](#None) +CQHTTP 协议 MessageSegment 适配。具体方法参考协议消息段类型或源码。 + ## _class_ `Message` 基类:[`nonebot.adapters.BaseMessage`](#None) + +CQHTTP 协议 Message 适配。 diff --git a/nonebot/adapters/cqhttp.py b/nonebot/adapters/cqhttp.py index 0f214e36..8a895f17 100644 --- a/nonebot/adapters/cqhttp.py +++ b/nonebot/adapters/cqhttp.py @@ -633,6 +633,9 @@ class Event(BaseEvent): class MessageSegment(BaseMessageSegment): + """ + CQHTTP 协议 MessageSegment 适配。具体方法参考协议消息段类型或源码。 + """ @overrides(BaseMessageSegment) def __init__(self, type: str, data: Dict[str, Any]) -> None: @@ -811,6 +814,9 @@ class MessageSegment(BaseMessageSegment): class Message(BaseMessage): + """ + CQHTTP 协议 Message 适配。 + """ @staticmethod @overrides(BaseMessage) From 22962d55e1f410bb56976189b6073c73e6bf13c6 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 1 Nov 2020 16:18:57 +0800 Subject: [PATCH 3/8] :construction_worker: create plugin issue template --- .github/ISSUE_TEMPLATE/plugin-publish.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/plugin-publish.md diff --git a/.github/ISSUE_TEMPLATE/plugin-publish.md b/.github/ISSUE_TEMPLATE/plugin-publish.md new file mode 100644 index 00000000..9ecc8321 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/plugin-publish.md @@ -0,0 +1,22 @@ +--- +name: Plugin Publish +about: Publish your plugin to nonebot homepage and nb-cli +title: "Plugin: blabla 的插件" +labels: Plugin +assignees: "" + +--- + +**你的插件名称:** + +nonebot-plugin-example + +> 请事先发布插件到[pypi](https://pypi.org/),插件名称应与 pypi 项目名称**保持一致** + +**简短描述插件功能** + +例:复读机 + +**插件项目仓库/主页链接** + +your project link here From 15e59a1778a5249830c78645861ffbefc59c2386 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 1 Nov 2020 18:21:10 +0800 Subject: [PATCH 4/8] :construction_worker: update plugin template --- .github/ISSUE_TEMPLATE/plugin-publish.md | 34 ++++++++++++++++-------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/plugin-publish.md b/.github/ISSUE_TEMPLATE/plugin-publish.md index 9ecc8321..a6189f77 100644 --- a/.github/ISSUE_TEMPLATE/plugin-publish.md +++ b/.github/ISSUE_TEMPLATE/plugin-publish.md @@ -1,22 +1,34 @@ --- + name: Plugin Publish about: Publish your plugin to nonebot homepage and nb-cli title: "Plugin: blabla 的插件" labels: Plugin assignees: "" - ---- - -**你的插件名称:** - -nonebot-plugin-example - -> 请事先发布插件到[pypi](https://pypi.org/),插件名称应与 pypi 项目名称**保持一致** - -**简短描述插件功能** +---**你的插件名称:** 例:复读机 +**简短描述插件功能:** + +例:复读群友的消息 + +**插件 import 使用的名称** + +例:nonebot-plugin-example + +**插件 install 使用的名称** + +例 1:nonebot-plugin-example + +通过 pypi 安装 + +> 请事先发布插件到[pypi](https://pypi.org/) + +例 2:git+https://github.com/nonebot/nonebot-plugin-example + +从 github 仓库安装 + **插件项目仓库/主页链接** -your project link here +例:nonebot/nonebot2(默认 github )或其他链接 From af7f42ac60472fddc585559f254b97d38fe0361b Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 1 Nov 2020 18:21:31 +0800 Subject: [PATCH 5/8] :memo: update plugin store page --- docs/.vuepress/components/Plugins.vue | 92 +++++++++++++++++++++------ docs/.vuepress/public/plugins.json | 22 +------ package-lock.json | 21 ++++-- package.json | 3 +- 4 files changed, 95 insertions(+), 43 deletions(-) diff --git a/docs/.vuepress/components/Plugins.vue b/docs/.vuepress/components/Plugins.vue index a36b9d39..977ed165 100644 --- a/docs/.vuepress/components/Plugins.vue +++ b/docs/.vuepress/components/Plugins.vue @@ -1,33 +1,77 @@ @@ -37,3 +81,13 @@ export default { min-height: 0 !important; } + + diff --git a/docs/.vuepress/public/plugins.json b/docs/.vuepress/public/plugins.json index b92dde26..8f97eaa6 100644 --- a/docs/.vuepress/public/plugins.json +++ b/docs/.vuepress/public/plugins.json @@ -1,24 +1,8 @@ [ { - "name": "nonebot-plugin-status", - "desc": "通过戳一戳获取服务器状态", - "author": "nonebot", - "repo": "nonebot/nonebot2" - }, - { - "name": "nonebot-plugin-status", - "desc": "通过戳一戳获取服务器状态", - "author": "nonebot", - "repo": "nonebot/nonebot2" - }, - { - "name": "nonebot-plugin-status", - "desc": "通过戳一戳获取服务器状态", - "author": "nonebot", - "repo": "nonebot/nonebot2" - }, - { - "name": "nonebot-plugin-status", + "id": "nonebot-plugin-status", + "link": "nonebot-plugin-status", + "name": "状态监控", "desc": "通过戳一戳获取服务器状态", "author": "nonebot", "repo": "nonebot/nonebot2" diff --git a/package-lock.json b/package-lock.json index efb2f7bf..34a6742d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3313,6 +3313,14 @@ "integrity": "sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=", "dev": true }, + "copy-to-clipboard": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/copy-to-clipboard/-/copy-to-clipboard-3.3.1.tgz", + "integrity": "sha512-i13qo6kIHTTpCm8/Wup+0b1mVWETvu2kIMzKoK8FpkLkFxlt0znUAHcMzox+T8sPlqtZXq3CulEjQHsYiGFJUw==", + "requires": { + "toggle-selection": "^1.0.6" + } + }, "copy-webpack-plugin": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-5.1.2.tgz", @@ -9711,6 +9719,11 @@ "repeat-string": "^1.6.1" } }, + "toggle-selection": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/toggle-selection/-/toggle-selection-1.0.6.tgz", + "integrity": "sha1-bkWxJj8gF/oKzH2J14sVuL932jI=" + }, "toidentifier": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", @@ -10461,7 +10474,7 @@ } }, "vuepress-theme-nonebot": { - "version": "git+https://github.com/nonebot/vuepress-theme-nonebot.git#16c96d1cd12cbb72d0233875b0a671cada93ca2a", + "version": "git+https://github.com/nonebot/vuepress-theme-nonebot.git#29b6c7a7b0f69eee8fa98b78094057de20c4233c", "from": "git+https://github.com/nonebot/vuepress-theme-nonebot.git", "dev": true, "requires": { @@ -10487,9 +10500,9 @@ } }, "vuetify": { - "version": "2.3.14", - "resolved": "https://registry.npmjs.org/vuetify/-/vuetify-2.3.14.tgz", - "integrity": "sha512-1Ys1MreJQOL/Ddp3YotBi1SlC2+1A0/RVkDXX3Azspt8incPdAnNB0JyChHiJ/TM+L+KSA7T4EXF9YDrCWENmg==" + "version": "2.3.16", + "resolved": "https://registry.npmjs.org/vuetify/-/vuetify-2.3.16.tgz", + "integrity": "sha512-LHPqY+Gmyb/75xJscO0a3CuB4ZdpqHLNaGMAbmfTyapI8Q02+hjABEZzitFU/XObD2KhrNWPJzmGZPhbshGUzg==" }, "watchpack": { "version": "1.7.4", diff --git a/package.json b/package.json index 0d01b7b2..3aec35d7 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "vuepress-theme-nonebot": "git+https://github.com/nonebot/vuepress-theme-nonebot.git" }, "dependencies": { - "vuetify": "^2.3.14", + "copy-to-clipboard": "^3.3.1", + "vuetify": "^2.3.16", "wowjs": "^1.1.3" } } From d9ea95c67ee637accf4d73d6ce4ddcd01ef9b940 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 1 Nov 2020 19:20:18 +0800 Subject: [PATCH 6/8] :ambulance: fix message segment get --- nonebot/adapters/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/nonebot/adapters/__init__.py b/nonebot/adapters/__init__.py index b7abd955..1f34d01e 100644 --- a/nonebot/adapters/__init__.py +++ b/nonebot/adapters/__init__.py @@ -286,6 +286,9 @@ class BaseMessageSegment(abc.ABC): def __setitem__(self, key, value): return setattr(self, key, value) + def get(self, key, default=None): + return getattr(self, key, default) + @classmethod @abc.abstractmethod def text(cls, text: str) -> "BaseMessageSegment": From 71ee9aee2132212e21a59230f5be37d623a6a8fe Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Mon, 2 Nov 2020 19:07:53 +0800 Subject: [PATCH 7/8] :construction: update plugin store --- docs/.vuepress/components/Plugins.vue | 77 +++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/docs/.vuepress/components/Plugins.vue b/docs/.vuepress/components/Plugins.vue index 977ed165..435e7938 100644 --- a/docs/.vuepress/components/Plugins.vue +++ b/docs/.vuepress/components/Plugins.vue @@ -1,14 +1,50 @@