app/assets/dev_lyplugin.html-Bn0sCva8.js

31 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{_ as n,o as s,c as a,e}from"./app-Bq7oQTEs.js";const t={},o=e(`<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2><p>轻雪插件是轻雪内置的一部分功能,运行在主进程中,可以很高程度地扩展轻雪的功能</p><h2 id="开始" tabindex="-1"><a class="header-anchor" href="#开始"><span>开始</span></a></h2><h3 id="创建插件" tabindex="-1"><a class="header-anchor" href="#创建插件"><span>创建插件</span></a></h3><p>在标准项目中位于liteyuki/plugins和src/liteyuki_plugins下的Python modules均会被当作插件加载你可自行添加配置文件以指定插件的加载路径 一个<code>.py</code>文件或一个包含<code>__init__.py</code>的文件夹即可被识别为插件 创建一个文件夹,例如<code>watchdog_plugin</code>,并在其中创建一个<code>__init__.py</code>文件,即可创建一个插件</p><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">from</span> liteyuki<span class="token punctuation">.</span>plugin <span class="token keyword">import</span> PluginMetadata
<span class="token comment"># 定义插件元数据,推荐填写</span>
__plugin_meta__ <span class="token operator">=</span> PluginMetadata<span class="token punctuation">(</span>
name<span class="token operator">=</span><span class="token string">&quot;NoneDog&quot;</span><span class="token punctuation">,</span> <span class="token comment"># 插件名称</span>
version<span class="token operator">=</span><span class="token string">&quot;1.0.0&quot;</span><span class="token punctuation">,</span> <span class="token comment"># 插件版本</span>
description<span class="token operator">=</span><span class="token string">&quot;A simple plugin for nonebot developer&quot;</span> <span class="token comment"># 插件描述</span>
<span class="token punctuation">)</span>
<span class="token comment"># 你的插件代码</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写逻辑部分" tabindex="-1"><a class="header-anchor" href="#编写逻辑部分"><span>编写逻辑部分</span></a></h3><p>轻雪主进程不涉及聊天部分,因此插件主要是一些后台任务或者与聊天机器人的通信 以下我们会编写一个简单的插件用于开发NoneBot时进行文件系统变更重载</p><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">import</span> os
<span class="token keyword">from</span> liteyuki<span class="token punctuation">.</span>dev <span class="token keyword">import</span> observer <span class="token comment"># 导入文件系统观察器</span>
<span class="token keyword">from</span> liteyuki <span class="token keyword">import</span> get_bot<span class="token punctuation">,</span> logger <span class="token comment"># 导入轻雪Bot和日志</span>
<span class="token keyword">from</span> watchdog<span class="token punctuation">.</span>events <span class="token keyword">import</span> FileSystemEvent <span class="token comment"># 导入文件系统事件</span>
liteyuki <span class="token operator">=</span> get_bot<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 获取唯一的轻雪Bot实例</span>
exclude_extensions <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string">&quot;.pyc&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;.pyo&quot;</span><span class="token punctuation">)</span> <span class="token comment"># 排除的文件扩展名</span>
<span class="token comment"># 用observer的on_file_system_event装饰器监听文件系统事件</span>
<span class="token decorator annotation punctuation">@observer<span class="token punctuation">.</span>on_file_system_event</span><span class="token punctuation">(</span>
directories<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">&quot;src/nonebot_plugins&quot;</span><span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
event_filter<span class="token operator">=</span><span class="token keyword">lambda</span> event<span class="token punctuation">:</span> <span class="token keyword">not</span> event<span class="token punctuation">.</span>src_path<span class="token punctuation">.</span>endswith<span class="token punctuation">(</span>exclude_extensions<span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token punctuation">(</span><span class="token string">&quot;__pycache__&quot;</span> <span class="token keyword">not</span> <span class="token keyword">in</span> event<span class="token punctuation">.</span>src_path<span class="token punctuation">)</span> <span class="token keyword">and</span> os<span class="token punctuation">.</span>path<span class="token punctuation">.</span>isfile<span class="token punctuation">(</span>event<span class="token punctuation">.</span>src_path<span class="token punctuation">)</span>
<span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">restart_nonebot_process</span><span class="token punctuation">(</span>event<span class="token punctuation">:</span> FileSystemEvent<span class="token punctuation">)</span><span class="token punctuation">:</span>
logger<span class="token punctuation">.</span>debug<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&quot;File </span><span class="token interpolation"><span class="token punctuation">{</span>event<span class="token punctuation">.</span>src_path<span class="token punctuation">}</span></span><span class="token string"> changed, reloading nonebot...&quot;</span></span><span class="token punctuation">)</span>
liteyuki<span class="token punctuation">.</span>restart_process<span class="token punctuation">(</span><span class="token string">&quot;nonebot&quot;</span><span class="token punctuation">)</span> <span class="token comment"># 调用重启进程方法</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="加载插件" tabindex="-1"><a class="header-anchor" href="#加载插件"><span>加载插件</span></a></h3><p>在配置文件中的<code>liteyuki.plugins</code>中添加你的插件路径,例如<code>watchdog_plugin</code>重启轻雪即可加载插件。然后我们在src/nonebot_plugins下创建一个文件例如<code>test.py</code>并在其中写入一些代码保存后轻雪会自动重载NoneBot进程</p>`,11),p=[o];function i(l,c){return s(),a("div",null,p)}const r=n(t,[["render",i],["__file","dev_lyplugin.html.vue"]]),d=JSON.parse('{"path":"/en/dev/dev_lyplugin.html","title":"轻雪插件开发","lang":"en-US","frontmatter":{"title":"轻雪插件开发","icon":"laptop-code","order":3,"category":"开发","description":"简介 轻雪插件是轻雪内置的一部分功能,运行在主进程中,可以很高程度地扩展轻雪的功能 开始 创建插件 在标准项目中位于liteyuki/plugins和src/liteyuki_plugins下的Python modules均会被当作插件加载你可自行添加配置文件以指定插件的加载路径 一个.py文件或一个包含__init__.py的文件夹即可被识别为插...","head":[["link",{"rel":"alternate","hreflang":"zh-cn","href":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/dev_lyplugin.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/dev_lyplugin.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"轻雪插件开发"}],["meta",{"property":"og:description","content":"简介 轻雪插件是轻雪内置的一部分功能,运行在主进程中,可以很高程度地扩展轻雪的功能 开始 创建插件 在标准项目中位于liteyuki/plugins和src/liteyuki_plugins下的Python modules均会被当作插件加载你可自行添加配置文件以指定插件的加载路径 一个.py文件或一个包含__init__.py的文件夹即可被识别为插..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:locale:alternate","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2024-08-16T17:27:57.000Z"}],["meta",{"property":"article:author","content":"远野千束"}],["meta",{"property":"article:modified_time","content":"2024-08-16T17:27:57.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"轻雪插件开发\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-16T17:27:57.000Z\\",\\"author\\":[{\\"@type\\":\\"Person\\",\\"name\\":\\"远野千束\\",\\"url\\":\\"https://sfkm.me\\"}]}"]]},"headers":[{"level":2,"title":"简介","slug":"简介","link":"#简介","children":[]},{"level":2,"title":"开始","slug":"开始","link":"#开始","children":[{"level":3,"title":"创建插件","slug":"创建插件","link":"#创建插件","children":[]},{"level":3,"title":"编写逻辑部分","slug":"编写逻辑部分","link":"#编写逻辑部分","children":[]},{"level":3,"title":"加载插件","slug":"加载插件","link":"#加载插件","children":[]}]}],"git":{"createdTime":1723829277000,"updatedTime":1723829277000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":1}]},"readingTime":{"minutes":1.55,"words":465},"filePathRelative":"en/dev/dev_lyplugin.md","localizedDate":"August 16, 2024","autoDesc":true}');export{r as comp,d as data};