Koishi Chatbot Framework

4.18.11 · active · verified Sun Apr 19

Koishi is an extensible, cross-platform chatbot framework for Node.js, currently at version 4.18.11. It facilitates the development of intelligent bots that can operate across various chat platforms like Discord and Telegram. The project maintains a rapid release cadence, with frequent patch and minor updates, ensuring timely integration of new features and bug fixes. A core differentiator is its reliance on the Satori Protocol for cross-platform interoperability, standardizing message parsing and event handling. Koishi emphasizes extensibility through a robust plugin system and a declarative command-line interface, enabling developers to build complex bot functionalities with a modular approach. It ships with full TypeScript support, providing strong typing for better developer experience and code maintainability.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a Koishi application, register a custom plugin, define a command, and set up a simple message listener. It highlights the use of `Context` for plugin development, `Schema` for configuration, and `h` for message formatting. A note on adapter configuration is included for a fully functional bot.

import { Context, Schema, h, Koishi } from 'koishi';
// In a real project, you would import and configure an adapter like:
// import '@koishijs/adapter-discord';

// Define a simple plugin
class MyHelloPlugin extends Context.Plugin {
  static schema = Schema.object({
    greeting: Schema.string().default('Hello').description('The greeting message.')
  });

  constructor(ctx: Context, config: { greeting: string }) {
    super(ctx, config);

    // Register a command
    ctx.command('greet <target:string>', 'Greets someone')
      .action(({ session }, target) => {
        if (!target) return 'Please tell me who to greet!';
        // Use h (Hyperscript) for rich message formatting, if supported by adapter
        return h('message', [
          h('text', `${config.greeting}, `),
          h('at', { id: session?.userId || 'unknown' }), // Placeholder for @mention
          h('text', ` ${target}! `)
        ]);
      });

    // Register a simple listener
    ctx.on('message', (session) => {
      if (session.content === 'ping') {
        session.send('pong');
      }
    });

    ctx.logger.info('MyHelloPlugin loaded!');
  }
}

async function main() {
  const app = new Koishi({
    prefix: '.', // Command prefix
    port: 8080, // WebUI port, if enabled
    // The `plugins` object is where you configure adapters and other plugins.
    // For this quickstart, we'll define a dummy adapter for demonstration.
    // In a real app, you'd install and configure e.g., `@koishijs/adapter-discord`.
    plugins: {
      'my-hello-plugin': { // Directly add the plugin to the config
        greeting: 'Hey there'
      }
      // Example of an actual adapter config (uncomment and replace with real token/platform):
      // '@koishijs/adapter-discord': {
      //   token: process.env.DISCORD_TOKEN || 'YOUR_DISCORD_BOT_TOKEN',
      //   intents: 32767 // Or specific intents
      // }
    }
  });

  // Manually register the plugin class (usually done via `app.plugin()` or config)
  app.plugin(MyHelloPlugin);

  try {
    await app.start();
    console.log('Koishi application started. Listening for commands.');
    console.log('Try to run a command like ".greet World" if an adapter is configured and connected.');
  } catch (error) {
    console.error('Failed to start Koishi:', error);
    console.warn('Ensure you have installed and configured at least one adapter (e.g., @koishijs/adapter-discord) ' +
                 'and provided a valid token in the plugins config if you want to connect to a chat platform.');
  }
}

main();

view raw JSON →