el-bot Documentation

website·Indexed 32 days ago

https://docs.bot.elpsy.cn/

A bot framework based on mirai-ts and mirai-api-http that allows users to implement bot functionality through simple configuration or custom plugins. It includes a built-in data system using MongoDB and mongoose, a logging system implemented with winston, and a command system based on commander.js.

Tokens
6.8K
Snippets
46
Records
53
Agent score
61%

What's inside el-bot

  1. Overview of el-bot framework

    el-bot is a configurable QQ bot framework written in TypeScript and running on Node.js, based on mirai-ts. It provides built-in features such as automatic responses, forwarding, command-line interfaces, and RSS. It supports dynamic plugin loading and functional programming patterns. Note: Users must be able to install and start mirai independently, as the project no longer provides installation scripts for mirai.
  2. Overview of the el-bot framework

    el-bot is a configuration-based QQ robot framework implemented based on mirai-ts. It is designed to allow users to implement most of their requirements through simple configuration.
  3. Verify mirai-api-http connectivity for el-bot

    Before using el-bot, you must ensure that the mirai-api-http plugin is successfully loaded and running.

    To verify connectivity, visit the /about endpoint of the API. By default, mirai-api-http uses port 8080, but el-bot-template defaults to port 4859 to avoid common port conflicts.

    # Example check using curl (replace 4859 with your configured port)
    curl http://localhost:4859/about
  4. Understand the el-bot and mirai ecosystem relationship

    el-bot is a cross-platform, configurable robot framework based on Node.js. It relies on the following stack to function:

    1. mirai: The core Kotlin-based library providing the QQ protocol.
    2. mirai-console: The server-side console used to start mirai and load plugins.
    3. mirai-api-http: A mirai-console plugin that exposes an HTTP API, allowing external languages to interact with mirai.
    4. mirai-ts: A TypeScript SDK that wraps mirai-api-http requests and provides type definitions/intellisense for editors like VS Code.
    5. el-bot: The framework built on top of mirai-ts.

    Users must be able to start mirai independently using tools like mirai-console-loader, miraiOK, or mirua.

  5. Develop a custom plugin for el-bot

    el-bot plugins are functions that receive a bot context (ctx). You can use ctx.mirai.api to access mirai-ts APIs without handling login or plugin loading manually.

    Plugin Structure:

    • Create a file (e.g., plugins/custom/test.js) or a folder with an index.js (e.g., plugins/custom/test/index.js).
    • The filename (or folder name) becomes the plugin name.
    • The plugin must export a function (default export).

    Context Object (ctx) Properties:

    • ctx.el.config: Access bot-related configurations from the config file.
    • ctx.mirai: Access the mirai-ts instance for event listening and API calls.
    // Example: plugins/custom/test.js
    const { default: Bot } = require("el-bot");
    
    /**
     * @param {Bot} ctx
     */
    module.exports = async function (ctx) {
      const mirai = ctx.mirai;
    
      // Listen for messages (covers Friend, Group, and Temp messages)
      mirai.on("message", (msg) => {
        console.log("Received message:", msg);
      });
    
      // Send a command via mirai-api-http
      const data = await mirai.api.command.send("help", []);
      console.log("Help info:" + data);
    
      // Listen for specific events (e.g., GroupRecallEvent)
      mirai.on("GroupRecallEvent", ({ operator }) => {
        const text = `${operator.memberName} recalled a message.`;
        mirai.api.sendGroupMessage(text, operator.group.id);
      });
    };
  6. Manage plugin session isolation using InnerMode

    The utils.InnerMode class allows developers to implement an 'internal mode' for plugins. This prevents state leakage between different chat groups or users. Without InnerMode, a plugin might trigger a response in Group B based on a command sent in Group A. By using InnerMode, the plugin tracks the state specifically for the current message context, ensuring that operations (like a search mode) are isolated to the group where they were initiated.
    import { utils } from "el-bot";
    const innerMode = new utils.InnerMode();
    
    export default async function() {
      mirai.on("message", (msg) => {
        // Associate the current message with the inner mode instance
        innerMode.setMsg(msg);
    
        // Example: Enter 'Image Search' mode if the user sends "搜图"
        if (msg.plain === "搜图") {
          innerMode.enter();
          msg.reply("I'm ready!");
        }
    
        // Check if the current context is in internal mode
        if (innerMode.getStatus()) {
          // Perform the specific action (e.g., fakeSearchImage())
          fakeSearchImage();
    
          // Exit internal mode to reset state for this context
          innerMode.exit();
        }
      });
    }
  7. Use the RSS subscription plugin

    The rss plugin allows the bot to track and notify users of updates from RSS feeds using rss-parser.

    • Commands: Send rss in a private chat to see all subscriptions and trigger an immediate fetch. Send rss in a group to see subscriptions specific to that group.
    • Storage: RSS data is stored in el-bot/tmp/rss.json. Deleting this file forces a fresh fetch of all feeds.
    • Recommendation: Use RSSHub for generating feeds for sites that do not natively support RSS.
  8. Install and use el-bot CLI globally

    You can install the el-bot package globally to access its command-line interface for managing the bot and its dependencies like mirai.
    # Install el-bot globally
    npm install -g el-bot
    # or
    yarn global add el-bot
    
    # Install mirai
    el install mirai
    
    # Start mirai
    el start mirai
    
    # Start the bot
    el bot
  9. Install el-bot

    To use el-bot, you must have Java and Node.js installed. It is recommended to use the official mirai-console-loader to start mirai and the mirai-api-http plugin.
    npm install el-bot
    # or
    yarn add el-bot