🔀 Merge pull request #260

Pre Release 2.0.0a11
This commit is contained in:
Ju4tCode
2021-03-05 06:39:19 -06:00
committed by GitHub
96 changed files with 11537 additions and 663 deletions

View File

@ -0,0 +1,221 @@
<template>
<v-card flat class="adapters">
<v-row>
<v-col cols="12" sm="4">
<v-text-field
v-model="filterText"
dense
rounded
outlined
clearable
hide-details
label="Filter Adapter"
>
<template v-slot:prepend-inner>
<div class="v-input__icon v-input__icon--prepend-inner">
<v-icon small>fa-filter</v-icon>
</div>
</template>
</v-text-field>
</v-col>
<v-col cols="12" sm="4">
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on, attrs }">
<v-btn dark block color="primary" v-bind="attrs" v-on="on"
>Publish Your Adapter
</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">Adapter Information</span>
</v-card-title>
<v-card-text>
<v-form ref="newAdapterForm" v-model="valid" lazy-validation>
<v-container>
<v-row>
<v-col cols="12">
<v-text-field
v-model="newAdapter.name"
label="协议名称"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newAdapter.desc"
label="协议介绍"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newAdapter.id"
label="PyPI 项目名"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newAdapter.link"
label="协议 import 包名"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newAdapter.repo"
label="仓库/主页链接"
required
></v-text-field>
</v-col>
</v-row>
</v-container>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="dialog = false">
Close
</v-btn>
<v-btn
:disabled="!valid"
color="blue darken-1"
text
@click="
dialog = false;
publishAdapter();
"
>
Publish
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-col>
<v-col cols="12" sm="4">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
<hr />
<v-row>
<v-col
cols="12"
sm="6"
v-for="(adapter, index) in displayAdapters"
:key="index"
>
<PublishCard
:name="adapter.name"
:desc="adapter.desc"
:id="adapter.id"
:author="adapter.author"
:link="adapter.repo"
></PublishCard>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
</v-card>
</template>
<script>
import PublishCard from "./PublishCard.vue";
import adapters from "../public/adapters.json";
export default {
name: "Adapters",
components: {
PublishCard
},
data() {
return {
adapters: adapters,
filterText: "",
page: 1,
dialog: false,
valid: false,
newAdapter: {
name: null,
desc: null,
id: null,
link: null,
repo: null
}
};
},
computed: {
pageNum() {
return Math.ceil(this.filteredAdapters.length / 10);
},
filteredAdapters() {
return this.adapters.filter(adapter => {
return (
adapter.id.indexOf(this.filterText || "") != -1 ||
adapter.name.indexOf(this.filterText || "") != -1 ||
adapter.desc.indexOf(this.filterText || "") != -1 ||
adapter.author.indexOf(this.filterText || "") != -1
);
});
},
displayAdapters() {
return this.filteredAdapters.slice((this.page - 1) * 10, this.page * 10);
},
publishPlugin() {
if (!this.$refs.newAdapterForm.validate()) {
return;
}
const title = encodeURIComponent(
`Adapter: ${this.newAdapter.name}`
).replace(/%2B/gi, "+");
const body = encodeURIComponent(
`
**协议名称:**
${this.newAdapter.name}
**协议功能:**
${this.newAdapter.desc}
**PyPI 项目名:**
${this.newAdapter.link}
**协议 import 包名:**
${this.newAdapter.id}
**协议项目仓库/主页链接:**
${this.newAdapter.repo}
<!-- DO NOT EDIT ! -->
<!--
- id: ${this.newAdapter.id}
- link: ${this.newAdapter.link}
- name: ${this.newAdapter.name}
- desc: ${this.newAdapter.desc}
- repo: ${this.newAdapter.repo}
-->
`.trim()
).replace(/%2B/gi, "+");
window.open(
`https://github.com/nonebot/nonebot2/issues/new?title=${title}&body=${body}&labels=Adapter`
);
}
}
};
</script>

View File

@ -0,0 +1,190 @@
<template>
<v-card flat class="bots">
<v-row>
<v-col cols="12" sm="4">
<v-text-field
v-model="filterText"
dense
rounded
outlined
clearable
hide-details
label="Filter Bot"
>
<template v-slot:prepend-inner>
<div class="v-input__icon v-input__icon--prepend-inner">
<v-icon small>fa-filter</v-icon>
</div>
</template>
</v-text-field>
</v-col>
<v-col cols="12" sm="4">
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on, attrs }">
<v-btn dark block color="primary" v-bind="attrs" v-on="on"
>Publish Your Bot
</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">Bot Information</span>
</v-card-title>
<v-card-text>
<v-form ref="newBotForm" v-model="valid" lazy-validation>
<v-container>
<v-row>
<v-col cols="12">
<v-text-field
v-model="newBot.name"
label="机器人名称"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newBot.desc"
label="机器人介绍"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newBot.repo"
label="仓库/主页链接"
required
></v-text-field>
</v-col>
</v-row>
</v-container>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="dialog = false">
Close
</v-btn>
<v-btn
:disabled="!valid"
color="blue darken-1"
text
@click="
dialog = false;
publishBot();
"
>
Publish
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-col>
<v-col cols="12" sm="4">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
<hr />
<v-row>
<v-col cols="12" sm="6" v-for="(bot, index) in displayBots" :key="index">
<PublishCard
:name="bot.name"
:desc="bot.desc"
:author="bot.author"
:link="bot.repo"
></PublishCard>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
</v-card>
</template>
<script>
import PublishCard from "./PublishCard.vue";
import bots from "../public/bots.json";
export default {
name: "Bots",
components: {
PublishCard
},
data() {
return {
bots: bots,
filterText: "",
page: 1,
dialog: false,
valid: false,
newBot: {
name: null,
desc: null,
repo: null
}
};
},
computed: {
pageNum() {
return Math.ceil(this.filteredBots.length / 10);
},
filteredBots() {
return this.bots.filter(bot => {
return (
bot.id.indexOf(this.filterText || "") != -1 ||
bot.name.indexOf(this.filterText || "") != -1 ||
bot.desc.indexOf(this.filterText || "") != -1 ||
bot.author.indexOf(this.filterText || "") != -1
);
});
},
displayBots() {
return this.filteredBots.slice((this.page - 1) * 10, this.page * 10);
},
publishBot() {
if (!this.$refs.newBotForm.validate()) {
return;
}
const title = encodeURIComponent(`Bot: ${this.newBot.name}`).replace(
/%2B/gi,
"+"
);
const body = encodeURIComponent(
`
**机器人名称:**
${this.newBot.name}
**机器人功能:**
${this.newBot.desc}
**机器人项目仓库/主页链接:**
${this.newBot.repo}
<!-- DO NOT EDIT ! -->
<!--
- name: ${this.newBot.name}
- desc: ${this.newBot.desc}
- repo: ${this.newBot.repo}
-->
`.trim()
).replace(/%2B/gi, "+");
window.open(
`https://github.com/nonebot/nonebot2/issues/new?title=${title}&body=${body}&labels=Bot`
);
}
}
};
</script>

View File

@ -0,0 +1,223 @@
<template>
<v-card flat class="plugins">
<v-row>
<v-col cols="12" sm="4">
<v-text-field
v-model="filterText"
dense
rounded
outlined
clearable
hide-details
label="Filter Plugin"
>
<template v-slot:prepend-inner>
<div class="v-input__icon v-input__icon--prepend-inner">
<v-icon small>fa-filter</v-icon>
</div>
</template>
</v-text-field>
</v-col>
<v-col cols="12" sm="4">
<v-dialog v-model="dialog" max-width="600px">
<template v-slot:activator="{ on, attrs }">
<v-btn dark block color="primary" v-bind="attrs" v-on="on"
>Publish Your Plugin
</v-btn>
</template>
<v-card>
<v-card-title>
<span class="headline">Plugin Information</span>
</v-card-title>
<v-card-text>
<v-form ref="newPluginForm" v-model="valid" lazy-validation>
<v-container>
<v-row>
<v-col cols="12">
<v-text-field
v-model="newPlugin.name"
label="插件名称"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newPlugin.desc"
label="插件介绍"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newPlugin.id"
label="PyPI 项目名"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newPlugin.link"
label="插件 import 包名"
required
></v-text-field>
</v-col>
<v-col cols="12">
<v-text-field
v-model="newPlugin.repo"
label="仓库/主页链接"
required
></v-text-field>
</v-col>
</v-row>
</v-container>
</v-form>
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="blue darken-1" text @click="dialog = false">
Close
</v-btn>
<v-btn
:disabled="!valid"
color="blue darken-1"
text
@click="
dialog = false;
publishPlugin();
"
>
Publish
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-col>
<v-col cols="12" sm="4">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
<hr />
<v-row>
<v-col
cols="12"
sm="6"
v-for="(plugin, index) in displayPlugins"
:key="index"
>
<PublishCard
:name="plugin.name"
:desc="plugin.desc"
:id="plugin.id"
:author="plugin.author"
:link="plugin.repo"
text="copy nb install command"
:command="`nb plugin install ${plugin.id}`"
></PublishCard>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
</v-card>
</template>
<script>
import PublishCard from "./PublishCard.vue";
import plugins from "../public/plugins.json";
export default {
name: "Plugins",
components: {
PublishCard
},
data() {
return {
plugins: plugins,
filterText: "",
page: 1,
dialog: false,
valid: false,
newPlugin: {
name: null,
desc: null,
id: null,
link: null,
repo: null
}
};
},
computed: {
pageNum() {
return Math.ceil(this.filteredPlugins.length / 10);
},
filteredPlugins() {
return this.plugins.filter(plugin => {
return (
plugin.id.indexOf(this.filterText || "") != -1 ||
plugin.name.indexOf(this.filterText || "") != -1 ||
plugin.desc.indexOf(this.filterText || "") != -1 ||
plugin.author.indexOf(this.filterText || "") != -1
);
});
},
displayPlugins() {
return this.filteredPlugins.slice((this.page - 1) * 10, this.page * 10);
},
publishPlugin() {
if (!this.$refs.newPluginForm.validate()) {
return;
}
const title = encodeURIComponent(
`Plugin: ${this.newPlugin.name}`
).replace(/%2B/gi, "+");
const body = encodeURIComponent(
`
**插件名称:**
${this.newPlugin.name}
**插件功能:**
${this.newPlugin.desc}
**PyPI 项目名:**
${this.newPlugin.link}
**插件 import 包名:**
${this.newPlugin.id}
**插件项目仓库/主页链接:**
${this.newPlugin.repo}
<!-- DO NOT EDIT ! -->
<!--
- id: ${this.newPlugin.id}
- link: ${this.newPlugin.link}
- name: ${this.newPlugin.name}
- desc: ${this.newPlugin.desc}
- repo: ${this.newPlugin.repo}
-->
`.trim()
).replace(/%2B/gi, "+");
window.open(
`https://github.com/nonebot/nonebot2/issues/new?title=${title}&body=${body}&labels=Plugin`
);
}
}
};
</script>

View File

@ -1,165 +0,0 @@
<template>
<div class="plugins">
<v-app>
<v-main>
<v-row>
<v-col cols="12" sm="4">
<v-text-field
v-model="filterText"
dense
rounded
outlined
clearable
hide-details
label="Filter Plugin"
>
<template v-slot:prepend-inner>
<div class="v-input__icon v-input__icon--prepend-inner">
<v-icon small>fa-filter</v-icon>
</div>
</template>
</v-text-field>
</v-col>
<v-col cols="12" sm="4">
<v-btn
block
color="primary"
target="_blank"
rel="noopener noreferrer"
href="https://github.com/nonebot/nonebot2/issues/new?labels=Plugin&template=plugin-publish.md&title=Plugin%3A+blabla+的插件"
>Publish Your Plugin
</v-btn>
</v-col>
<v-col cols="12" sm="4">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
<hr />
<v-row>
<v-col
cols="12"
sm="6"
v-for="(plugin, index) in displayPlugins"
:key="index"
>
<v-card>
<v-card-title>
{{ plugin.name }}
<v-spacer></v-spacer>
<a
class="repo-link"
v-if="repoLink(plugin.repo)"
rel="noopener noreferrer"
target="_blank"
:title="plugin.repo"
:href="repoLink(plugin.repo)"
>
<v-icon>fab fa-github</v-icon>
</a>
</v-card-title>
<v-card-text>{{ plugin.desc }}</v-card-text>
<v-card-text>
<v-icon x-small>fa-fingerprint</v-icon>
{{ plugin.id }}
<v-icon x-small class="ml-2">fa-user</v-icon>
{{ plugin.author }}
</v-card-text>
<v-card-actions>
<v-btn
block
depressed
class="btn-copy"
@click="copyCommand(plugin)"
>
copy nb-cli command
<v-icon right small>fa-copy</v-icon>
</v-btn>
<v-snackbar v-model="snackbar">Copied!</v-snackbar>
</v-card-actions>
</v-card>
</v-col>
</v-row>
<v-row>
<v-col cols="12">
<v-pagination
v-model="page"
:length="pageNum"
prev-icon="fa-caret-left"
next-icon="fa-caret-right"
></v-pagination>
</v-col>
</v-row>
</v-main>
</v-app>
</div>
</template>
<script>
import copy from "copy-to-clipboard";
import plugins from "../public/plugins.json";
export default {
name: "Plugins",
data() {
return {
plugins: plugins,
snackbar: false,
filterText: "",
page: 1
};
},
computed: {
pageNum() {
return Math.ceil(this.filteredPlugins.length / 10);
},
filteredPlugins() {
return this.plugins.filter(plugin => {
return (
plugin.id.indexOf(this.filterText) != -1 ||
plugin.name.indexOf(this.filterText) != -1 ||
plugin.desc.indexOf(this.filterText) != -1 ||
plugin.author.indexOf(this.filterText) != -1
);
});
},
displayPlugins() {
return this.filteredPlugins.slice((this.page - 1) * 10, this.page * 10);
}
},
methods: {
repoLink(repo) {
if (repo) {
return /^https?:/.test(repo) ? repo : `https://github.com/${repo}`;
}
return null;
},
copyCommand(plugin) {
copy(`nb plugin install ${plugin.id}`, {
format: "text/plain"
});
this.snackbar = true;
}
}
};
</script>
<style>
.v-application--wrap {
min-height: 0 !important;
}
</style>
<style scoped>
.repo-link {
text-decoration: none !important;
display: inline-block;
}
.repo-link:hover i {
color: #ea5252;
}
</style>

View File

@ -0,0 +1,84 @@
<template>
<v-card>
<v-card-title>
{{ name }}
<v-spacer></v-spacer>
<a
class="repo-link"
v-if="repoLink(link)"
rel="noopener noreferrer"
target="_blank"
:title="link"
:href="repoLink(link)"
>
<v-icon>fab fa-github</v-icon>
</a>
</v-card-title>
<v-card-text>{{ desc }}</v-card-text>
<v-card-text>
<template v-if="id">
<v-icon x-small>fa-fingerprint</v-icon>
{{ id }}
</template>
<v-icon x-small class="ml-2">fa-user</v-icon>
{{ author }}
</v-card-text>
<v-card-actions v-if="showCommand">
<v-btn block depressed class="btn-copy" @click="copyCommand()">
{{ text }}
<v-icon right small>fa-copy</v-icon>
</v-btn>
<v-snackbar v-model="snackbar">Copied!</v-snackbar>
</v-card-actions>
</v-card>
</template>
<script>
import copy from "copy-to-clipboard";
export default {
props: {
name: String,
desc: String,
id: String,
author: String,
link: String,
text: String,
command: String
},
data() {
return {
snackbar: false
};
},
computed: {
showCommand() {
return this.text && this.command;
}
},
methods: {
repoLink(repo) {
if (repo) {
return /^https?:/.test(repo) ? repo : `https://github.com/${repo}`;
}
return null;
},
copyCommand() {
copy(this.command, {
format: "text/plain"
});
this.snackbar = true;
}
}
};
</script>
<style scoped>
.repo-link {
text-decoration: none !important;
display: inline-block;
}
.repo-link:hover i {
color: #ea5252;
}
</style>

View File

@ -0,0 +1,70 @@
<template>
<div class="store">
<v-app>
<v-main>
<v-card>
<v-toolbar dense flat>
<v-tabs v-model="tab" centered>
<v-tab v-for="(name, index) in tabs" :key="index">{{
name
}}</v-tab>
</v-tabs>
</v-toolbar>
<v-tabs-items class="sub-item" v-model="tab">
<v-tab-item>
<Adapter></Adapter>
</v-tab-item>
<v-tab-item>
<Plugin></Plugin>
</v-tab-item>
<v-tab-item>
<Bot></Bot>
</v-tab-item>
</v-tabs-items>
</v-card>
</v-main>
</v-app>
</div>
</template>
<script>
import Adapter from "./Adapter.vue";
import Plugin from "./Plugin.vue";
import Bot from "./Bot.vue";
export default {
name: "Store",
components: {
Adapter,
Plugin,
Bot
},
data() {
return {
tab: 1,
tabs: {
0: "协议",
1: "插件",
2: "机器人"
}
};
},
computed: {},
methods: {}
};
</script>
<style>
.v-application--wrap {
min-height: 0 !important;
}
</style>
<style scoped>
.store {
margin: 0 -20px;
}
.sub-item {
padding: 0 10px;
}
</style>

View File

@ -82,7 +82,7 @@ module.exports = context => ({
{ text: "指南", link: "/guide/" },
{ text: "进阶", link: "/advanced/" },
{ text: "API", link: "/api/" },
{ text: "插件广场", link: "/plugin-store" },
{ text: "商店", link: "/store" },
{ text: "更新日志", link: "/changelog" }
],
sidebarDepth: 2,

View File

@ -0,0 +1 @@
[]

View File

@ -1,4 +1,5 @@
[
"2.0.0a11",
"2.0.0a10",
"2.0.0a8.post2",
"2.0.0a7"

View File

@ -2,6 +2,168 @@
## 它如何工作?
<!-- TODO: how to work -->
如同[概览](../guide/README.md)所言:
~~未填坑~~
> NoneBot2 是一个可扩展的 Python 异步机器人框架,它会对机器人收到的事件进行解析和处理,并以插件化的形式,按优先级分发给事件所对应的事件响应器,来完成具体的功能。
`Nonebot2` 是一个可以对机器人上报的事件进行处理并完成具体功能的机器人框架,在这里,我们将简要讲述它的工作内容。
**便捷起见,以下内容对 `Nonebot2` 会被称为 `nonebot`,与 `Nonebot2` 交互的机器人实现会被称为 `协议端`**
在实际应用中,`nonebot` 会充当一个高性能,轻量级的 Python 微服务框架。协议端可以通过 `http`, `websocket` 等方式与之通信,这个通信往往是双向的:一方面,协议端可以上报数据给 `nonebot``nonebot` 会处理数据并返回响应给协议端;另一方面,`nonebot` 可以主动推送数据给协议端。而 `nonebot` 便是围绕上述的双向通信进行工作的。
在开始工作之前,`nonebot` 会依照**配置文件或初始化配置**启动,并会注册**协议适配器** `adapter`,之后便会加载**插件** 随后,倘若一个协议端与 `nonebot` 进行了连接,`nonebot` 的后端驱动 `driver` 就会将 `adapter` 实例化为 `bot``nonebot` 便会利用 `bot` 开始工作,它的工作内容分为两个方面:
1. **事件处理**`bot` 会将协议端上报的数据转化为 `事件`(`Event`),之后 `nonebot` 会根据一套既定流程来处理 `事件`
2. **调用 `API`**, 在**事件处理**的过程中,`nonebot` 可以通过 `bot` 调用协议端指定的 `API` 来获取更多数据,或者反馈响应给协议端; `nonebot` 也可以通过调用 `API` 向协议端主动请求数据或者主动推送数据。
在**指南**模块, 我们已经叙述了[如何配置 nonebot](../guide/basic-configuration.md), [如何注册协议适配器](../guide/getting-started.md)[如何加载插件](../guide/loading-a-plugin.md), 在这里便不再赘述。
下面,我们将对**事件处理** **调用 API**进行说明。
## 事件处理
我们可以先看事件处理的流程图:
![handle-event](../guide/images/Handle-Event.png)
在流程图里,我们可以看到,`nonebot` 会有三个阶段来处理事件:
1. **driver 处理上报数据**
2. **adapter 处理原始数据**
3. **nonebot 处理 Event**
我们将顺序说明这三个阶段。其中,会将第三个阶段拆分成**概念解释****处理 Event****特殊异常处理**三个部分来说明。
### driver 处理上报数据
1. 协议端会通过 `websocket` 或者 `http` 等方式与 `nonebot` 的后端驱动 `driver` 连接,`driver` 会根据之前注册的 `adapter` 和配置文件的内容来进行鉴权,从而获得这个连接的唯一识别 id `self-id`,随后 `adapter` 就会利用 `self-id` 实例化为 `bot` 对象。
::: tip
需要注意的是,如果协议端通过 `websocket``nonebot` 连接,这个步骤只会在建立连接时进行;通过 `http` 方式连接时,会在协议端每次上报数据时都进行这个步骤。
:::
::: warning
连接之前必须要注册 `adapter`
:::
::: warning
`self-id` 是帐号的唯一识别 ID这意味着不能出现相同的 `self-id`
:::
2. `driver` 会将接收到的数据转交给 `bot` 对象进一步处理。
### adapter 处理原始数据
1. `bot` 会利用事先定义好的 `Event Model` 对上报的数据进行分析处理,将数据转化为 `nonebot` 可以处理的 `Event` 对象。
::: tip
`adapter` 在转换数据格式的同时可以进行一系列的特殊操作,例如 `CQHTTP` 会对 `reply` 信息进行提取。
:::
2. `Event` 会传入 `nonebot` 做进一步处理。
### nonebot 处理 Event
在讲述这个阶段之前,我们需要先对几个概念进行解释。
#### 概念解释
1. **hook**,或者说**钩子函数**,它们可以在 `nonebot` 处理 `Event` 的不同时刻进行拦截,修改或者扩展,在 `nonebot` 中,钩子函数分为 `事件预处理hook``运行预处理hook``运行后处理hook``事件后处理hook`
::: tip
关于`hook`的更多信息,可以查阅[这里](./runtime-hook.md)
:::
2. **Matcher**与**matcher**,在**指南**中,我们讲述了[如何注册事件响应器](../guide/creating-a-matcher),这里的事件响应器或者说 `Matcher` 并不是一个具体的实例 `instance`,而是一个具有特定属性的类 `class`。只有当 `Matcher` **响应事件**时,才会实例化为具体的 `instance`,也就是 `matcher``matcher` 可以认为是 `nonebot` 处理 `Event` 的基本单位,运行 `matcher``nonebot`工作的主要内容。
3. **handler**,或者说**事件处理函数**, 它们可以认为是 `nonebot` 处理 `Event` 的最小单位。在不考虑 `hook` 的情况下,**运行 matcher 就是顺序运行 matcher.handlers**,这句话换种表达方式就是,`handler` 只有添加到 `matcher.handlers` 时,才可以参与到 `nonebot` 的工作中来。
::: tip
如何让 `handler` 添加到 `matcher.handlers`
一方面,我们可以参照[这里](../guide/creating-a-handler)利用装饰器来添加;另一方面,我们在用 `on()` 或者 `on_*()` 注册事件响应器时,可以添加 `handlers=[handler1, handler2, ...]` 这样的关键词参数来添加。
:::
#### 处理 Event
1. **执行事件预处理 hook** `nonebot` 接收到 `Event` 后,会传入到 `事件预处理hook` 中进行处理。
::: warning
需要注意的是,执行多个 `事件预处理hook` 时并无顺序可言,它们是**并行运行**的。这个原则同样适用于其他的 `hook`
:::
2. **按优先级升序选出同一优先级的 Matcher**`nonebot` 提供了一个全局字典 `matchers`,这个字典的 `key` 是优先级 `priority``value` 是一个 `list`,里面存放着同一优先级的 `Matcher`。在注册 `Matcher` 时,它和优先级 `priority` 会添加到里面。
在执行 `事件预处理hook` 后,`nonebot` 会对 `matchers``key` 升序排序并选择出当前最小优先级的 `Matcher`
3. **根据 Matcher 定义的 Rule, Permission 判断是否运行**,在选出 `Matcher` 后,`nonebot` 会将 `bot``Event` 传入到 `Matcher.check_rule``Matcher.check_perm` 两个函数中,两个函数分别对 Matcher 定义的 Rule, Permission 进行 check当 check 通过后,这个 `Matcher` 就会响应事件。但是当同一个优先级的所有 `Matcher` 均没有响应时,`nonebot` 会返回到上一个步骤,选择出下一优先级的 `Matcher`
4. **实例化 matcher 并执行运行预处理 hook**,当 `Matcher` 响应事件后,它便会实例化为 `matcher`,并执行 `运行预处理hook`
5. **顺序运行 matcher 的所有 handlers**`运行预处理hook` 执行完毕后,便会运行 `matcher`,也就是**顺序运行**它的 `handlers`
::: tip
`matcher` 运行 `handlers` 的顺序是: 先运行该 `matcher` 的类 `Matcher` 注册时添加的 `handlers`(如果有的话),再按照装饰器装饰顺序运行装饰的 `handlers`
:::
6. **执行运行后处理 hook**`matcher``handlers` 运行完毕后,会执行 `运行后处理hook`
7. **判断是否停止事件传播**`nonebot` 会根据当前优先级所有 `matcher``block` 参数或者 `StopPropagation` 异常判断是否停止传播 `Event`,如果事件没有停止传播,`nonebot` 便会返回到第 2 步, 选择出下一优先级的 `Matcher`
8. **执行事件后处理 hook**,在 `Event` 停止传播或执行完所有响应的 `Matcher` 后,`nonebot` 会执行 `事件后处理hook`
`事件后处理hook` 执行完毕后,当前`Event`的处理周期就顺利结束了。
#### 特殊异常处理
在这个阶段,`nonebot` 规定了几个特殊的异常,当 `nonebot` 捕获到它们时,会用特定的行为来处理它们。
1. **IgnoredException**
这个异常可以在 `事件预处理hook``运行预处理hook` 抛出。
`事件预处理hook` 抛出它时,`nonebot` 会忽略当前的 `Event`,不进行处理。
`运行预处理hook` 抛出它时,`nonebot` 会忽略当前的 `matcher`,结束当前 `matcher` 的运行。
::: warning
`hook` 需要抛出这个异常时,要写明原因。
:::
2. **PausedException**
这个异常可以在 `handler` 中由 `Matcher.pause` 抛出。
`nonebot` 捕获到它时,会停止运行当前 `handler` 并结束当前 `matcher` 的运行,并将后续的 `handler` 交给一个临时 `Matcher` 来响应当前交互用户的下一个消息事件,当临时 `Matcher` 响应时,临时 `Matcher` 会运行后续的 handlers。
3. **RejectedException**
这个异常可以在 `handler` 中由 `Matcher.reject` 抛出。
`nonebot` 捕获到它时,会停止运行当前 `handler` 并结束当前 `matcher` 的运行,并将**当前 handler 和后续 `handler`**交给一个临时 `Matcher` 来响应当前交互用户的下一个消息事件,当临时 `Matcher` 响应时,临时 `Matcher` 会运行当前 `handler` 和后续的 `handler`
4. **FinishedException**
这个异常可以在 `handler` 中由 `Matcher.finish` 抛出。
`nonebot` 捕获到它时,会停止运行当前 `handler` 并结束当前 `matcher` 的运行。
5. **StopPropagation**
这个异常一般会在执行 `运行后处理hook` 后抛出。
`nonebot` 捕获到它时, 会停止传播当前 `Event`,不再寻找下一优先级的 `Matcher`,直接执行 `事件后处理hook`
## 调用 API
`nonebot` 可以通过 `bot` 来调用 APIAPI 可以向协议端发送数据,也可以向协议端请求更多的数据。
::: tip
不同 `adapter` 规定了不同的 API对应的 API 列表请参照协议规范。
:::
一般来说,我们可以用 `bot.*` 来调用 `API`(\*是 `API``action` 或者 `endpoint`)。
对于发送消息而言,一方面可以调用既有的 API另一方面 `nonebot` 实现了两个便捷方法,`bot.send(event, message, **kwargs)` 方法和可以在 `handler` 中使用的 `Matcher.send(message, **kwargs)` 方法,来向事件主体发送消息。

View File

@ -179,7 +179,7 @@ await bot.send_msg(message="hello world")
## _class_ `MessageSegment`
基类:`abc.ABC`
基类:`abc.ABC`, `Mapping`
消息段基类
@ -291,7 +291,7 @@ Event 基类。提供获取关键信息的方法,其余信息可直接获取
* `Literal["message", "notice", "request", "meta_event"]`
* `str`
* 其他自定义 `str`

View File

@ -307,7 +307,7 @@ CQHTTP 协议 Bot 适配。继承属性参考 [BaseBot](./#class-basebot) 。
## _class_ `MessageSegment`
基类:[`nonebot.adapters._base.MessageSegment`](README.md#nonebot.adapters._base.MessageSegment)
基类:`abc.ABC`, `Mapping`
CQHTTP 协议 MessageSegment 适配。具体方法参考协议消息段类型或源码。

View File

@ -199,7 +199,7 @@ sidebarDepth: 0
## _class_ `MessageSegment`
基类:[`nonebot.adapters._base.MessageSegment`](README.md#nonebot.adapters._base.MessageSegment)
基类:`abc.ABC`, `Mapping`
钉钉 协议 MessageSegment 适配。具体方法参考协议消息段类型或源码。

View File

@ -722,9 +722,9 @@ mirai-api-http 正向 Websocket 协议 Bot 适配。
## _class_ `MessageSegment`
基类:[`nonebot.adapters._base.MessageSegment`](README.md#nonebot.adapters._base.MessageSegment)
基类:`abc.ABC`, `Mapping`
CQHTTP 协议 MessageSegment 适配。具体方法参考 [mirai-api-http 消息类型](https://github.com/project-mirai/mirai-api-http/blob/master/docs/MessageType.md)
Mirai-API-HTTP 协议 MessageSegment 适配。具体方法参考 [mirai-api-http 消息类型](https://github.com/project-mirai/mirai-api-http/blob/master/docs/MessageType.md)
### `as_dict()`

View File

@ -197,6 +197,36 @@ sidebarDepth: 0
### `_default_type_updater`
* **类型**
`Optional[T_ArgsParser]`
* **说明**
事件响应器类型更新函数
### `_default_permission_updater`
* **类型**
`Optional[T_ArgsParser]`
* **说明**
事件响应器权限更新函数
### `__init__()`
实例化 Matcher 以便运行
@ -341,6 +371,38 @@ sidebarDepth: 0
### _classmethod_ `type_updater(func)`
* **说明**
装饰一个函数来更改当前事件响应器的默认响应事件类型更新函数
* **参数**
* `func: T_TypeUpdater`: 响应事件类型更新函数
### _classmethod_ `permission_updater(func)`
* **说明**
装饰一个函数来更改当前事件响应器的默认会话权限更新函数
* **参数**
* `func: T_PermissionUpdater`: 会话权限更新函数
### _classmethod_ `handle()`
@ -468,3 +530,12 @@ sidebarDepth: 0
* `**kwargs`: 其他传递给 `bot.send` 的参数,请参考对应 adapter 的 bot 对象 api
### `stop_propagation()`
* **说明**
阻止事件传播

View File

@ -55,6 +55,12 @@ sidebarDepth: 0
* `load_all_plugins` => `nonebot.plugin.load_all_plugins`
* `load_from_json` => `nonebot.plugin.load_from_json`
* `load_from_toml` => `nonebot.plugin.load_from_toml`
* `load_builtin_plugins` => `nonebot.plugin.load_builtin_plugins`

View File

@ -1334,6 +1334,59 @@ def something_else():
## `load_from_json(file_path, encoding='utf-8')`
* **说明**
导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件,以 `_` 开头的插件不会被导入!
* **参数**
* `file_path: str`: 指定 json 文件路径
* `encoding: str`: 指定 json 文件编码
* **返回**
* `Set[Plugin]`
## `load_from_toml(file_path, encoding='utf-8')`
* **说明**
导入指定 toml 文件 `[nonebot.plugins]` 中的 `plugins` 以及 `plugin_dirs` 下多个插件,
`_` 开头的插件不会被导入!
* **参数**
* `file_path: str`: 指定 toml 文件路径
* `encoding: str`: 指定 toml 文件编码
* **返回**
* `Set[Plugin]`
## `load_builtin_plugins(name='echo')`

View File

@ -212,3 +212,35 @@ sidebarDepth: 0
* **说明**
ArgsParser 即消息参数解析函数,在 Matcher.got 获取参数时被运行。
## `T_TypeUpdater`
* **类型**
`Callable[[Bot, Event, T_State, str], Awaitable[str]]`
* **说明**
TypeUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新响应的事件类型。默认会更新为 `message`
## `T_PermissionUpdater`
* **类型**
`Callable[[Bot, Event, T_State, Permission], Awaitable[Permission]]`
* **说明**
PermissionUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新会话对象权限。默认会更新为当前事件的触发对象。

View File

@ -101,6 +101,8 @@ nb plugin install nonebot_plugin_test
NoneBot 内置的事件响应器中,所有 `message` 类的事件响应器默认会阻断事件传递,其他则不会。
在部分情况中,可以使用 `matcher.stop_propagation()` 方法动态阻止事件传播,该方法需要 `handler` 在参数中获取 `matcher` 实例后调用方法。
## 自定义 rule
rule 的出现使得 nonebot 对事件的响应可以非常自由nonebot 内置了一些规则:

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB