@atri-bot/core 1.1.9 → 1.1.11

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
@@ -275,7 +275,7 @@ declare class ATRI extends InjectLogger {
275
275
  * 2 - 加载钩子返回 false (返回 钩子名)
276
276
  */
277
277
  loadPlugin(packageName: string, options?: LoadPluginOptions): Promise<[0, BasePlugin] | [1 | 2, string]>;
278
- unloadPlugin(packageName: string): Promise<boolean>;
278
+ unloadPlugin(packageName: string): Promise<[0] | [1, string]>;
279
279
  loadConfig<TConfig extends object>(packageName: string, defaultConfig: TConfig): Promise<{}>;
280
280
  saveConfig<TConfig extends object>(pluginName: string, config: TConfig): Promise<void>;
281
281
  addPluginLoadHook(hookName: string, hook: LoadPluginHook): void;
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import{InjectLogger as q,Logger as w,LogLevel as R}from"@huan_kong/logger";import{createRequire as A}from"module";import l from"fs";import f from"path";import $ from"process";import{InjectLogger as O,Logger as x,LogLevel as E}from"@huan_kong/logger";import{CommanderError as N}from"commander";import{NCWebsocket as T,Structs as p}from"node-napcat-ts";function v(){let g=performance.now();return()=>(performance.now()-g).toFixed(2)}function m(g,e,t="up"){return g.sort((n,s)=>n[e]>s[e]?t==="up"?1:-1:n[e]<s[e]?t==="up"?-1:1:0)}var c=class g extends O{ws;config;events={command:[],message:[],notice:[],request:[]};constructor(e,t){super({level:e.debug?E.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=>{if(n.message.length===0){this.logger.DEBUG("\u6536\u5230\u7A7A\u6D88\u606F, \u5DF2\u8DF3\u8FC7\u5904\u7406\u6D41\u7A0B:",n);return}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=>{if(n.message.length===0)return;let s=`message.${n.message_type}.${n.sub_type}`,o=this.config.adminId.includes(n.user_id),r=n.message[0].type==="reply";for(let i of this.events.message)if(s.includes(i.endPoint??"message")&&(!i.needReply||r)&&(!i.needAdmin||o)&&(!i.regexp||i.regexp.test(n.raw_message)))try{if(await i.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${i.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(u){this.logger.ERROR(`\u63D2\u4EF6 ${i.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,u)}for(let i of this.events.command)if(s.includes(i.endPoint??"message")&&(!i.needAdmin||o)&&(!i.needReply||r)){let[u,a]=this.parseCommand(n.raw_message,i.commandName,i.commander);if(u===1)continue;if(u===2){await this.sendMsg(n,[p.text(a)]);continue}let{prefix:h,commandName:y,params:d,args:C}=a;try{if(await i.callback({context:n,prefix:h,commandName:y,params:d,args:C})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${i.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(_){this.logger.ERROR(`\u63D2\u4EF6 ${i.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,_)}}}),t.on("request",async n=>{let s=`request.${n.request_type}.${"sub_type"in n?n.sub_type:""}`;for(let o of this.events.request)if(s.includes(o.endPoint??"request"))try{if(await o.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${o.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(r){this.logger.ERROR(`\u63D2\u4EF6 ${o.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,r)}}),t.on("notice",async n=>{let s=`notice.${n.notice_type}.${"sub_type"in n?n.sub_type:""}`;n.notice_type==="notify"&&(n.sub_type==="input_status"?s+=`.${n.group_id!==0?"group":"friend"}`:n.sub_type==="poke"&&(s+=`.${"group_id"in n?"group":"friend"}`));for(let o of this.events.notice)if(s.includes(o.endPoint??"notice"))try{if(await o.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${o.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(r){this.logger.ERROR(`\u63D2\u4EF6 ${o.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,r)}}),this.logger.INFO("Bot \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){return new Promise((t,n)=>{let s=new x({title:"Bot",level:e.debug?E.DEBUG:e.logLevel}),o=new T({...e.connection,reconnection:e.reconnection}),r=v();o.on("socket.connecting",i=>{r=v(),s.INFO(`\u8FDE\u63A5\u4E2D [#${i.reconnection.nowAttempts}/${i.reconnection.attempts}]`)}),o.on("socket.error",i=>{if(s.ERROR(`\u8FDE\u63A5\u5931\u8D25 [#${i.reconnection.nowAttempts}/${i.reconnection.attempts}]`),s.ERROR("\u9519\u8BEF\u4FE1\u606F:",i),i.error_type==="response_error"&&(s.ERROR("NapCat \u670D\u52A1\u7AEF\u8FD4\u56DE\u9519\u8BEF, \u53EF\u80FD\u662F AccessToken \u9519\u8BEF"),process.exit(1)),i.reconnection.nowAttempts>=i.reconnection.attempts)throw n(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${i.reconnection.attempts}\u6B21!`),new Error(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${i.reconnection.attempts}\u6B21!`)}),o.on("socket.open",async i=>{s.INFO(`\u8FDE\u63A5\u6210\u529F [#${i.reconnection.nowAttempts}/${i.reconnection.attempts}]`),s.INFO(`\u8FDE\u63A5 NapCat \u8017\u65F6: ${r()}ms`),t(new g(e,o))}),o.connect()})}regEvent(e){let t={...e,priority:e.priority??1};switch(t.type){case"command":return this.events.command=m([...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=m([...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=m([...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=m([...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 s=e.charAt(0),o=this.config.prefix.find(a=>a===s);if(!o)return[1,"\u672A\u5339\u914D\u5230\u524D\u7F00"];let r=e.split(" ");if(r.length===0)return[1,"\u547D\u4EE4\u4FE1\u606F\u672A\u7A7A"];let i=r[0].slice(o.length),u=r.slice(1).filter(a=>a!=="");if(t!=="*"&&(typeof t=="string"&&t!==i||t instanceof RegExp&&i.match(t)===null))return[1,"\u547D\u4EE4\u540D\u4E0D\u5339\u914D"];if(n)try{let a=n.configureOutput({writeErr:()=>{},writeOut:()=>{}}).exitOverride().parse(u,{from:"user"});return[0,{prefix:o,commandName:i,params:a.opts(),args:a.processedArgs}]}catch(a){if(a instanceof N||"code"in a&&"message"in a){let{code:h,message:y}=a;if(h==="commander.helpDisplayed")return[2,this.getCommandHelpInformation(t.toString())??""];let d=y.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");return[2,d+(d.includes("\u4F60\u662F\u60F3\u8981")?"":`
1
+ import{InjectLogger as q,Logger as w,LogLevel as R}from"@huan_kong/logger";import{createRequire as $}from"module";import l from"fs";import f from"path";import A from"process";import{InjectLogger as O,Logger as x,LogLevel as _}from"@huan_kong/logger";import{CommanderError as N}from"commander";import{NCWebsocket as T,Structs as p}from"node-napcat-ts";function v(){let g=performance.now();return()=>(performance.now()-g).toFixed(2)}function u(g,e,t="up"){return g.sort((n,s)=>n[e]>s[e]?t==="up"?1:-1:n[e]<s[e]?t==="up"?-1:1:0)}var c=class g extends O{ws;config;events={command:[],message:[],notice:[],request:[]};constructor(e,t){super({level:e.debug?_.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=>{if(n.message.length===0){this.logger.DEBUG("\u6536\u5230\u7A7A\u6D88\u606F, \u5DF2\u8DF3\u8FC7\u5904\u7406\u6D41\u7A0B:",n);return}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=>{if(n.message.length===0)return;let s=`message.${n.message_type}.${n.sub_type}`,o=this.config.adminId.includes(n.user_id),r=n.message[0].type==="reply";for(let i of this.events.message)if(s.includes(i.endPoint??"message")&&(!i.needReply||r)&&(!i.needAdmin||o)&&(!i.regexp||i.regexp.test(n.raw_message)))try{if(await i.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${i.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(m){this.logger.ERROR(`\u63D2\u4EF6 ${i.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,m)}for(let i of this.events.command)if(s.includes(i.endPoint??"message")&&(!i.needAdmin||o)&&(!i.needReply||r)){let[m,a]=this.parseCommand(n.raw_message,i.commandName,i.commander);if(m===1)continue;if(m===2){await this.sendMsg(n,[p.text(a)]);continue}let{prefix:h,commandName:y,params:d,args:C}=a;try{if(await i.callback({context:n,prefix:h,commandName:y,params:d,args:C})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${i.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(E){this.logger.ERROR(`\u63D2\u4EF6 ${i.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,E)}}}),t.on("request",async n=>{let s=`request.${n.request_type}.${"sub_type"in n?n.sub_type:""}`;for(let o of this.events.request)if(s.includes(o.endPoint??"request"))try{if(await o.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${o.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(r){this.logger.ERROR(`\u63D2\u4EF6 ${o.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,r)}}),t.on("notice",async n=>{let s=`notice.${n.notice_type}.${"sub_type"in n?n.sub_type:""}`;n.notice_type==="notify"&&(n.sub_type==="input_status"?s+=`.${n.group_id!==0?"group":"friend"}`:n.sub_type==="poke"&&(s+=`.${"group_id"in n?"group":"friend"}`));for(let o of this.events.notice)if(s.includes(o.endPoint??"notice"))try{if(await o.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${o.pluginName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(r){this.logger.ERROR(`\u63D2\u4EF6 ${o.pluginName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,r)}}),this.logger.INFO("Bot \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){return new Promise((t,n)=>{let s=new x({title:"Bot",level:e.debug?_.DEBUG:e.logLevel}),o=new T({...e.connection,reconnection:e.reconnection}),r=v();o.on("socket.connecting",i=>{r=v(),s.INFO(`\u8FDE\u63A5\u4E2D [#${i.reconnection.nowAttempts}/${i.reconnection.attempts}]`)}),o.on("socket.error",i=>{if(s.ERROR(`\u8FDE\u63A5\u5931\u8D25 [#${i.reconnection.nowAttempts}/${i.reconnection.attempts}]`),s.ERROR("\u9519\u8BEF\u4FE1\u606F:",i),i.error_type==="response_error"&&(s.ERROR("NapCat \u670D\u52A1\u7AEF\u8FD4\u56DE\u9519\u8BEF, \u53EF\u80FD\u662F AccessToken \u9519\u8BEF"),process.exit(1)),i.reconnection.nowAttempts>=i.reconnection.attempts)throw n(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${i.reconnection.attempts}\u6B21!`),new Error(`\u91CD\u8BD5\u6B21\u6570\u8D85\u8FC7\u8BBE\u7F6E\u7684${i.reconnection.attempts}\u6B21!`)}),o.on("socket.open",async i=>{s.INFO(`\u8FDE\u63A5\u6210\u529F [#${i.reconnection.nowAttempts}/${i.reconnection.attempts}]`),s.INFO(`\u8FDE\u63A5 NapCat \u8017\u65F6: ${r()}ms`),t(new g(e,o))}),o.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 s=e.charAt(0),o=this.config.prefix.find(a=>a===s);if(!o)return[1,"\u672A\u5339\u914D\u5230\u524D\u7F00"];let r=e.split(" ");if(r.length===0)return[1,"\u547D\u4EE4\u4FE1\u606F\u672A\u7A7A"];let i=r[0].slice(o.length),m=r.slice(1).filter(a=>a!=="");if(t!=="*"&&(typeof t=="string"&&t!==i||t instanceof RegExp&&i.match(t)===null))return[1,"\u547D\u4EE4\u540D\u4E0D\u5339\u914D"];if(n)try{let a=n.configureOutput({writeErr:()=>{},writeOut:()=>{}}).exitOverride().parse(m,{from:"user"});return[0,{prefix:o,commandName:i,params:a.opts(),args:a.processedArgs}]}catch(a){if(a instanceof N||"code"in a&&"message"in a){let{code:h,message:y}=a;if(h==="commander.helpDisplayed")return[2,this.getCommandHelpInformation(t.toString())??""];let d=y.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").replace(/Expected (\d+) arguments? but got (\d+)\./,"\u671F\u671B $1 \u4E2A\u53C2\u6570\uFF0C\u4F46\u5F97\u5230\u4E86 $2 \u4E2A\u53C2\u6570\u3002");return[2,d+(d.includes("\u4F60\u662F\u60F3\u8981")?"":`
2
2
  (\u4F7F\u7528 -h \u83B7\u53D6\u5E2E\u52A9\u4FE1\u606F)`)]}else return this.logger.ERROR("\u547D\u4EE4\u5904\u7406\u51FA\u9519:",a),[2,a instanceof Error?a.message:"\u672A\u77E5\u9519\u8BEF"]}return[0,{prefix:o,commandName:i,params:{},args:[]}]}getCommandInfo(e,t,n="name"){let s=e[n]().replace("/","");return s===""||s==="program"?t:s}getCommandHelpInformation(e){let t=this.events.command.find(r=>r.commandName.toString()===e);if(!t||!t.commander)return;let n=this.getCommandInfo(t.commander,t.commandName.toString()),s=this.config.prefix[0];return t.commander.name(n.includes(s)?n:`${s}${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:s=!0}={}){try{if(e.message_type==="private")return await this.ws.send_private_msg({user_id:e.user_id,message:t});{let o=[];return n&&e.message_id&&o.push(p.reply(e.message_id)),s&&e.user_id&&o.push(p.at(e.user_id),p.text(`
3
- `)),t=[...o,...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 b=class g extends q{config;configDir;bot;import;loadedPlugins={};loadPluginHooks={};constructor(e,t){super({level:e.debug?R.DEBUG:e.logLevel}),this.config=e,this.bot=t,this.configDir=e.configDir??f.join(e.baseDir,"config"),this.import=A(e.baseDir)}static async init(e){e.debug&&(e.logLevel=R.DEBUG);let t=new w({title:"ATRI",level:e.debug?R.DEBUG:e.logLevel});e.disableClearTerminal||console.log("\x1Bc"),e.disableStartupMessage||(console.log(`%c __ .__
3
+ `)),t=[...o,...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 b=class g extends q{config;configDir;bot;import;loadedPlugins={};loadPluginHooks={};constructor(e,t){super({level:e.debug?R.DEBUG:e.logLevel}),this.config=e,this.bot=t,this.configDir=e.configDir??f.join(e.baseDir,"config"),this.import=$(e.baseDir)}static async init(e){e.debug&&(e.logLevel=R.DEBUG);let t=new w({title:"ATRI",level:e.debug?R.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 c.init(e.bot),s=new g(e,n);for(let o of e.plugins??[]){let[r]=await s.loadPlugin(o);r!==0&&(t.ERROR("\u63D2\u4EF6\u52A0\u8F7D\u5931\u8D25\uFF0C\u7A0B\u5E8F\u7EC8\u6B62"),$.exit(1))}return t.INFO("ATRI \u521D\u59CB\u5316\u5B8C\u6210"),s}async loadPlugin(e,t){if(t={initPlugin:!0,quiet:!1,...t??{}},t.quiet||this.logger.INFO(`\u52A0\u8F7D\u63D2\u4EF6: ${e}`),this.loadedPlugins[e])return t.quiet||this.logger.WARN(`\u63D2\u4EF6 ${e} \u5DF2\u52A0\u8F7D\uFF0C\u8DF3\u8FC7\u52A0\u8F7D`),[0,this.loadedPlugins[e]];let n;try{n=await this.import(e)}catch(r){return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5BFC\u5165\u5931\u8D25:`,r),[1,r instanceof Error?r.message:String(r)]}if(!n.Plugin)return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u5931\u8D25: \u672A\u627E\u5230 Plugin \u7C7B`),[1,"\u672A\u627E\u5230 Plugin \u7C7B"];let s;try{let r=this.import.resolve(f.join(e,"package.json"));s=JSON.parse(l.readFileSync(r,"utf-8"))}catch(r){t.quiet||(this.logger.WARN(`\u63D2\u4EF6 ${e} \u672A\u627E\u5230 package.json, \u5C06\u4F7F\u7528\u672A\u77E5\u4EE3\u66FF`),this.logger.DEBUG(r)),s={name:"\u672A\u77E5",version:"\u672A\u77E5"}}let o;try{o=new n.Plugin(this,s)}catch(r){return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5B9E\u4F8B\u5316\u5931\u8D25:`,r),[1,r instanceof Error?r.message:String(r)]}if(!t.initPlugin)return[0,o];try{for(let r in this.loadPluginHooks){let i=this.loadPluginHooks[r];if(!await i({plugin:o,packageName:e}))return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u5931\u8D25: \u52A0\u8F7D\u94A9\u5B50 ${r} \u8FD4\u56DE false`),[2,r]}return o.disableAutoLoadConfig||(o.config=await this.loadConfig(o.configName??e,o.defaultConfig??{})),await o.load(),this.loadedPlugins[e]=o,t.quiet||this.logger.INFO(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u6210\u529F`),[0,o]}catch(r){return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u5931\u8D25:`,r),[1,r instanceof Error?r.message:String(r)]}}async unloadPlugin(e){let t=this.loadedPlugins[e];if(!t)return this.logger.WARN(`\u63D2\u4EF6 ${e} \u672A\u52A0\u8F7D`),!0;try{t.unregHandlers.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){e=e.replaceAll("/","__"),l.existsSync(this.configDir)||l.mkdirSync(this.configDir,{recursive:!0});let n=f.join(this.configDir,`${e}.json`);if(!l.existsSync(n))return l.writeFileSync(n,JSON.stringify(t,null,2)),t;try{let s=JSON.parse(l.readFileSync(n,"utf-8"));return{...t,...s}}catch(s){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u914D\u7F6E\u52A0\u8F7D\u5931\u8D25:`,s),{}}}async saveConfig(e,t){l.existsSync(this.configDir)||l.mkdirSync(this.configDir,{recursive:!0});let n=f.join(this.configDir,`${e}.json`);l.writeFileSync(n,JSON.stringify(t,null,2))}addPluginLoadHook(e,t){this.loadPluginHooks[e]=t}removePluginLoadHook(e){delete this.loadPluginHooks[e]}};var P=class{static enum(e){return function(t){if(!e.includes(t))throw new Error(`\u53C2\u6570 "${t}" \u4E0D\u662F\u6709\u6548\u53C2\u6570, \u6709\u6548\u53C2\u6570:
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 c.init(e.bot),s=new g(e,n);for(let o of e.plugins??[]){let[r]=await s.loadPlugin(o);r!==0&&(t.ERROR("\u63D2\u4EF6\u52A0\u8F7D\u5931\u8D25\uFF0C\u7A0B\u5E8F\u7EC8\u6B62"),A.exit(1))}return t.INFO("ATRI \u521D\u59CB\u5316\u5B8C\u6210"),s}async loadPlugin(e,t){if(t={initPlugin:!0,quiet:!1,...t??{}},t.quiet||this.logger.INFO(`\u52A0\u8F7D\u63D2\u4EF6: ${e}`),this.loadedPlugins[e])return t.quiet||this.logger.WARN(`\u63D2\u4EF6 ${e} \u5DF2\u52A0\u8F7D\uFF0C\u8DF3\u8FC7\u52A0\u8F7D`),[0,this.loadedPlugins[e]];let n;try{n=await this.import(e)}catch(r){return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5BFC\u5165\u5931\u8D25:`,r),[1,r instanceof Error?r.message:String(r)]}if(!n.Plugin)return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u5931\u8D25: \u672A\u627E\u5230 Plugin \u7C7B`),[1,"\u672A\u627E\u5230 Plugin \u7C7B"];let s;try{let r=this.import.resolve(f.join(e,"package.json"));s=JSON.parse(l.readFileSync(r,"utf-8"))}catch(r){t.quiet||(this.logger.WARN(`\u63D2\u4EF6 ${e} \u672A\u627E\u5230 package.json, \u5C06\u4F7F\u7528\u672A\u77E5\u4EE3\u66FF`),this.logger.DEBUG(r)),s={name:"\u672A\u77E5",version:"\u672A\u77E5"}}let o;try{o=new n.Plugin(this,s)}catch(r){return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5B9E\u4F8B\u5316\u5931\u8D25:`,r),[1,r instanceof Error?r.message:String(r)]}if(!t.initPlugin)return[0,o];try{for(let r in this.loadPluginHooks){let i=this.loadPluginHooks[r];if(!await i({plugin:o,packageName:e}))return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u5931\u8D25: \u52A0\u8F7D\u94A9\u5B50 ${r} \u8FD4\u56DE false`),[2,r]}return o.disableAutoLoadConfig||(o.config=await this.loadConfig(o.configName??e,o.defaultConfig??{})),await o.load(),this.loadedPlugins[e]=o,t.quiet||this.logger.INFO(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u6210\u529F`),[0,o]}catch(r){return t.quiet||this.logger.ERROR(`\u63D2\u4EF6 ${e} \u52A0\u8F7D\u5931\u8D25:`,r),[1,r instanceof Error?r.message:String(r)]}}async unloadPlugin(e){let t=this.loadedPlugins[e];if(!t)return this.logger.WARN(`\u63D2\u4EF6 ${e} \u672A\u52A0\u8F7D`),[1,"\u63D2\u4EF6\u672A\u52A0\u8F7D"];try{return t.unregHandlers.forEach(n=>n()),await t.unload(),delete this.loadedPlugins[e],this.logger.INFO(`\u63D2\u4EF6 ${e} \u5378\u8F7D\u6210\u529F`),[0]}catch(n){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u5378\u8F7D\u5931\u8D25:`,n),[1,n instanceof Error?n.message:String(n)]}}async loadConfig(e,t){e=e.replaceAll("/","__"),l.existsSync(this.configDir)||l.mkdirSync(this.configDir,{recursive:!0});let n=f.join(this.configDir,`${e}.json`);if(!l.existsSync(n))return l.writeFileSync(n,JSON.stringify(t,null,2)),t;try{let s=JSON.parse(l.readFileSync(n,"utf-8"));return{...t,...s}}catch(s){return this.logger.ERROR(`\u63D2\u4EF6 ${e} \u914D\u7F6E\u52A0\u8F7D\u5931\u8D25:`,s),{}}}async saveConfig(e,t){l.existsSync(this.configDir)||l.mkdirSync(this.configDir,{recursive:!0});let n=f.join(this.configDir,`${e}.json`);l.writeFileSync(n,JSON.stringify(t,null,2))}addPluginLoadHook(e,t){this.loadPluginHooks[e]=t}removePluginLoadHook(e){delete this.loadPluginHooks[e]}};var P=class{static enum(e){return function(t){if(!e.includes(t))throw new Error(`\u53C2\u6570 "${t}" \u4E0D\u662F\u6709\u6548\u53C2\u6570, \u6709\u6548\u53C2\u6570:
9
9
  ${e.map(n=>` - ${n}`).join(`
10
- `)}`);return t}}};import{Logger as I,LogLevel as H}from"@huan_kong/logger";var k=class{packageJson;disableAutoLoadConfig;configName;defaultConfig;config;atri;bot;ws;unregHandlers=[];logger;constructor(e,t){this.atri=e,this.bot=e.bot,this.ws=e.bot.ws,this.config={},this.packageJson=t,this.logger=new I({title:this.packageJson.name,level:e.config.debug?H.DEBUG:e.config.logLevel})}saveConfig(e){this.atri.saveConfig(this.configName??this.packageJson.name,e??this.config)}regCommandEvent(e){let t=this.bot.regEvent({...e,type:"command",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}regMessageEvent(e){let t=this.bot.regEvent({...e,type:"message",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}regRequestEvent(e){let t=this.bot.regEvent({...e,type:"request",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}regNoticeEvent(e){let t=this.bot.regEvent({...e,type:"notice",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}};export{b as ATRI,k as BasePlugin,c as Bot,P as CommanderUtils,v as performanceCounter,m as sortObjectArray};
10
+ `)}`);return t}}};import{Logger as I,LogLevel as H}from"@huan_kong/logger";var k=class{packageJson;disableAutoLoadConfig;configName;defaultConfig;config;atri;bot;ws;unregHandlers=[];logger;constructor(e,t){this.atri=e,this.bot=e.bot,this.ws=e.bot.ws,this.config={},this.packageJson=t,this.logger=new I({title:this.packageJson.name,level:e.config.debug?H.DEBUG:e.config.logLevel})}saveConfig(e){this.atri.saveConfig(this.configName??this.packageJson.name,e??this.config)}regCommandEvent(e){let t=this.bot.regEvent({...e,type:"command",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}regMessageEvent(e){let t=this.bot.regEvent({...e,type:"message",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}regRequestEvent(e){let t=this.bot.regEvent({...e,type:"request",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}regNoticeEvent(e){let t=this.bot.regEvent({...e,type:"notice",pluginName:this.packageJson.name});return this.unregHandlers.push(t),t}};export{b as ATRI,k as BasePlugin,c as Bot,P as CommanderUtils,v 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.9",
3
+ "version": "1.1.11",
4
4
  "description": "QQBOT TypeScript framework",
5
5
  "author": "huankong233",
6
6
  "license": "MIT",