@atri-bot/core 1.1.2 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -253,6 +253,7 @@ interface PluginModule {
253
253
  Plugin?: new (...args: ConstructorParameters<typeof BasePlugin>) => BasePlugin;
254
254
  }
255
255
  type LoadPluginHook = (plugin: BasePlugin) => Promise<boolean> | boolean;
256
+ type ImportFunction = () => Promise<PluginModule>;
256
257
 
257
258
  declare class ATRI extends InjectLogger {
258
259
  config: ATRIConfig;
@@ -262,7 +263,7 @@ declare class ATRI extends InjectLogger {
262
263
  loadPluginHooks: Record<string, LoadPluginHook>;
263
264
  private constructor();
264
265
  static init(config: ATRIConfig): Promise<ATRI>;
265
- loadPlugin(importFunction: () => Promise<PluginModule>): Promise<boolean>;
266
+ loadPlugin(importFunction: ImportFunction): Promise<boolean>;
266
267
  unloadPlugin(pluginName: string): Promise<boolean>;
267
268
  loadConfig<TConfig extends object>(pluginName: string, defaultConfig: TConfig): Promise<{}>;
268
269
  saveConfig<TConfig extends object>(pluginName: string, config: TConfig): Promise<void>;
@@ -284,4 +285,4 @@ declare function performanceCounter(): () => string;
284
285
  declare function sortObjectArray<T extends object>(arr: T[], property: keyof T, sortType?: 'up' | 'down'): T[];
285
286
  declare function getImportItemName(importFunction: () => Promise<any>): string;
286
287
 
287
- export { ATRI, type ATRIConfig, type AutoInferCommandEndPoint, type AutoInferMessageEndPoint, type AutoInferNoticeEndPoint, type AutoInferRequestEndPoint, BasePlugin, Bot, type BotConfig, type BotEvents, type CallbackReturn, type CallbackReturnType, type CommandCallback, type CommandContext, type CommandData, type CommandEvent, type LoadPluginHook, type MessageCallback, type MessageEvent, type NonEmptyArray, type NoticeCallback, type NoticeEvent, type OptionArgs, type OptionParams, type PluginModule, type RegEventOptions, type RemoveField, type RequestCallback, type RequestEvent, type UnRegHandler, getImportItemName, performanceCounter, sortObjectArray };
288
+ export { ATRI, type ATRIConfig, type AutoInferCommandEndPoint, type AutoInferMessageEndPoint, type AutoInferNoticeEndPoint, type AutoInferRequestEndPoint, BasePlugin, Bot, type BotConfig, type BotEvents, type CallbackReturn, type CallbackReturnType, type CommandCallback, type CommandContext, type CommandData, type CommandEvent, type ImportFunction, type LoadPluginHook, type MessageCallback, type MessageEvent, type NonEmptyArray, type NoticeCallback, type NoticeEvent, type OptionArgs, type OptionParams, type PluginModule, type RegEventOptions, type RemoveField, type RequestCallback, type RequestEvent, type UnRegHandler, getImportItemName, performanceCounter, sortObjectArray };
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import{InjectLogger as w,Logger as I,LogLevel as f}from"@huan_kong/logger";import m from"fs";import h from"path";import{InjectLogger as O,Logger as T,LogLevel as R}from"@huan_kong/logger";import{CommanderError as x}from"commander";import{NCWebsocket as P,Structs as d}from"node-napcat-ts";function c(){let g=performance.now();return()=>(performance.now()-g).toFixed(2)}function u(g,e,t="up"){return g.sort((n,o)=>n[e]>o[e]?t==="up"?1:-1:n[e]<o[e]?t==="up"?-1:1:0)}function C(g){let e=g.toString().match(/import\(([^)]+)\)/);return e?e[1].replace(/['"]/g,""):"\u83B7\u53D6\u5931\u8D25"}var p=class g extends O{ws;config;events={command:[],message:[],notice:[],request:[]};constructor(e,t){super({level:e.debug?R.DEBUG:e.logLevel}),this.ws=t,this.config=e,e.debug&&(t.on("api.preSend",n=>this.logger.DEBUG("\u53D1\u9001API\u8BF7\u6C42",n)),t.on("api.response.success",n=>this.logger.DEBUG("\u6536\u5230API\u6210\u529F\u54CD\u5E94",n)),t.on("api.response.failure",n=>this.logger.DEBUG("\u6536\u5230API\u5931\u8D25\u54CD\u5E94",n)),t.on("message",n=>this.logger.DEBUG("\u6536\u5230\u6D88\u606F:",n)),t.on("request",n=>this.logger.DEBUG("\u6536\u5230\u8BF7\u6C42:",n)),t.on("notice",n=>this.logger.DEBUG("\u6536\u5230\u901A\u77E5:",n))),t.on("message",async n=>{let o=`message.${n.message_type}.${n.sub_type}`,s=this.config.adminId.includes(n.user_id),i=n.message[0].type==="reply";for(let r of this.events.message)if(o.includes(r.endPoint??"message")&&(!r.needReply||i)&&(!r.needAdmin||s)&&(!r.regexp||r.regexp.test(n.raw_message)))try{if(await r.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${r.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(l){this.logger.ERROR(`\u63D2\u4EF6 ${r.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,l)}for(let r of this.events.command)if(o.includes(r.endPoint??"message")&&(!r.needAdmin||s)&&(!r.needReply||i)){let[l,a]=this.parseCommand(n.raw_message,r.commandName,r.commander);if(l===1)continue;if(l===2){await this.sendMsg(n,[d.text(a)]);continue}let{prefix:y,commandName:E,params:N,args:k}=a;try{if(await r.callback({context:n,prefix:y,commandName:E,params:N,args:k})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${r.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(v){this.logger.ERROR(`\u63D2\u4EF6 ${r.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,v)}}}),t.on("request",async n=>{let o=`request.${n.request_type}.${"sub_type"in n?n.sub_type:""}`;for(let s of this.events.request)if(o.includes(s.endPoint??"request"))try{if(await s.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${s.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(i){this.logger.ERROR(`\u63D2\u4EF6 ${s.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,i)}}),t.on("notice",async n=>{let o=`notice.${n.notice_type}.${"sub_type"in n?n.sub_type:""}`;n.notice_type==="notify"&&(n.sub_type==="input_status"?o+=`.${n.group_id!==0?"group":"friend"}`:n.sub_type==="poke"&&(o+=`.${"group_id"in n?"group":"friend"}`));for(let s of this.events.notice)if(o.includes(s.endPoint??"notice"))try{if(await s.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${s.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(i){this.logger.ERROR(`\u63D2\u4EF6 ${s.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,i)}}),this.logger.INFO("Bot \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){return new Promise((t,n)=>{let o=new T({title:"Bot",level:e.debug?R.DEBUG:e.logLevel}),s=new P({...e.connection,reconnection:e.reconnection}),i=c();s.on("socket.connecting",r=>{i=c(),o.INFO(`\u8FDE\u63A5\u4E2D [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`)}),s.on("socket.error",r=>{if(o.ERROR(`\u8FDE\u63A5\u5931\u8D25 [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`),o.ERROR("\u9519\u8BEF\u4FE1\u606F:",r),r.error_type==="response_error"&&(o.ERROR("NapCat \u670D\u52A1\u7AEF\u8FD4\u56DE\u9519\u8BEF, \u53EF\u80FD\u662F AccessToken \u9519\u8BEF"),process.exit(1)),r.reconnection.nowAttempts>=r.reconnection.attempts)throw n(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${r.reconnection.attempts}\u6B21!`),new Error(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${r.reconnection.attempts}\u6B21!`)}),s.on("socket.open",async r=>{o.INFO(`\u8FDE\u63A5\u6210\u529F [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`),o.INFO(`\u8FDE\u63A5 NapCat \u8017\u65F6: ${i()}ms`),t(new g(e,s))}),s.connect()})}regEvent(e){let t={...e,priority:e.priority??1};switch(t.type){case"command":return this.events.command=u([...this.events.command,t],"priority","down"),()=>{let n=this.events.command.indexOf(t);n!==-1&&this.events.command.splice(n,1)};case"message":return this.events.message=u([...this.events.message,t],"priority","down"),()=>{let n=this.events.message.indexOf(t);n!==-1&&this.events.message.splice(n,1)};case"notice":return this.events.notice=u([...this.events.notice,t],"priority","down"),()=>{let n=this.events.notice.indexOf(t);n!==-1&&this.events.notice.splice(n,1)};case"request":return this.events.request=u([...this.events.request,t],"priority","down"),()=>{let n=this.events.request.indexOf(t);n!==-1&&this.events.request.splice(n,1)}}}parseCommand(e,t,n){let o=e.charAt(0),s=this.config.prefix.find(a=>a===o);if(!s)return[1,"\u672A\u5339\u914D\u5230\u524D\u7F00"];let i=e.split(" ");if(i.length===0)return[1,"\u547D\u4EE4\u4FE1\u606F\u672A\u7A7A"];let r=i[0].slice(s.length),l=i.slice(1).filter(a=>a!=="");if(t!=="*"&&(typeof t=="string"&&t!==r||t instanceof RegExp&&r.match(t)===null))return[1,"\u547D\u4EE4\u540D\u4E0D\u5339\u914D"];if(n)try{let a=n.configureOutput({writeErr:()=>{},writeOut:()=>{}}).exitOverride().parse(l,{from:"user"});return[0,{prefix:s,commandName:r,params:a.opts(),args:a.processedArgs}]}catch(a){return a instanceof x?a.code==="commander.helpDisplayed"?[2,this.getCommandHelpInformation(t.toString())??""]:(a.message=a.message.replace("error:","\u9519\u8BEF:").replace("unknown option","\u672A\u77E5\u9009\u9879").replace("missing required argument","\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570").replace("too many arguments","\u53C2\u6570\u8FC7\u591A").replace("invalid argument","\u65E0\u6548\u53C2\u6570").replace("option '","\u9009\u9879 '").replace("argument missing","\u7F3A\u5C11\u53C2\u6570").replace("Did you mean","\u4F60\u662F\u60F3\u8981"),[2,a.message+(a.message.includes("\u4F60\u662F\u60F3\u8981")?"":`
2
- (\u4F7F\u7528 -h \u83B7\u53D6\u5E2E\u52A9\u4FE1\u606F)`)]):(this.logger.ERROR(a),[2,a instanceof Error?a.message:"\u672A\u77E5\u9519\u8BEF"])}return[0,{prefix:s,commandName:r,params:{},args:[]}]}getCommandInfo(e,t,n="name"){let o=e[n]().replace("/","");return o===""||o==="program"?t:o}getCommandHelpInformation(e){let t=this.events.command.find(i=>i.commandName.toString()===e);if(!t||!t.commander)return;let n=this.getCommandInfo(t.commander,t.commandName.toString()),o=this.config.prefix[0];return t.commander.name(n.includes(o)?n:`${o}${n}`).helpOption("-h, --help","\u5C55\u793A\u5E2E\u52A9\u4FE1\u606F").helpInformation().replace("default:","\u9ED8\u8BA4\u503C:").replace("Arguments:","\u53C2\u6570:").replace("Options:","\u9009\u9879:").replace("Usage:","\u7528\u6CD5:")}async sendMsg(e,t,{reply:n=!0,at:o=!0}={}){try{if(e.message_type==="private")return await this.ws.send_private_msg({user_id:e.user_id,message:t});{let s=[];return n&&e.message_id&&s.push(d.reply(e.message_id)),o&&e.user_id&&s.push(d.at(e.user_id),d.text(`
3
- `)),t=[...s,...t],await this.ws.send_group_msg({group_id:e.group_id,message:t})}}catch{return null}}async sendForwardMsg(e,t){try{return e.message_type==="private"?await this.ws.send_private_forward_msg({user_id:e.user_id,message:t}):await this.ws.send_group_forward_msg({group_id:e.group_id,message:t})}catch{return null}}async isFriend(e){return this.ws.get_friend_list().then(t=>t.find(n=>n.user_id===e.user_id))}async getUsername(e){return"group_id"in e?this.ws.get_group_member_info({group_id:e.group_id,user_id:e.user_id}).then(t=>t.nickname):this.ws.get_stranger_info({user_id:e.user_id}).then(t=>t.nickname)}};var _=class g extends w{config;configDir;bot;loadedPlugins={};loadPluginHooks={};constructor(e,t){super({level:e.debug?f.DEBUG:e.logLevel}),this.config=e,this.bot=t,this.configDir=e.configDir??h.join(e.baseDir,"config"),this.logger.INFO("ATRI \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){e.debug&&(e.logLevel=f.DEBUG);let t=new I({title:"ATRI",level:e.debug?f.DEBUG:e.logLevel});e.disableClearTerminal||console.log("\x1Bc"),e.disableStartupMessage||(console.log(`%c __ .__
1
+ import{InjectLogger as I,Logger as w,LogLevel as f}from"@huan_kong/logger";import m from"fs";import h from"path";import{InjectLogger as O,Logger as T,LogLevel as R}from"@huan_kong/logger";import{CommanderError as x}from"commander";import{NCWebsocket as P,Structs as p}from"node-napcat-ts";function c(){let g=performance.now();return()=>(performance.now()-g).toFixed(2)}function u(g,e,t="up"){return g.sort((n,o)=>n[e]>o[e]?t==="up"?1:-1:n[e]<o[e]?t==="up"?-1:1:0)}function C(g){let e=g.toString().match(/import\(([^)]+)\)/);return e?e[1].replace(/['"]/g,""):"\u83B7\u53D6\u5931\u8D25"}var d=class g extends O{ws;config;events={command:[],message:[],notice:[],request:[]};constructor(e,t){super({level:e.debug?R.DEBUG:e.logLevel}),this.ws=t,this.config=e,e.debug&&(t.on("api.preSend",n=>this.logger.DEBUG("\u53D1\u9001API\u8BF7\u6C42",n)),t.on("api.response.success",n=>this.logger.DEBUG("\u6536\u5230API\u6210\u529F\u54CD\u5E94",n)),t.on("api.response.failure",n=>this.logger.DEBUG("\u6536\u5230API\u5931\u8D25\u54CD\u5E94",n)),t.on("message",n=>this.logger.DEBUG("\u6536\u5230\u6D88\u606F:",n)),t.on("request",n=>this.logger.DEBUG("\u6536\u5230\u8BF7\u6C42:",n)),t.on("notice",n=>this.logger.DEBUG("\u6536\u5230\u901A\u77E5:",n))),t.on("message",async n=>{let o=`message.${n.message_type}.${n.sub_type}`,s=this.config.adminId.includes(n.user_id),i=n.message[0].type==="reply";for(let r of this.events.message)if(o.includes(r.endPoint??"message")&&(!r.needReply||i)&&(!r.needAdmin||s)&&(!r.regexp||r.regexp.test(n.raw_message)))try{if(await r.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${r.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(l){this.logger.ERROR(`\u63D2\u4EF6 ${r.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,l)}for(let r of this.events.command)if(o.includes(r.endPoint??"message")&&(!r.needAdmin||s)&&(!r.needReply||i)){let[l,a]=this.parseCommand(n.raw_message,r.commandName,r.commander);if(l===1)continue;if(l===2){await this.sendMsg(n,[p.text(a)]);continue}let{prefix:y,commandName:E,params:N,args:k}=a;try{if(await r.callback({context:n,prefix:y,commandName:E,params:N,args:k})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${r.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(v){this.logger.ERROR(`\u63D2\u4EF6 ${r.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,v)}}}),t.on("request",async n=>{let o=`request.${n.request_type}.${"sub_type"in n?n.sub_type:""}`;for(let s of this.events.request)if(o.includes(s.endPoint??"request"))try{if(await s.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${s.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(i){this.logger.ERROR(`\u63D2\u4EF6 ${s.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,i)}}),t.on("notice",async n=>{let o=`notice.${n.notice_type}.${"sub_type"in n?n.sub_type:""}`;n.notice_type==="notify"&&(n.sub_type==="input_status"?o+=`.${n.group_id!==0?"group":"friend"}`:n.sub_type==="poke"&&(o+=`.${"group_id"in n?"group":"friend"}`));for(let s of this.events.notice)if(o.includes(s.endPoint??"notice"))try{if(await s.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${s.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(i){this.logger.ERROR(`\u63D2\u4EF6 ${s.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,i)}}),this.logger.INFO("Bot \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){return new Promise((t,n)=>{let o=new T({title:"Bot",level:e.debug?R.DEBUG:e.logLevel}),s=new P({...e.connection,reconnection:e.reconnection}),i=c();s.on("socket.connecting",r=>{i=c(),o.INFO(`\u8FDE\u63A5\u4E2D [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`)}),s.on("socket.error",r=>{if(o.ERROR(`\u8FDE\u63A5\u5931\u8D25 [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`),o.ERROR("\u9519\u8BEF\u4FE1\u606F:",r),r.error_type==="response_error"&&(o.ERROR("NapCat \u670D\u52A1\u7AEF\u8FD4\u56DE\u9519\u8BEF, \u53EF\u80FD\u662F AccessToken \u9519\u8BEF"),process.exit(1)),r.reconnection.nowAttempts>=r.reconnection.attempts)throw n(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${r.reconnection.attempts}\u6B21!`),new Error(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${r.reconnection.attempts}\u6B21!`)}),s.on("socket.open",async r=>{o.INFO(`\u8FDE\u63A5\u6210\u529F [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`),o.INFO(`\u8FDE\u63A5 NapCat \u8017\u65F6: ${i()}ms`),t(new g(e,s))}),s.connect()})}regEvent(e){let t={...e,priority:e.priority??1};switch(t.type){case"command":return this.events.command=u([...this.events.command,t],"priority","down"),()=>{let n=this.events.command.indexOf(t);n!==-1&&this.events.command.splice(n,1)};case"message":return this.events.message=u([...this.events.message,t],"priority","down"),()=>{let n=this.events.message.indexOf(t);n!==-1&&this.events.message.splice(n,1)};case"notice":return this.events.notice=u([...this.events.notice,t],"priority","down"),()=>{let n=this.events.notice.indexOf(t);n!==-1&&this.events.notice.splice(n,1)};case"request":return this.events.request=u([...this.events.request,t],"priority","down"),()=>{let n=this.events.request.indexOf(t);n!==-1&&this.events.request.splice(n,1)}}}parseCommand(e,t,n){let o=e.charAt(0),s=this.config.prefix.find(a=>a===o);if(!s)return[1,"\u672A\u5339\u914D\u5230\u524D\u7F00"];let i=e.split(" ");if(i.length===0)return[1,"\u547D\u4EE4\u4FE1\u606F\u672A\u7A7A"];let r=i[0].slice(s.length),l=i.slice(1).filter(a=>a!=="");if(t!=="*"&&(typeof t=="string"&&t!==r||t instanceof RegExp&&r.match(t)===null))return[1,"\u547D\u4EE4\u540D\u4E0D\u5339\u914D"];if(n)try{let a=n.configureOutput({writeErr:()=>{},writeOut:()=>{}}).exitOverride().parse(l,{from:"user"});return[0,{prefix:s,commandName:r,params:a.opts(),args:a.processedArgs}]}catch(a){return a instanceof x?a.code==="commander.helpDisplayed"?[2,this.getCommandHelpInformation(t.toString())??""]:(a.message=a.message.replace("error:","\u9519\u8BEF:").replace("unknown option","\u672A\u77E5\u9009\u9879").replace("missing required argument","\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570").replace("too many arguments","\u53C2\u6570\u8FC7\u591A").replace("invalid argument","\u65E0\u6548\u53C2\u6570").replace("option '","\u9009\u9879 '").replace("argument missing","\u7F3A\u5C11\u53C2\u6570").replace("Did you mean","\u4F60\u662F\u60F3\u8981"),[2,a.message+(a.message.includes("\u4F60\u662F\u60F3\u8981")?"":`
2
+ (\u4F7F\u7528 -h \u83B7\u53D6\u5E2E\u52A9\u4FE1\u606F)`)]):(this.logger.ERROR(a),[2,a instanceof Error?a.message:"\u672A\u77E5\u9519\u8BEF"])}return[0,{prefix:s,commandName:r,params:{},args:[]}]}getCommandInfo(e,t,n="name"){let o=e[n]().replace("/","");return o===""||o==="program"?t:o}getCommandHelpInformation(e){let t=this.events.command.find(i=>i.commandName.toString()===e);if(!t||!t.commander)return;let n=this.getCommandInfo(t.commander,t.commandName.toString()),o=this.config.prefix[0];return t.commander.name(n.includes(o)?n:`${o}${n}`).helpOption("-h, --help","\u5C55\u793A\u5E2E\u52A9\u4FE1\u606F").helpInformation().replace("default:","\u9ED8\u8BA4\u503C:").replace("Arguments:","\u53C2\u6570:").replace("Options:","\u9009\u9879:").replace("Usage:","\u7528\u6CD5:")}async sendMsg(e,t,{reply:n=!0,at:o=!0}={}){try{if(e.message_type==="private")return await this.ws.send_private_msg({user_id:e.user_id,message:t});{let s=[];return n&&e.message_id&&s.push(p.reply(e.message_id)),o&&e.user_id&&s.push(p.at(e.user_id),p.text(`
3
+ `)),t=[...s,...t],await this.ws.send_group_msg({group_id:e.group_id,message:t})}}catch{return null}}async sendForwardMsg(e,t){try{return e.message_type==="private"?await this.ws.send_private_forward_msg({user_id:e.user_id,message:t}):await this.ws.send_group_forward_msg({group_id:e.group_id,message:t})}catch{return null}}async isFriend(e){return this.ws.get_friend_list().then(t=>t.find(n=>n.user_id===e.user_id))}async getUsername(e){return"group_id"in e?this.ws.get_group_member_info({group_id:e.group_id,user_id:e.user_id}).then(t=>t.nickname):this.ws.get_stranger_info({user_id:e.user_id}).then(t=>t.nickname)}};var _=class g extends I{config;configDir;bot;loadedPlugins={};loadPluginHooks={};constructor(e,t){super({level:e.debug?f.DEBUG:e.logLevel}),this.config=e,this.bot=t,this.configDir=e.configDir??h.join(e.baseDir,"config"),this.logger.INFO("ATRI \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){e.debug&&(e.logLevel=f.DEBUG);let t=new w({title:"ATRI",level:e.debug?f.DEBUG:e.logLevel});e.disableClearTerminal||console.log("\x1Bc"),e.disableStartupMessage||(console.log(`%c __ .__
4
4
  _____ _/ |_ _______ |__|
5
5
  \\__ \\ \\ __\\ \\_ __ \\ | |
6
6
  / __ \\_ | | | | \\/ | |
7
7
  (____ / |__| |__| |__|
8
- \\/`,"font-family: Consolas;"),t.INFO("\u30A2\u30C8\u30EA\u306F\u3001\u9AD8\u6027\u80FD\u3067\u3059\u304B\u3089\uFF01")),"debug"in e.bot||(e.bot.debug=e.debug),"logLevel"in e.bot||(e.bot.logLevel=e.logLevel);let n=await p.init(e.bot);return new g(e,n)}async loadPlugin(e){let t=C(e);if(t==="\u83B7\u53D6\u5931\u8D25")return this.logger.ERROR("\u65E0\u6CD5\u4ECE import \u51FD\u6570\u4E2D\u83B7\u53D6\u63D2\u4EF6\u540D\u79F0\u3002\u8BF7\u786E\u4FDD\u4F20\u5165\u7684\u51FD\u6570\u662F\u4E00\u4E2A import \u8BED\u53E5\u3002"),!1;this.logger.INFO(`\u52A0\u8F7D\u63D2\u4EF6: ${t}`);let n;try{n=await e()}catch(o){return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25:`,o),!1}if(!n.Plugin)return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25: \u672A\u627E\u5230 Plugin \u7C7B`),!1;try{let o=new n.Plugin(this);if(!o.pluginName)return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25: \u7F3A\u5C11\u5FC5\u8981\u53C2\u6570: pluginName`),!1;for(let s in this.loadPluginHooks){let i=this.loadPluginHooks[s];if(!await i(o))return this.logger.ERROR(`\u63D2\u4EF6 ${o.pluginName} \u52A0\u8F7D\u5931\u8D25: \u52A0\u8F7D\u94A9\u5B50 ${s} \u8FD4\u56DE false`),!1}if(!o.getDisableAutoLoadConfig()){let s=await this.loadConfig(o.getConfigName(),o.getDefaultConfig());o.setConfig(s)}await o.load(),this.loadedPlugins[o.pluginName]=o,this.logger.INFO(`\u63D2\u4EF6 ${o.pluginName} \u52A0\u8F7D\u6210\u529F`)}catch(o){return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25:`,o),!1}return!0}async unloadPlugin(e){if(!this.loadedPlugins[e])return this.logger.WARN(`\u63D2\u4EF6 ${e} \u672A\u52A0\u8F7D`),!0;let t=this.loadedPlugins[e];try{t.getUnregHandlers().forEach(n=>n()),await t.unload(),delete this.loadedPlugins[e],this.logger.INFO(`\u63D2\u4EF6 ${e} \u5378\u8F7D\u6210\u529F`)}catch(n){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5378\u8F7D\u5931\u8D25:`,n),!1}return!0}async loadConfig(e,t){m.existsSync(this.configDir)||m.mkdirSync(this.configDir,{recursive:!0});let n=h.join(this.configDir,`${e}.json`);if(!m.existsSync(n))return m.writeFileSync(n,JSON.stringify(t,null,2)),t;try{let o=JSON.parse(m.readFileSync(n,"utf-8"));return{...t,...o}}catch(o){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u914D\u7F6E\u52A0\u8F7D\u5931\u8D25:`,o),{}}}async saveConfig(e,t){m.existsSync(this.configDir)||m.mkdirSync(this.configDir,{recursive:!0});let n=h.join(this.configDir,`${e}.json`);m.writeFileSync(n,JSON.stringify(t,null,2))}addPluginLoadHook(e,t){this.loadPluginHooks[e]=t}removePluginLoadHook(e){delete this.loadPluginHooks[e]}};import{Logger as H,LogLevel as A}from"@huan_kong/logger";var b=class{disableAutoLoadConfig;configName;defaultConfig;config;atri;bot;ws;unregHandlers=[];logger;constructor(e){this.atri=e,this.bot=e.bot,this.ws=e.bot.ws,this.config={}}initLogger(){let e=this.atri.config;this.logger=new H({title:this.pluginName,level:e.debug?A.DEBUG:e.logLevel})}setConfig(e){this.config=e}saveConfig(e){this.atri.saveConfig(this.configName??this.pluginName,e??this.config)}getDisableAutoLoadConfig(){return this.disableAutoLoadConfig}getConfigName(){return this.configName??this.pluginName}getDefaultConfig(){return this.defaultConfig??{}}getUnregHandlers(){return this.unregHandlers}getPluginName(){return this.pluginName}regCommandEvent(e){let t=this.bot.regEvent({...e,type:"command",pluginName:this.pluginName});return this.unregHandlers.push(t),t}regMessageEvent(e){let t=this.bot.regEvent({...e,type:"message",pluginName:this.pluginName});return this.unregHandlers.push(t),t}regRequestEvent(e){let t=this.bot.regEvent({...e,type:"request",pluginName:this.pluginName});return this.unregHandlers.push(t),t}regNoticeEvent(e){let t=this.bot.regEvent({...e,type:"notice",pluginName:this.pluginName});return this.unregHandlers.push(t),t}};export{_ as ATRI,b as BasePlugin,p as Bot,C as getImportItemName,c as performanceCounter,u as sortObjectArray};
8
+ \\/`,"font-family: Consolas;"),t.INFO("\u30A2\u30C8\u30EA\u306F\u3001\u9AD8\u6027\u80FD\u3067\u3059\u304B\u3089\uFF01")),"debug"in e.bot||(e.bot.debug=e.debug),"logLevel"in e.bot||(e.bot.logLevel=e.logLevel);let n=await d.init(e.bot);return new g(e,n)}async loadPlugin(e){let t=C(e);if(t==="\u83B7\u53D6\u5931\u8D25")return this.logger.ERROR("\u65E0\u6CD5\u4ECE import \u51FD\u6570\u4E2D\u83B7\u53D6\u63D2\u4EF6\u540D\u79F0\u3002\u8BF7\u786E\u4FDD\u4F20\u5165\u7684\u51FD\u6570\u662F\u4E00\u4E2A import \u8BED\u53E5\u3002"),!1;this.logger.INFO(`\u52A0\u8F7D\u63D2\u4EF6: ${t}`);let n;try{n=await e()}catch(o){return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25:`,o),!1}if(!n.Plugin)return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25: \u672A\u627E\u5230 Plugin \u7C7B`),!1;try{let o=new n.Plugin(this);if(!o.pluginName)return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25: \u7F3A\u5C11\u5FC5\u8981\u53C2\u6570: pluginName`),!1;for(let s in this.loadPluginHooks){let i=this.loadPluginHooks[s];if(!await i(o))return this.logger.ERROR(`\u63D2\u4EF6 ${o.pluginName} \u52A0\u8F7D\u5931\u8D25: \u52A0\u8F7D\u94A9\u5B50 ${s} \u8FD4\u56DE false`),!1}if(!o.getDisableAutoLoadConfig()){let s=await this.loadConfig(o.getConfigName(),o.getDefaultConfig());o.setConfig(s)}await o.load(),this.loadedPlugins[o.pluginName]=o,this.logger.INFO(`\u63D2\u4EF6 ${o.pluginName} \u52A0\u8F7D\u6210\u529F`)}catch(o){return this.logger.ERROR(`\u63D2\u4EF6 ${t} \u52A0\u8F7D\u5931\u8D25:`,o),!1}return!0}async unloadPlugin(e){if(!this.loadedPlugins[e])return this.logger.WARN(`\u63D2\u4EF6 ${e} \u672A\u52A0\u8F7D`),!0;let t=this.loadedPlugins[e];try{t.getUnregHandlers().forEach(n=>n()),await t.unload(),delete this.loadedPlugins[e],this.logger.INFO(`\u63D2\u4EF6 ${e} \u5378\u8F7D\u6210\u529F`)}catch(n){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5378\u8F7D\u5931\u8D25:`,n),!1}return!0}async loadConfig(e,t){m.existsSync(this.configDir)||m.mkdirSync(this.configDir,{recursive:!0});let n=h.join(this.configDir,`${e}.json`);if(!m.existsSync(n))return m.writeFileSync(n,JSON.stringify(t,null,2)),t;try{let o=JSON.parse(m.readFileSync(n,"utf-8"));return{...t,...o}}catch(o){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u914D\u7F6E\u52A0\u8F7D\u5931\u8D25:`,o),{}}}async saveConfig(e,t){m.existsSync(this.configDir)||m.mkdirSync(this.configDir,{recursive:!0});let n=h.join(this.configDir,`${e}.json`);m.writeFileSync(n,JSON.stringify(t,null,2))}addPluginLoadHook(e,t){this.loadPluginHooks[e]=t}removePluginLoadHook(e){delete this.loadPluginHooks[e]}};import{Logger as H,LogLevel as A}from"@huan_kong/logger";var b=class{disableAutoLoadConfig;configName;defaultConfig;config;atri;bot;ws;unregHandlers=[];logger;constructor(e){this.atri=e,this.bot=e.bot,this.ws=e.bot.ws,this.config={}}initLogger(){let e=this.atri.config;this.logger=new H({title:this.pluginName,level:e.debug?A.DEBUG:e.logLevel})}setConfig(e){this.config=e}saveConfig(e){this.atri.saveConfig(this.configName??this.pluginName,e??this.config)}getDisableAutoLoadConfig(){return this.disableAutoLoadConfig}getConfigName(){return this.configName??this.pluginName}getDefaultConfig(){return this.defaultConfig??{}}getUnregHandlers(){return this.unregHandlers}getPluginName(){return this.pluginName}regCommandEvent(e){let t=this.bot.regEvent({...e,type:"command",pluginName:this.pluginName});return this.unregHandlers.push(t),t}regMessageEvent(e){let t=this.bot.regEvent({...e,type:"message",pluginName:this.pluginName});return this.unregHandlers.push(t),t}regRequestEvent(e){let t=this.bot.regEvent({...e,type:"request",pluginName:this.pluginName});return this.unregHandlers.push(t),t}regNoticeEvent(e){let t=this.bot.regEvent({...e,type:"notice",pluginName:this.pluginName});return this.unregHandlers.push(t),t}};export{_ as ATRI,b as BasePlugin,d as Bot,C as getImportItemName,c as performanceCounter,u as sortObjectArray};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atri-bot/core",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "QQBOT TypeScript framework",
5
5
  "author": "huankong233",
6
6
  "license": "MIT",