This commit is contained in:
sieadev
2025-04-21 14:23:15 +00:00
parent 06303b9fc5
commit 6d8f5b7fac
61 changed files with 632 additions and 21 deletions
@@ -0,0 +1,21 @@
import{_ as s,c as a,a0 as i,o as t}from"./chunks/framework.p2VkXzrt.js";const k=JSON.parse('{"title":"📦 Module Lifecycle","description":"","frontmatter":{"banner_title":"MoBot - Module Lifecycle","banner_description":"Learn about the lifecycle of a MoBot module","head":[["meta",{"name":"twitter:image","content":"/assets/banner-cards/mobot-creating-a-module-module-lifecycle.png"}],["meta",{"name":"twitter:image:src","content":"https://docs.pixel-services.com/assets/banner-cards/mobot-creating-a-module-module-lifecycle.png"}],["meta",{"name":"twitter:card","content":"summary_large_image"}],["meta",{"name":"twitter:image:height","content":"1280"}],["meta",{"name":"twitter:image:width","content":"669"}],["meta",{"name":"twitter:description","content":""}]]},"headers":[],"relativePath":"mobot/creating-a-module/module-lifecycle.md","filePath":"mobot/creating-a-module/module-lifecycle.md"}'),l={name:"mobot/creating-a-module/module-lifecycle.md"};function n(o,e,h,d,r,p){return t(),a("div",null,e[0]||(e[0]=[i(`<h1 id="📦-module-lifecycle" tabindex="-1">📦 Module Lifecycle <a class="header-anchor" href="#📦-module-lifecycle" aria-label="Permalink to &quot;📦 Module Lifecycle&quot;"></a></h1><p>MoBot modules follow a specific lifecycle to ensure proper initialization, operation, and cleanup. Understanding this lifecycle is crucial for creating reliable and efficient modules.</p><h2 id="🔄-lifecycle-phases" tabindex="-1">🔄 Lifecycle Phases <a class="header-anchor" href="#🔄-lifecycle-phases" aria-label="Permalink to &quot;🔄 Lifecycle Phases&quot;"></a></h2><p>The lifecycle of a MoBot module consists of several phases, each with its own purpose and responsibilities. Heres a breakdown of each phase:</p><h3 id="_1-initialization" tabindex="-1">1. Initialization <a class="header-anchor" href="#_1-initialization" aria-label="Permalink to &quot;1. Initialization&quot;"></a></h3><p>When MoBot starts, it scans the modules directory for JAR files. Each module is loaded, and its MbModule class is instantiated. During this phase, the module&#39;s dependencies are resolved, and its configuration is loaded.</p><h3 id="_2-pre-enable" tabindex="-1">2. Pre-Enable <a class="header-anchor" href="#_2-pre-enable" aria-label="Permalink to &quot;2. Pre-Enable&quot;"></a></h3><p>Before the Discord Bot is fully initialized, the <code>preEnable()</code> method of the module&#39;s main class is called. This is where you can perform actions that involve the <code>BotBuilder</code>. This includes stuff like setting the bots status or other tasks that can not be performed after the bot is fully initialized.</p><h3 id="_3-enable" tabindex="-1">3. Enable <a class="header-anchor" href="#_3-enable" aria-label="Permalink to &quot;3. Enable&quot;"></a></h3><p>After initialization, the <code>onEnable()</code> method of the module&#39;s main class is called. This is where you should set up your module&#39;s main functionality, such as registering commands, listeners, or tasks.</p><h3 id="_4-disable" tabindex="-1">4. Disable <a class="header-anchor" href="#_4-disable" aria-label="Permalink to &quot;4. Disable&quot;"></a></h3><p>When MoBot shuts down or the module is unloaded, the <code>onDisable()</code> method is called. Use this phase to clean up resources, save data, and gracefully stop any ongoing tasks.</p><h3 id="_5-post-disable" tabindex="-1">5. Post-Disable <a class="header-anchor" href="#_5-post-disable" aria-label="Permalink to &quot;5. Post-Disable&quot;"></a></h3><p>After the Discord Bot is fully disabled, the <code>postDisable()</code> method of the module&#39;s main class is called. You can use this phase to perform any final cleanup tasks that require the bot to be fully disabled.</p><div class="warning custom-block"><p class="custom-block-title">NOTE</p><p>All of these methods are optional, and you can choose to implement only the ones you need.</p></div><h2 id="usage-example" tabindex="-1">Usage Example <a class="header-anchor" href="#usage-example" aria-label="Permalink to &quot;Usage Example&quot;"></a></h2><div class="language-java vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">java</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> class</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyModule</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> extends</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MbModule</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">Override</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> preEnable</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Perform actions before the bot is fully initialized</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">Override</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> onEnable</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Register commands, listeners, etc.</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> </span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">Override</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> onDisable</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Save data, stop tasks, etc.</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> @</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">Override</span></span>
<span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> void</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> postDisable</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span class="line"><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> // Final cleanup tasks</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><h2 id="🛠️-cheat-sheet" tabindex="-1">🛠️ Cheat Sheet <a class="header-anchor" href="#🛠️-cheat-sheet" aria-label="Permalink to &quot;🛠️ Cheat Sheet&quot;"></a></h2><table tabindex="0"><thead><tr><th>Method</th><th>Description</th></tr></thead><tbody><tr><td><code>preEnable()</code></td><td>Called before the bot is fully initialized. Use this to set up the bot.</td></tr><tr><td><code>onEnable()</code></td><td>Called when the module is enabled. Use this to register commands, listeners, etc.</td></tr><tr><td><code>onDisable()</code></td><td>Called when the module is disabled. Use this to save data, clean up resources etc.</td></tr><tr><td><code>postDisable()</code></td><td>Called after the bot is fully disabled. Use this for final cleanup tasks.</td></tr></tbody></table>`,19)]))}const u=s(l,[["render",n]]);export{k as __pageData,u as default};