Add config UI and persistence to builtin plugin

Introduces a configuration UI schema and persistent config storage for the napcat-plugin-builtin. The plugin now loads and saves its configuration, supports dynamic prefix and reply toggling, and updates dependencies to napcat-types v0.0.6.
This commit is contained in:
手瓜一十雪
2026-01-28 14:13:48 +08:00
parent 89bac8f6e3
commit b7f54fafa2
10 changed files with 102 additions and 64 deletions

View File

@@ -3,8 +3,31 @@ import { EventType } from 'napcat-types/napcat-onebot/event/index';
import type { PluginModule } from 'napcat-types/napcat-onebot/network/plugin-manger';
import type { OB11Message, OB11PostSendMsg } from 'napcat-types/napcat-onebot/types/index';
import fs from 'fs';
import type { PluginConfigSchema, OB11PluginMangerAdapter } from 'napcat-types/napcat-onebot/network/plugin-manger';
let actions: ActionMap | undefined = undefined;
let startTime: number = Date.now();
let platformInstance: OB11PluginMangerAdapter | undefined = undefined;
interface BuiltinPluginConfig {
prefix: string;
enableReply: boolean;
description: string;
theme?: string;
features?: string[];
[key: string]: unknown;
}
let currentConfig: BuiltinPluginConfig = {
prefix: '#napcat',
enableReply: true,
description: '这是一个内置插件的配置示例'
};
const PLUGIN_NAME = 'napcat-plugin-builtin';
export let plugin_config_ui: PluginConfigSchema = [];
/**
* 插件初始化
@@ -12,6 +35,61 @@ let startTime: number = Date.now();
const plugin_init: PluginModule['plugin_init'] = async (_core, _obContext, _actions, _instance) => {
console.log('[Plugin: builtin] NapCat 内置插件已初始化');
actions = _actions;
platformInstance = _instance;
if (_instance.NapCatConfig) {
const NapCatConfig = _instance.NapCatConfig;
plugin_config_ui = NapCatConfig.combine(
NapCatConfig.html('<div style="padding: 10px; background: rgba(0,0,0,0.05); border-radius: 8px;"><h3>👋 Welcome to NapCat Builtin Plugin</h3><p>This is a demonstration of the plugin configuration interface.</p></div>'),
NapCatConfig.text('prefix', 'Command Prefix', '#napcat', 'The prefix to trigger the version info command'),
NapCatConfig.boolean('enableReply', 'Enable Reply', true, 'Switch to enable or disable the reply functionality'),
NapCatConfig.select('theme', 'Theme Selection', [
{ label: 'Light Mode', value: 'light' },
{ label: 'Dark Mode', value: 'dark' },
{ label: 'Auto', value: 'auto' }
], 'light', 'Select a theme for the response (Demo purpose only)'),
NapCatConfig.multiSelect('features', 'Enabled Features', [
{ label: 'Version Info', value: 'version' },
{ label: 'Status Report', value: 'status' },
{ label: 'Debug Log', value: 'debug' }
], ['version'], 'Select features to enable'),
NapCatConfig.text('description', 'Description', '这是一个内置插件的配置示例', 'A multi-line text area for notes')
);
}
// Try to load config
try {
if (platformInstance && platformInstance.getPluginConfigPath) {
const configPath = platformInstance.getPluginConfigPath(PLUGIN_NAME);
if (fs.existsSync(configPath)) {
const savedConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
Object.assign(currentConfig, savedConfig);
}
}
} catch (e) {
console.warn('[Plugin: builtin] Failed to load config', e);
}
};
export const plugin_get_config = async () => {
return currentConfig;
};
export const plugin_set_config = async (config: BuiltinPluginConfig) => {
currentConfig = config;
if (platformInstance && platformInstance.getPluginConfigPath) {
try {
const configPath = platformInstance.getPluginConfigPath(PLUGIN_NAME);
const configDir = configPath.substring(0, configPath.lastIndexOf(process.platform === 'win32' ? '\\' : '/'));
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
} catch (e) {
console.error('[Plugin: builtin] Failed to save config', e);
throw e;
}
}
};
/**
@@ -19,7 +97,13 @@ const plugin_init: PluginModule['plugin_init'] = async (_core, _obContext, _acti
* 当收到包含 #napcat 的消息时,回复版本信息
*/
const plugin_onmessage: PluginModule['plugin_onmessage'] = async (adapter, _core, _obCtx, event, _actions, instance) => {
if (event.post_type !== EventType.MESSAGE || !event.raw_message.startsWith('#napcat')) {
// Use config logic
const prefix = currentConfig.prefix || '#napcat';
if (currentConfig.enableReply === false) {
return;
}
if (event.post_type !== EventType.MESSAGE || !event.raw_message.startsWith(prefix)) {
return;
}

View File

@@ -6,7 +6,7 @@
"description": "NapCat 内置插件",
"author": "NapNeko",
"dependencies": {
"napcat-types": "0.0.5"
"napcat-types": "0.0.6"
},
"devDependencies": {
"@types/node": "^22.0.1"