app/assets/dev_lyplugin.html-DhSssRYw.js

36 lines
12 KiB
JavaScript
Raw Normal View History

import{_ as n,o as s,c as a,e}from"./app-275NBlFz.js";const t={},p=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>一个<code>.py</code>文件或一个包含<code>__init__.py</code>的文件夹即可被识别为插件</p><p>首先创建一个文件夹,例如<code>watchdog_plugin</code>,并在其中创建一个<code>__init__.py</code>文件,即可创建一个插件</p><p><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 punctuation">,</span> PluginType
<span class="token keyword">from</span> <span class="token punctuation">.</span>watch_dog <span class="token keyword">import</span> <span class="token operator">*</span> <span class="token comment"># </span>
<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 punctuation">,</span> <span class="token comment"># </span>
<span class="token builtin">type</span><span class="token operator">=</span>PluginType<span class="token punctuation">.</span>SERVICE <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 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 <code>watch_dog.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">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><h4 id="1" tabindex="-1"><a class="header-anchor" href="#1"><span>1</span></a></h4><ul><li><code>liteyuki.plugins</code><code>watchdog_plugin</code></li></ul><h4 id="2" tabindex="-1"><a class="header-anchor" href="#2"><span>2</span></a></h4><ul><li>使</li><li><code>main.py</code></li></ul><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>dev<span class="token punctuation">.</span>plugin <span class="token keyword">import</span> run_plugins
run_plugins<span class="token punctuation">(</span><span class="token string">&quot;watchdog_plugin&quot;</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></div><p><code>python main.py</code></p><p>src/nonebot_plugins<code>test.py</code>NoneBot</p>`,19),o=[p];function i(c,l){return s(),a("div",null,o)}const d=n(t,[["render",i],["__file","dev_lyplugin.html.vue"]]),r=JSON.parse('{"path":"/dev/dev_lyplugin.html","title":"","lang":"zh-CN","frontmatter":{"title":"","icon":"laptop-code","order":3,"category":"","description":" .py__init__.py watchdog_plugin__init__.py __init__.py ...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/dev_lyplugin.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/dev_lyplugin.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot "}],["meta",{"property":"og:title","content":""}],["meta",{"property":"og:description","content":" .py__init__.py watchdog_plugin__init__.py __init__.py ..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-18T15:52:26.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-18T15:52:26.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-18T15:52:26.000Z\\",\\"author\\":[]}"]]},"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":1723411596000,"updatedTime":1723996346000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":4}]},"readingTime":{"minutes":1.68,"words":504},"filePathRelative":"dev/dev_lyplugin.md","localizedDate":"2024811","autoDesc":true}');export{d as comp,r as data};