Skip to content

SkyChat Plugin Development Documentation

Benjamin Raymond edited this page Nov 26, 2023 · 2 revisions

SkyChat Plugin Development Documentation

Overview

SkyChat, a room-based messaging application, supports extension through custom plugins. Plugins are categorized into two types: RoomPlugin and GlobalPlugin. This document guides developers on writing plugins for SkyChat using TypeScript.

Plugin Structure

Base Class: Plugin

  • Storage Path: Defines the base path for persistent storage.
  • Rules: Specifies command rules like cooldowns, parameter patterns, etc.
  • Command Execution: Implements command checking and execution logic.
  • Storage Management: Methods for loading and syncing plugin data.
  • User Data Handling: Functions to load and save user-specific data.

Room-Specific Plugin: RoomPlugin

  • Inherits from Plugin.
  • Room Instance: Attached to a specific room.
  • Storage Path: Overrides to provide room-specific storage path.
  • Hooks: Methods for intercepting room events (e.g., message broadcast, connection events).

Global Plugin: GlobalPlugin

  • Inherits from Plugin.
  • Global Availability: Instantiated once at the room manager level.
  • Storage Path: Overrides to provide a global storage path.
  • Hooks: Methods for handling application-wide events (e.g., new messages, connection events).

Writing a Custom Plugin

  1. Choose the Plugin Type:

    • RoomPlugin: For room-specific functionality.
    • GlobalPlugin: For application-wide functionality.
  2. Create the Plugin Class:

    • Extend RoomPlugin or GlobalPlugin.
    • Implement required methods and properties.
  3. Implement Command Logic:

    • Define commandName and commandAliases.
    • Set up the rules property to define command parameters and constraints.
    • Implement the run method with the plugin's core logic.
  4. Handle Storage and User Data:

    • Use syncStorage and loadStorage for persistent data.
    • Manage user-specific data with getUserData and saveUserData.
  5. Implement Hooks (if needed):

    • Override methods like onNewMessageHook, onConnectionClosed, etc.
  6. Testing and Debugging:

    • Test the plugin in a development environment.
    • Ensure compatibility with SkyChat's core features.

Best Practices

Error Handling

Effective error handling is crucial for maintaining the stability and reliability of your plugin. Here's an example to illustrate robust error handling in a plugin method:

async run(alias: string, param: string, connection: Connection): Promise<void> {
    const id = parseInt(param.split(' ')[0]);

    // Find message
    const message = await this.room.getMessageById(id);

    if (!message) {
        throw new Error('Message not found');
    }

    // Additional plugin logic here...
}

In this example, the run method begins by parsing and validating the input parameter. If the expected message is not found, it throws an error with a descriptive message. This approach ensures that the plugin fails gracefully and provides clear feedback to the user or the system about what went wrong.