15 lines
8.4 KiB
JavaScript
15 lines
8.4 KiB
JavaScript
import{_ as s,c as i,a0 as n,o as t}from"./chunks/framework.p2VkXzrt.js";const c=JSON.parse('{"title":"🛠️ Creating a Command","description":"","frontmatter":{"banner_title":"MoBot - Creating a Command","banner_description":"Learn how to create custom commands for MoBot","head":[["meta",{"name":"twitter:image","content":"/assets/banner-cards/mobot-commands-creating-a-command.png"}],["meta",{"name":"twitter:image:src","content":"https://docs.pixel-services.com/assets/banner-cards/mobot-commands-creating-a-command.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/commands/creating-a-command.md","filePath":"mobot/commands/creating-a-command.md"}'),e={name:"mobot/commands/creating-a-command.md"};function o(h,a,d,l,r,p){return t(),i("div",null,a[0]||(a[0]=[n(`<h1 id="🛠️-creating-a-command" tabindex="-1">🛠️ Creating a Command <a class="header-anchor" href="#🛠️-creating-a-command" aria-label="Permalink to "🛠️ Creating a Command""></a></h1><p>Commands are a core feature of Discord Bots. They allow users to interact with the bot and perform various actions. In MoBot, creating a command is straightforward and flexible, allowing you to define custom behavior for your bot.</p><h2 id="🚀-commands-in-the-mobot-api" tabindex="-1">🚀 Commands in the MoBot API <a class="header-anchor" href="#🚀-commands-in-the-mobot-api" aria-label="Permalink to "🚀 Commands in the MoBot API""></a></h2><p>To make commands easy to use, MoBot provides a extensive API for creating and managing commands. Apart from the <code>SlashCommandHandler</code> interface, the SlashCommand System is build entirely from annotations.</p><div class="warning custom-block"><p class="custom-block-title">NOTE</p><p>You will have to implement the <code>SlashCommandHandler</code> interface if you want to use <code>@SlashCommand</code> and other annotations.</p></div><h2 id="📜-slashcommand-annotation" tabindex="-1">📜 <code>@SlashCommand</code> Annotation <a class="header-anchor" href="#📜-slashcommand-annotation" aria-label="Permalink to "📜 \`@SlashCommand\` Annotation""></a></h2><p>The <code>@SlashCommand</code> annotation is used to define a slash command. Apart from the name and description, you can also specify aliases and required permissions. This annotation is placed on a method that will handle the command when it is invoked. The method should accept a <code>SlashCommandInteractionEvent</code> parameter, which contains information about the command invocation and allows you to respond to the user.</p><table tabindex="0"><thead><tr><th>Option</th><th>Description</th></tr></thead><tbody><tr><td><code>name</code></td><td>The name of the command. This is what users will type to invoke the command.</td></tr><tr><td><code>description</code></td><td>A short description of what the command does.</td></tr><tr><td><code>aliases</code></td><td>An array of alternative names for the command.</td></tr><tr><td><code>permissions</code></td><td>The permission required to use the command.</td></tr></tbody></table><p>Here’s an example of a simple <code>PingCommand</code> using the <code>@SlashCommand</code> annotation:</p><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;"> PingCommand</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> implements</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> SlashCommandHandler</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;">SlashCommand</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span></span>
|
||
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> name</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "ping"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
|
||
<span class="line"><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> description</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> "Ping the bot to check if it's alive"</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> )</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;"> onPingCommand</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(SlashCommandInteractionEvent </span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">event</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
|
||
<span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> event.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">reply</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">"Pong!"</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">).</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">queue</span><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></code></pre></div><h2 id="🛠️-registering-the-command" tabindex="-1">🛠️ Registering the Command <a class="header-anchor" href="#🛠️-registering-the-command" aria-label="Permalink to "🛠️ Registering the Command""></a></h2><p>To register the command, you need to create an instance of your command class and register it using the <code>#registerSlashCommandHandler</code> method in your module's main class.</p><p>Here's an example of how to register the <code>PingCommand</code> in your module:</p><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;"> onEnable</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
|
||
<span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> registerSlashCommandHandler</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">new</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> PingCommand</span><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></code></pre></div>`,14)]))}const k=s(e,[["render",o]]);export{c as __pageData,k as default};
|