Files
PixelDocs/assets/mobot_commands_creating-a-command.md.DrPP2R0G.js
T
2025-04-21 14:23:15 +00:00

15 lines
8.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 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 &quot;🛠️ Creating a Command&quot;"></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 &quot;🚀 Commands in the MoBot API&quot;"></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 &quot;📜 \`@SlashCommand\` Annotation&quot;"></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>Heres 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;"> &quot;ping&quot;</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;"> &quot;Ping the bot to check if it&#39;s alive&quot;</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;">&quot;Pong!&quot;</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 &quot;🛠️ Registering the Command&quot;"></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&#39;s main class.</p><p>Here&#39;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};