Refactor builtin plugin for improved type safety

Replaced generic 'any' types with 'NetworkAdapterConfig' for better type safety in getVersionInfo and sendMessage functions. Removed redundant comments and improved code clarity. Changed a warning log to a standard log for config load failures.
This commit is contained in:
手瓜一十雪 2026-01-28 14:54:43 +08:00
parent 1ae10ae0c6
commit 93126e514e

View File

@ -2,10 +2,10 @@ 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 { 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();
interface BuiltinPluginConfig {
@ -26,9 +26,6 @@ let currentConfig: BuiltinPluginConfig = {
export let plugin_config_ui: PluginConfigSchema = [];
/**
*
*/
const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
console.log('[Plugin: builtin] NapCat 内置插件已初始化');
plugin_config_ui = ctx.NapCatConfig.combine(
@ -56,7 +53,7 @@ const plugin_init: PluginModule['plugin_init'] = async (ctx) => {
Object.assign(currentConfig, savedConfig);
}
} catch (e) {
console.warn('[Plugin: builtin] Failed to load config', e);
console.log('[Plugin: builtin] Failed to load config', e);
}
};
@ -82,27 +79,21 @@ export const plugin_set_config = async (ctx: any, config: BuiltinPluginConfig) =
}
};
/**
*
* #napcat
*/
const plugin_onmessage: PluginModule['plugin_onmessage'] = async (_ctx, event) => {
// Use config logic
const prefix = currentConfig.prefix || '#napcat';
if (currentConfig.enableReply === false) {
return;
}
const prefix = currentConfig.prefix || '#napcat';
if (event.post_type !== EventType.MESSAGE || !event.raw_message.startsWith(prefix)) {
return;
}
try {
const versionInfo = await getVersionInfo(_ctx.actions, _ctx.adapterName, {});
const versionInfo = await getVersionInfo(_ctx.actions, _ctx.adapterName, _ctx.pluginManager.config);
if (!versionInfo) return;
const message = formatVersionMessage(versionInfo);
await sendMessage(_ctx.actions, event, message, _ctx.adapterName, {});
await sendMessage(_ctx.actions, event, message, _ctx.adapterName, _ctx.pluginManager.config);
console.log('[Plugin: builtin] 已回复版本信息');
} catch (error) {
@ -110,10 +101,7 @@ const plugin_onmessage: PluginModule['plugin_onmessage'] = async (_ctx, event) =
}
};
/**
* as
*/
async function getVersionInfo (actions: ActionMap, adapter: string, config: any) {
async function getVersionInfo (actions: ActionMap, adapter: string, config: NetworkAdapterConfig) {
if (!actions) return null;
try {
@ -129,9 +117,6 @@ async function getVersionInfo (actions: ActionMap, adapter: string, config: any)
}
}
/**
*
*/
function formatUptime (ms: number): string {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
@ -149,18 +134,12 @@ function formatUptime (ms: number): string {
}
}
/**
*
*/
function formatVersionMessage (info: { appName: string; appVersion: string; protocolVersion: string; }) {
const uptime = Date.now() - startTime;
return `NapCat 信息\n版本: ${info.appVersion}\n平台: ${process.platform}${process.arch === 'x64' ? ' (64-bit)' : ''}\n运行时间: ${formatUptime(uptime)}`;
}
/**
*
*/
async function sendMessage (actions: ActionMap, event: OB11Message, message: string, adapter: string, config: any) {
async function sendMessage (actions: ActionMap, event: OB11Message, message: string, adapter: string, config: NetworkAdapterConfig) {
const params: OB11PostSendMsg = {
message,
message_type: event.message_type,