Skip to content

Plugin Creation

AstroX10 edited this page Dec 28, 2024 · 1 revision

Creating Custom Commands for Xstro

1. Understanding the Command Structure

Commands are registered using the bot function. It takes two arguments:

  1. Command Configuration: An object that defines the command's behavior and properties.
  2. Callback Function: An asynchronous function that contains the logic to execute the command.

2. Example: Registering a Command

Below is an example of a simple ping command:

import { bot } from '#lib'; // Global import for the bot framework

bot(
  {
    pattern: 'ping', // Command trigger word
    public: true, // true = accessible by everyone, false = restricted to owners/sudo
    isGroup: true, // true = command only works in groups
    desc: 'Check performance response', // Command description for help menus
    type: 'system' // Category of the command (e.g., 'system', 'fun', 'utility')
  },
  async (message, match) => {
    /**
     * Command functionality
     */
    const start = Date.now();
    await message.send('Pong!'); // Sends a reply
    const end = Date.now();
    await message.send(`Response Time: ${end - start}ms`);
  }
);

3. Configuration Properties

Property Type Description
pattern string The command's trigger word (e.g., ping)
public boolean true for public commands, false for owner/sudo-only commands
isGroup boolean true if the command works only in groups, otherwise it won't execute
desc string A brief description of the command for help menus or debugging
type string Command category (e.g., system, fun, utility)

4. Callback Function

The second parameter is an asynchronous function that:

  1. Receives Parameters:

    • message: The messaging instance for handling replies or events
    • match: Arguments or inputs matched by the command
  2. Contains Logic: Implement your command's functionality inside this function

Note: The Xstro framework includes built-in error handling, so you don't need to use try-catch blocks.

5. Writing Your Command Logic

Commands can perform various tasks, such as replying to messages, accessing inputs, or interacting with APIs. For instance:

Send a Reply:

await message.send('Hello, World!');

Access Command Arguments:

const input = match || 'default'; // Uses `match` to get user input

Perform Asynchronous Actions:

const data = await fetch('https://api.example.com/data');
const result = await data.json();
await message.send(`Data: ${result.value}`);

6. Example Commands

Echo Command

bot(
  {
    pattern: 'echo',
    public: true,
    isGroup: false, // This command works only in private chats
    desc: 'Echoes back your input',
    type: 'utility'
  },
  async (message, match) => {
    await message.send(match || 'You didn\'t provide any text!');
  }
);

Group-Only Command

bot(
  {
    pattern: 'greet',
    public: true,
    isGroup: true, // This command works only in groups
    desc: 'Send a group greeting',
    type: 'fun'
  },
  async (message) => {
    await message.send('Hello, everyone!');
  }
);