@atri-bot/core 1.1.25 → 1.1.27
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.js +3 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var I=Object.defineProperty;var g=(d,e)=>I(d,"name",{value:e,configurable:!0});import{InjectLogger as L,Logger as M,LogLevel as w}from"@huan_kong/logger";import{createRequire as O}from"module";import l from"fs";import h from"path";import B from"process";import{InjectLogger as
|
|
2
|
-
${n instanceof Error?n.stack??n.message:String(n)}, \u8BF7\u524D\u5F80\u540E\u53F0\u68C0\u67E5`)])}),this.logger.INFO("Bot \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){return new Promise((t,n)=>{let s=new
|
|
1
|
+
var I=Object.defineProperty;var g=(d,e)=>I(d,"name",{value:e,configurable:!0});import{InjectLogger as L,Logger as M,LogLevel as w}from"@huan_kong/logger";import{createRequire as O}from"module";import l from"fs";import h from"path";import B from"process";import{InjectLogger as A,Logger as q,LogLevel as P}from"@huan_kong/logger";import{CommanderError as S}from"commander";import{NCWebsocket as D,Structs as c}from"node-napcat-ts";function b(){let d=performance.now();return()=>(performance.now()-d).toFixed(2)}g(b,"performanceCounter");function f(d,e,t="up"){return d.sort((n,s)=>n[e]>s[e]?t==="up"?1:-1:n[e]<s[e]?t==="up"?-1:1:0)}g(f,"sortObjectArray");var v=class d extends A{static{g(this,"Bot")}ws;config;events={command:[],message:[],notice:[],request:[]};constructor(e,t){super({level:e.logLevel??(e.debug?P.DEBUG:void 0)}),this.ws=t,this.config=e,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",async n=>{if(n.message.length===0){this.logger.DEBUG("\u6536\u5230\u7A7A\u6D88\u606F, \u5DF2\u8DF3\u8FC7\u5904\u7406\u6D41\u7A0B:",n);return}if(this.config.debug&&!this.config.adminId.includes(n.user_id)){this.logger.DEBUG("\u5F53\u524D\u5904\u4E8E\u8C03\u8BD5\u6A21\u5F0F, \u975E\u7BA1\u7406\u5458\u6D88\u606F, \u5DF2\u8DF3\u8FC7\u5904\u7406\u6D41\u7A0B:",n);return}else this.logger.DEBUG("\u6536\u5230\u6D88\u606F:",n);let s=`message.${n.message_type}.${n.sub_type}`,i=this.config.adminId.includes(n.user_id),o=n.message[0].type==="reply";for(let r of this.events.message)if(s.includes(r.endPoint??"message")&&(!r.needReply||o)&&(!r.needAdmin||i)&&(!r.regexp||r.regexp.test(n.raw_message)))try{if(await r.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${r.packageName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(m){this.logger.ERROR(`\u63D2\u4EF6 ${r.packageName} message \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,m),await this.sendMsg(n,[c.text(`\u63D2\u4EF6 ${r.packageName} message \u4E8B\u4EF6\u5904\u7406\u5931\u8D25, \u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u5904\u7406: ${m instanceof Error?m.stack??m.message:String(m)}`)])}for(let r of this.events.command)if(s.includes(r.endPoint??"message")&&(!r.needAdmin||i)&&(!r.needReply||o)){let[m,a]=this.parseCommand(n.raw_message,r.commandName,r.commander);if(m===1)continue;if(m===2){await this.sendMsg(n,[c.text(a)]);continue}let{prefix:C,commandName:_,params:y,args:x}=a;try{if(await r.callback({context:n,prefix:C,commandName:_,params:y,args:x})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${r.packageName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(p){this.logger.ERROR(`\u63D2\u4EF6 ${r.packageName} command \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,p),await this.sendMsg(n,[c.text(`\u63D2\u4EF6 ${r.packageName} command \u4E8B\u4EF6\u5904\u7406\u5931\u8D25, \u8BF7\u8054\u7CFB\u7BA1\u7406\u5458\u5904\u7406: ${p instanceof Error?p.stack??p.message:String(p)}`)])}}}),t.on("request",async n=>{this.logger.DEBUG("\u6536\u5230\u8BF7\u6C42:",n);let s=`request.${n.request_type}.${"sub_type"in n?n.sub_type:""}`;for(let i of this.events.request)if(s.includes(i.endPoint??"request"))try{if(await i.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${i.packageName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(o){this.logger.ERROR(`\u63D2\u4EF6 ${i.packageName} \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,o),await this.sendMsg({message_type:"private",user_id:this.config.adminId[0]},[c.text(`\u63D2\u4EF6 ${i.packageName} request \u4E8B\u4EF6\u5904\u7406\u5931\u8D25: ${o instanceof Error?o.stack??o.message:String(o)}`)])}}),t.on("notice",async n=>{this.logger.DEBUG("\u6536\u5230\u901A\u77E5:",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 i of this.events.notice)if(s.includes(i.endPoint??"notice"))try{if(await i.callback({context:n})==="quit"){this.logger.DEBUG(`\u63D2\u4EF6 ${i.packageName} \u8BF7\u6C42\u63D0\u524D\u7EC8\u6B62`);break}}catch(o){this.logger.ERROR(`\u63D2\u4EF6 ${i.packageName} notice \u4E8B\u4EF6\u5904\u7406\u5931\u8D25:`,o),await this.sendMsg({message_type:"private",user_id:this.config.adminId[0]},[c.text(`\u63D2\u4EF6 ${i.packageName} notice \u4E8B\u4EF6\u5904\u7406\u5931\u8D25: ${o instanceof Error?o.stack??o.message:String(o)}`)])}}),process.on("uncaughtException",n=>{this.sendMsg({message_type:"private",user_id:this.config.adminId[0]},[c.text(`\u6355\u83B7\u5230\u672A\u5904\u7406\u7684\u5F02\u5E38, \u673A\u5668\u4EBA\u5C06\u9000\u51FA, \u9519\u8BEF\u4FE1\u606F:
|
|
2
|
+
${n instanceof Error?n.stack??n.message:String(n)}, \u8BF7\u524D\u5F80\u540E\u53F0\u68C0\u67E5`)])}),this.logger.INFO("Bot \u521D\u59CB\u5316\u5B8C\u6210")}static async init(e){return new Promise((t,n)=>{let s=new q({title:"Bot",level:e.logLevel??(e.debug?P.DEBUG:void 0)}),i=new D({...e.connection,reconnection:e.reconnection}),o=b();i.on("socket.connecting",r=>{o=b(),s.INFO(`\u8FDE\u63A5\u4E2D [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`)}),i.on("socket.error",r=>{if(s.ERROR(`\u8FDE\u63A5\u5931\u8D25 [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`),s.ERROR("\u9519\u8BEF\u4FE1\u606F:",r),r.error_type==="response_error"&&(s.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!`)}),i.on("socket.open",async r=>{s.INFO(`\u8FDE\u63A5\u6210\u529F [#${r.reconnection.nowAttempts}/${r.reconnection.attempts}]`),s.INFO(`\u8FDE\u63A5 NapCat \u8017\u65F6: ${o()}ms`),t(new d(e,i))}),i.connect()})}regEvent(e){let t={...e,priority:e.priority??1};switch(t.type){case"command":return this.events.command=f([...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=f([...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=f([...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=f([...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),i=this.config.prefix.find(a=>a===s);if(!i)return[1,"\u672A\u5339\u914D\u5230\u524D\u7F00"];let o=e.split(" ");if(o.length===0)return[1,"\u547D\u4EE4\u4FE1\u606F\u672A\u7A7A"];let r=o[0].slice(i.length),m=o.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:g(()=>{},"writeErr"),writeOut:g(()=>{},"writeOut")}).exitOverride().parse(m,{from:"user"});return[0,{prefix:i,commandName:r,params:a.opts(),args:a.processedArgs}]}catch(a){if(a instanceof S||"code"in a&&"message"in a){let{code:C,message:_}=a;if(C==="commander.helpDisplayed")return[2,this.getCommandHelpInformation(t.toString())??""];let 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,y+(y.includes("\u4F60\u662F\u60F3\u8981")?"":`
|
|
3
3
|
(\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:i,commandName:r,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(o=>o.commandName==="*"?!1:typeof o.commandName=="string"?o.commandName===e:o.commandName instanceof RegExp?o.commandName.test(e):!1);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().replaceAll("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 i=[];return n&&e.message_id&&i.push(c.reply(e.message_id)),s&&e.user_id&&i.push(c.at(e.user_id),c.text(`
|
|
4
|
-
`)),t=[...i,...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)}};import{InjectLogger as H,LogLevel as k}from"@huan_kong/logger";import u from"fs";import R from"path";var E=class extends H{static{g(this,"LogRecorder")}config;atriConfig;originConsoleLog=console.log;constructor(e,t){if(super({level:e.logLevel??(e.debug?k.DEBUG:void 0)}),this.atriConfig=e,this.config=t,e.debug&&!t.enable){this.logger.WARN("\u5F53\u524D\u5904\u4E8E\
|
|
4
|
+
`)),t=[...i,...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)}};import{InjectLogger as H,LogLevel as k}from"@huan_kong/logger";import u from"fs";import R from"path";var E=class extends H{static{g(this,"LogRecorder")}config;atriConfig;originConsoleLog=console.log;constructor(e,t){if(super({level:e.logLevel??(e.debug?k.DEBUG:void 0)}),this.atriConfig=e,this.config=t,e.debug&&!t.enable){this.logger.WARN("\u5F53\u524D\u5904\u4E8E\u8C03\u8BD5\u6A21\u5F0F, \u5982\u9700\u8BB0\u5F55\u65E5\u5FD7, \u8BF7\u5C06 logRecorder.enable \u8BBE\u4E3A true");return}this.config={maxFiles:31,maxSize:100*1024*1024,logLevel:e.debug?k.DEBUG:k.INFO,logDir:R.join(e.baseDir,"logs"),...this.config},this.rewriteConsole(),this.handleException(),this.logger.INFO("\u65E5\u5FD7\u8BB0\u5F55\u5668\u5DF2\u542F\u7528")}rewriteConsole(){console.log=(e=>(...t)=>{this.saveLog(t),e(...t)})(console.log)}handleException(){process.on("uncaughtException",e=>{this.logger.ERROR(`\u6355\u83B7\u5230\u672A\u5904\u7406\u7684\u5F02\u5E38, \u9519\u8BEF\u4FE1\u606F:
|
|
5
5
|
`,e)})}stripAnsi(e){return e.replace(/\u001B\[[0-9;]*m/g,"")}saveLog(e){let t=e.map(s=>this.stripAnsi(s)).join(" ");u.existsSync(this.config.logDir)||u.mkdirSync(this.config.logDir,{recursive:!0});let n=R.join(this.config.logDir,`${new Date().toISOString().split("T")[0]}.log`);u.existsSync(n)||this.removeUselessLogs(),u.appendFileSync(n,t+`
|
|
6
6
|
`,{encoding:"utf-8"})}removeUselessLogs(){let e=u.readdirSync(this.config.logDir).filter(n=>/^\d{4}-\d{2}-\d{2}/.test(n)&&u.statSync(R.join(this.config.logDir,n)).isFile());if(e.length<=this.config.maxFiles)return;let t=e.length-this.config.maxFiles+1;e.sort().slice(0,t).map(n=>u.rmSync(R.join(this.config.logDir,n)))}};var T=class d extends L{static{g(this,"ATRI")}config;configDir;bot;logRecorder;import;loadedPlugins={};loadPluginHooks={};constructor(e,t,n){super({level:e.logLevel??(e.debug?w.DEBUG:void 0)}),this.config=e,this.bot=t,this.logRecorder=n,this.configDir=e.configDir??h.join(e.baseDir,"config"),this.import=O(e.baseDir)}static async init(e){let t=new M({title:"ATRI",level:e.logLevel??(e.debug?w.DEBUG:void 0)});e.disableClearTerminal||console.log("\x1Bc"),e.disableStartupMessage||console.log(`%c __ .__
|
|
7
7
|
_____ _/ |_ _______ |__|
|