mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-04 06:31:13 +00:00
Add plugin logger interface and update builtin plugin
Introduces a PluginLogger interface and injects a plugin-specific logger into the plugin context for consistent logging. Refactors the builtin plugin to use the new logger instead of direct console calls. Updates napcat-types to version 0.0.9 in dependencies and lock files.
This commit is contained in:
parent
0aa0c44634
commit
001fe01ace
@ -54,6 +54,22 @@ export class NapCatConfig {
|
||||
|
||||
export type PluginConfigSchema = PluginConfigItem[];
|
||||
|
||||
/**
|
||||
* 插件日志接口 - 简化的日志 API
|
||||
*/
|
||||
export interface PluginLogger {
|
||||
/** 普通日志 */
|
||||
log (...args: any[]): void;
|
||||
/** 调试日志 */
|
||||
debug (...args: any[]): void;
|
||||
/** 信息日志 */
|
||||
info (...args: any[]): void;
|
||||
/** 警告日志 */
|
||||
warn (...args: any[]): void;
|
||||
/** 错误日志 */
|
||||
error (...args: any[]): void;
|
||||
}
|
||||
|
||||
export interface NapCatPluginContext {
|
||||
core: NapCatCore;
|
||||
oneBot: NapCatOneBot11Adapter;
|
||||
@ -65,6 +81,8 @@ export interface NapCatPluginContext {
|
||||
NapCatConfig: typeof NapCatConfig;
|
||||
adapterName: string;
|
||||
pluginManager: OB11PluginMangerAdapter;
|
||||
/** 插件日志器 - 自动添加插件名称前缀 */
|
||||
logger: PluginLogger;
|
||||
}
|
||||
|
||||
export interface PluginModule<T extends OB11EmitEventContent = OB11EmitEventContent> {
|
||||
@ -331,6 +349,17 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
||||
const dataPath = path.join(this.pluginPath, plugin.dirname, 'data');
|
||||
const configPath = path.join(dataPath, 'config.json');
|
||||
|
||||
// Create plugin-specific logger with prefix
|
||||
const pluginPrefix = `[Plugin: ${plugin.name}]`;
|
||||
const coreLogger = this.logger;
|
||||
const pluginLogger: PluginLogger = {
|
||||
log: (...args: any[]) => coreLogger.log(pluginPrefix, ...args),
|
||||
debug: (...args: any[]) => coreLogger.logDebug(pluginPrefix, ...args),
|
||||
info: (...args: any[]) => coreLogger.log(pluginPrefix, ...args),
|
||||
warn: (...args: any[]) => coreLogger.logWarn(pluginPrefix, ...args),
|
||||
error: (...args: any[]) => coreLogger.logError(pluginPrefix, ...args),
|
||||
};
|
||||
|
||||
const context: NapCatPluginContext = {
|
||||
core: this.core,
|
||||
oneBot: this.obContext,
|
||||
@ -341,7 +370,8 @@ export class OB11PluginMangerAdapter extends IOB11NetworkAdapter<PluginConfig> {
|
||||
configPath: configPath,
|
||||
NapCatConfig: NapCatConfig,
|
||||
adapterName: this.name,
|
||||
pluginManager: this
|
||||
pluginManager: this,
|
||||
logger: pluginLogger
|
||||
};
|
||||
|
||||
plugin.context = context; // Store context on plugin object
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
import type { ActionMap } from 'napcat-types/napcat-onebot/action/index';
|
||||
import { EventType } from 'napcat-types/napcat-onebot/event/index';
|
||||
import type { PluginModule } from 'napcat-types/napcat-onebot/network/plugin-manger';
|
||||
import type { PluginModule, PluginLogger, PluginConfigSchema } from 'napcat-types/napcat-onebot/network/plugin-manger';
|
||||
import type { OB11Message, OB11PostSendMsg } from 'napcat-types/napcat-onebot/types/index';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import type { PluginConfigSchema } from 'napcat-types/napcat-onebot/network/plugin-manger';
|
||||
import { NetworkAdapterConfig } from 'napcat-types/napcat-onebot/config/config';
|
||||
let startTime: number = Date.now();
|
||||
let logger: PluginLogger | null = null;
|
||||
|
||||
interface BuiltinPluginConfig {
|
||||
prefix: string;
|
||||
@ -27,7 +27,8 @@ let currentConfig: BuiltinPluginConfig = {
|
||||
export let plugin_config_ui: PluginConfigSchema = [];
|
||||
|
||||
const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
|
||||
console.log('[Plugin: builtin] NapCat 内置插件已初始化');
|
||||
logger = ctx.logger;
|
||||
logger.info('NapCat 内置插件已初始化');
|
||||
plugin_config_ui = ctx.NapCatConfig.combine(
|
||||
ctx.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>'),
|
||||
ctx.NapCatConfig.text('prefix', 'Command Prefix', '#napcat', 'The prefix to trigger the version info command'),
|
||||
@ -53,7 +54,7 @@ const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
|
||||
Object.assign(currentConfig, savedConfig);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('[Plugin: builtin] Failed to load config', e);
|
||||
logger?.warn('Failed to load config', e);
|
||||
}
|
||||
|
||||
};
|
||||
@ -73,7 +74,7 @@ export const plugin_set_config = async (ctx: any, config: BuiltinPluginConfig) =
|
||||
}
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf-8');
|
||||
} catch (e) {
|
||||
console.error('[Plugin: builtin] Failed to save config', e);
|
||||
logger?.error('Failed to save config', e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
@ -95,9 +96,9 @@ const plugin_onmessage: PluginModule['plugin_onmessage'] = async (_ctx, event) =
|
||||
const message = formatVersionMessage(versionInfo);
|
||||
await sendMessage(_ctx.actions, event, message, _ctx.adapterName, _ctx.pluginManager.config);
|
||||
|
||||
console.log('[Plugin: builtin] 已回复版本信息');
|
||||
logger?.info('已回复版本信息');
|
||||
} catch (error) {
|
||||
console.log('[Plugin: builtin] 处理消息时发生错误:', error);
|
||||
logger?.error('处理消息时发生错误:', error);
|
||||
}
|
||||
};
|
||||
|
||||
@ -112,7 +113,7 @@ async function getVersionInfo (actions: ActionMap, adapter: string, config: Netw
|
||||
protocolVersion: data.protocol_version,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('[Plugin: builtin] 获取版本信息失败:', error);
|
||||
logger?.error('获取版本信息失败:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -150,7 +151,7 @@ async function sendMessage (actions: ActionMap, event: OB11Message, message: str
|
||||
try {
|
||||
await actions.call('send_msg', params, adapter, config);
|
||||
} catch (error) {
|
||||
console.log('[Plugin: builtin] 发送消息失败:', error);
|
||||
logger?.error('发送消息失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
"description": "NapCat 内置插件",
|
||||
"author": "NapNeko",
|
||||
"dependencies": {
|
||||
"napcat-types": "0.0.8"
|
||||
"napcat-types": "0.0.9"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.0.1"
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "napcat-types",
|
||||
"version": "0.0.8",
|
||||
"version": "0.0.9",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
"types": "./napcat-types/index.d.ts",
|
||||
|
||||
@ -213,8 +213,8 @@ importers:
|
||||
packages/napcat-plugin-builtin:
|
||||
dependencies:
|
||||
napcat-types:
|
||||
specifier: 0.0.8
|
||||
version: 0.0.8
|
||||
specifier: 0.0.9
|
||||
version: 0.0.9
|
||||
devDependencies:
|
||||
'@types/node':
|
||||
specifier: ^22.0.1
|
||||
@ -5438,8 +5438,8 @@ packages:
|
||||
engines: {node: ^10 || ^12 || ^13.7 || ^14 || >=15.0.1}
|
||||
hasBin: true
|
||||
|
||||
napcat-types@0.0.8:
|
||||
resolution: {integrity: sha512-QxpYmAWr44tGimWmA6Az+eODlSOU3H2cJ9QzHvC09eaEwLvP6BpD0TApdWFYkzNBFFiHVLXv6lqdCAyAV3RAPg==}
|
||||
napcat-types@0.0.9:
|
||||
resolution: {integrity: sha512-lhK9SgGotQc58jobqnZEnDTadwcwT/Y62aGCH4hQijdiP2eSiu3YWX2oFFoUJxyIpUKFYAfWJkPUv1YHZ/aaWQ==}
|
||||
|
||||
napcat.protobuf@1.1.4:
|
||||
resolution: {integrity: sha512-z7XtLSBJ/PxmYb0VD/w+eYr/X3LyGz+SZ2QejFTOczwt6zWNxy2yV1mTMTvJoc3BWkI3ESVFRxkuT6+pj1tb1Q==}
|
||||
@ -12809,7 +12809,7 @@ snapshots:
|
||||
|
||||
nanoid@3.3.11: {}
|
||||
|
||||
napcat-types@0.0.8:
|
||||
napcat-types@0.0.9:
|
||||
dependencies:
|
||||
'@sinclair/typebox': 0.34.41
|
||||
'@types/cors': 2.8.19
|
||||
|
||||
Loading…
Reference in New Issue
Block a user