koishi-plugin-edging
Version:
一个 Koishi 插件
4 lines (3 loc) • 13.4 kB
JavaScript
var F=Object.defineProperty;var P=(I,O)=>F(I,"name",{value:O,configurable:!0});(()=>{"use strict";var I={913:function(h,p,l){var S=this&&this.__createBinding||(Object.create?function(e,i,o,d){d===void 0&&(d=o);var y=Object.getOwnPropertyDescriptor(i,o);(!y||("get"in y?!i.__esModule:y.writable||y.configurable))&&(y={enumerable:!0,get:function(){return i[o]}}),Object.defineProperty(e,d,y)}:function(e,i,o,d){d===void 0&&(d=o),e[d]=i[o]}),f=this&&this.__setModuleDefault||(Object.create?function(e,i){Object.defineProperty(e,"default",{enumerable:!0,value:i})}:function(e,i){e.default=i}),g=this&&this.__decorate||function(e,i,o,d){var y=arguments.length,T=y<3?i:d===null?d=Object.getOwnPropertyDescriptor(i,o):d,w;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")T=Reflect.decorate(e,i,o,d);else for(var E=e.length-1;E>=0;E--)(w=e[E])&&(T=(y<3?w(T):y>3?w(i,o,T):w(i,o))||T);return y>3&&T&&Object.defineProperty(i,o,T),T},c=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var i={};if(e!=null)for(var o in e)o!=="default"&&Object.prototype.hasOwnProperty.call(e,o)&&S(i,e,o);return f(i,e),i},a=this&&this.__metadata||function(e,i){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(e,i)},v=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(p,"__esModule",{value:!0}),p.EdgingPluginConfig=p.EdgingProfileConfig=p.EdgingProfile=void 0;const s=l(856),b=l(126),j=v(l(17)),x=c(l(147)),m=v(l(245)),_=l(573),u=P((e,i)=>(o,d)=>{(0,s.SchemaProperty)({description:e.split(" ")[1],...i})(o,d),(0,s.PutOption)(d,e)(o,d)},"ProfileProperty");class t{constructor(){this.previousText=""}getBot(i){return this.from?i.find(o=>o.sid===this.from):this.session.bot}isOvertime(){return(0,m.default)().diff(this.startTime,"seconds")>this.duration}getTarget(){return this.to?this.to:this.session.userId}async loadModel(i){const o=j.default.join(i,`${this.model}.txt`),d=await x.promises.readFile(o,{encoding:this.encoding});this.texts=d.split(/\r?\n/).map(y=>y.trim()).filter(y=>y.length>0)}getUsePics(){return this.usePics&&!this.noUsePics}attachText(i){return`${b.Random.pick(this.prefixes)}${i}${b.Random.pick(this.suffixes)}`}getText(){const i=this.texts.length>1?this.texts.filter(d=>d!==this.previousText):this.texts,o=b.Random.pick(i);return this.previousText=o,this.attachText(o)}isFail(i){return this.failTriggerWord&&i.startsWith(this.failTriggerWord)}async initialize(i,o){if(Object.keys(i.defaultProfile).forEach(d=>{!this[d]&&i.defaultProfile[d]&&(this[d]=i.defaultProfile[d])}),!this.model)return"\u6CA1\u6709\u6307\u5B9A\u6A21\u677F\u5462\u3002";if(this.bot=this.getBot(o),!this.bot)return`\u6CA1\u6709\u627E\u5230\u966A\u7740\u73A9\u7684 ${this.from} \u5462\u3002`;try{await this.loadModel(i.modelRoot)}catch(d){return`\u6A21\u677F\u4F3C\u4E4E\u96BE\u4EA7\u4E86: ${d.message}`}this.startTime=(0,m.default)()}send(i){const o=this.bot;return(0,_.sendPriv)(o,this.getTarget(),i)}process(){return this.send(this.getText())}fail(){if(!!this.failText)return this.send(this.attachText(this.failText))}success(){if(!!this.successText)return this.send(this.attachText(this.successText))}describe(){return`\u6211\u5728\u548C ${this.getTarget()} \u4F7F\u7528\u6A21\u677F ${this.model} \u73A9\u800D\u3002
`}}P(t,"EdgingProfile"),g([(0,s.PutSession)(),a("design:type",b.Session)],t.prototype,"session",void 0),g([u("-b <from> \u6765\u6E90\u3002"),a("design:type",String)],t.prototype,"from",void 0),g([u("-u <to> \u76EE\u6807\u3002"),a("design:type",String)],t.prototype,"to",void 0),g([u("-d <duration> \u65F6\u95F4\uFF0C\u5355\u4F4D\u79D2\u3002",{default:600}),a("design:type",Number)],t.prototype,"duration",void 0),g([u("-m <model> \u4F7F\u7528\u6A21\u677F\u3002"),a("design:type",String)],t.prototype,"model",void 0),g([u("--encoding <encoding> \u6587\u4EF6\u7F16\u7801\u3002",{type:String,default:"utf8"}),a("design:type",String)],t.prototype,"encoding",void 0),g([(0,s.SchemaProperty)({type:String,default:[""],description:"\u968F\u673A\u524D\u7F00\u3002"}),a("design:type",Array)],t.prototype,"prefixes",void 0),g([(0,s.SchemaProperty)({type:String,default:[""],description:"\u968F\u673A\u540E\u7F00\u3002"}),a("design:type",Array)],t.prototype,"suffixes",void 0),g([u("-p \u4F7F\u7528\u56FE\u7247\u3002"),a("design:type",Boolean)],t.prototype,"usePics",void 0),g([u("-n \u4E0D\u4F7F\u7528\u56FE\u7247\u3002"),a("design:type",Boolean)],t.prototype,"noUsePics",void 0),g([u("--fail-text <tags> \u8D25\u5317\u65F6\u7684\u6D88\u606F\u3002"),a("design:type",String)],t.prototype,"failText",void 0),g([u("--success-text <tags> \u6210\u529F\u65F6\u7684\u6D88\u606F\u3002"),a("design:type",String)],t.prototype,"successText",void 0),g([u("--fail-trigger-word <word> \u8D25\u5317\u68C0\u6D4B\u8BCD\u3002"),a("design:type",String)],t.prototype,"failTriggerWord",void 0),p.EdgingProfile=t;let r=P(class extends t{},"EdgingProfileConfig");r=g([(0,s.RegisterSchema)()],r),p.EdgingProfileConfig=r;let n=P(class{constructor(i){}},"EdgingPluginConfig");g([(0,s.SchemaProperty)({description:"\u6A21\u677F\u6587\u4EF6\u5B58\u653E\u8DEF\u5F84\u3002",default:"./models"}),a("design:type",String)],n.prototype,"modelRoot",void 0),g([(0,s.SchemaProperty)({description:"\u56FE\u7247\u6807\u7B7E\u3002",type:String}),a("design:type",Array)],n.prototype,"picTags",void 0),g([(0,s.SchemaProperty)({description:"\u56FE\u6E90\u6807\u7B7E\u3002",type:String}),a("design:type",Array)],n.prototype,"sourceTags",void 0),g([(0,s.SchemaProperty)({description:"\u9ED8\u8BA4\u914D\u7F6E\u3002"}),a("design:type",r)],n.prototype,"defaultProfile",void 0),g([(0,s.SchemaProperty)({description:"\u5141\u8BB8\u8FDB\u884C\u914D\u7F6E\u7684\u9009\u62E9\u5668\u3002",type:"object",default:{}}),a("design:type",Object)],n.prototype,"panelSelection",void 0),g([(0,s.SchemaProperty)({description:"\u6700\u5C0F\u6587\u672C\u95F4\u9694\u3002",default:1e3}),a("design:type",Number)],n.prototype,"minTextInterval",void 0),g([(0,s.SchemaProperty)({description:"\u6700\u5927\u6587\u672C\u95F4\u9694\u3002",default:5e4}),a("design:type",Number)],n.prototype,"maxTextInterval",void 0),g([(0,s.SchemaProperty)({description:"\u6700\u5C0F\u56FE\u7247\u95F4\u9694\u3002",default:5e3}),a("design:type",Number)],n.prototype,"minPicInterval",void 0),g([(0,s.SchemaProperty)({description:"\u6700\u5927\u56FE\u7247\u95F4\u9694\u3002",default:5e4}),a("design:type",Number)],n.prototype,"maxPicInterval",void 0),n=g([(0,s.RegisterSchema)(),a("design:paramtypes",[Object])],n),p.EdgingPluginConfig=n},607:function(h,p,l){var S=this&&this.__createBinding||(Object.create?function(u,t,r,n){n===void 0&&(n=r);var e=Object.getOwnPropertyDescriptor(t,r);(!e||("get"in e?!t.__esModule:e.writable||e.configurable))&&(e={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(u,n,e)}:function(u,t,r,n){n===void 0&&(n=r),u[n]=t[r]}),f=this&&this.__decorate||function(u,t,r,n){var e=arguments.length,i=e<3?t:n===null?n=Object.getOwnPropertyDescriptor(t,r):n,o;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")i=Reflect.decorate(u,t,r,n);else for(var d=u.length-1;d>=0;d--)(o=u[d])&&(i=(e<3?o(i):e>3?o(t,r,i):o(t,r))||i);return e>3&&i&&Object.defineProperty(t,r,i),i},g=this&&this.__exportStar||function(u,t){for(var r in u)r!=="default"&&!Object.prototype.hasOwnProperty.call(t,r)&&S(t,u,r)},c=this&&this.__metadata||function(u,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(u,t)},a=this&&this.__param||function(u,t){return function(r,n){t(r,n,u)}};Object.defineProperty(p,"__esModule",{value:!0}),p.EdgingPanel=void 0;const v=l(913),s=l(856),b=l(126),j=l(573);g(l(913),p);class x{getUserId(){return this.userId||this.operatorId}}P(x,"UserDef"),f([(0,s.PutUserId)(),c("design:type",String)],x.prototype,"operatorId",void 0),f([(0,s.PutOption)("userId","-u <userId> \u76EE\u6807\u7528\u6237 ID\u3002"),c("design:type",String)],x.prototype,"userId",void 0);let m=P(class{edgingCommand(){}start(t){return this.main.start(t)}stop(t){return this.main.stop(t.getUserId())}finish(t){return this.main.finish(t.getUserId())}status(t){return this.main.status(t)}},"EdgingPanel");f([(0,s.Inject)("edging"),c("design:type",Object)],m.prototype,"main",void 0),f([(0,s.UseCommand)("edging","\u5BF8\u6B62\u7684\u914D\u7F6E\u3002",{empty:!0}),c("design:type",Function),c("design:paramtypes",[]),c("design:returntype",void 0)],m.prototype,"edgingCommand",null),f([(0,s.UseCommand)("edging.start","Start edging."),a(0,(0,s.PutObject)()),c("design:type",Function),c("design:paramtypes",[v.EdgingProfile]),c("design:returntype",void 0)],m.prototype,"start",null),f([(0,s.UseCommand)("edging.stop","Stop edging."),a(0,(0,s.PutObject)()),c("design:type",Function),c("design:paramtypes",[x]),c("design:returntype",void 0)],m.prototype,"stop",null),f([(0,s.UseCommand)("edging.finish","Finish edging."),a(0,(0,s.PutObject)()),c("design:type",Function),c("design:paramtypes",[x]),c("design:returntype",void 0)],m.prototype,"finish",null),f([(0,s.UseCommand)("edging.status","Show edging status."),a(0,(0,s.PutBot)()),c("design:type",Function),c("design:paramtypes",[b.Bot]),c("design:returntype",void 0)],m.prototype,"status",null),m=f([(0,s.DefinePlugin)()],m),p.EdgingPanel=m;let _=P(class extends(0,s.StarterPlugin)(v.EdgingPluginConfig){constructor(){super(...arguments),this.profiles=new Map}onApply(){(0,s.selectContext)(this.ctx,this.config.panelSelection).plugin(m),(0,j.dynamicInterval)(this.ctx,this.wrapTry(()=>this.processText(),"process text"),this.config.minTextInterval,this.config.maxTextInterval),(0,j.dynamicInterval)(this.ctx,this.wrapTry(()=>this.processPics(),"process pic"),this.config.minPicInterval,this.config.maxPicInterval)}endPlay(t){this.logger.info(`Play from ${t.bot.sid} to ${t.getTarget()} finished.`),this.profiles.delete(t.getTarget())}async checkOvertime(t){return t.isOvertime()?(this.endPlay(t),await t.success(),!0):!1}async runForEachProfile(t,r="run",n=Array.from(this.profiles.values())){return Promise.all(n.map(e=>this.wrapTry(t,`${r} for profile ${e.getTarget()}`)(e)))}async processText(){return this.logger.info("Processing text..."),this.runForEachProfile(async t=>{if(this.logger.info(`Processing text for profile ${t.getTarget()}...`),!await this.checkOvertime(t))return t.process()},"process text")}async processPics(){if(!this.pics)return;const t=Array.from(this.profiles.values()).filter(e=>e.getUsePics());if(!t.length)return;this.logger.info("Processing pics...");const r=await this.pics.randomPic(this.config.picTags,this.config.sourceTags);if(this.logger.info(`Get pic ${r.url}`),!r){this.logger.warn("No pic found.");return}const n=await this.pics.getSegment(r.url);return this.runForEachProfile(async e=>{if(this.logger.info(`Processing pic for profile ${e.getTarget()}...`),!await this.checkOvertime(e))return e.send(n.toString())},"process pic",t)}async checkFinish(t,r){const n=this.profiles.get(t.userId);if(!n||t.bot!==n.bot)return r();if(n.isFail(t.content)){await n.fail(),this.endPlay(n);return}return r()}wrapTry(t,r="perform action"){return async(...n)=>{try{return await t(...n)}catch(e){this.logger.error(`Failed to ${r}: ${e.message}`)}}}async start(t){if(this.profiles.has(t.getTarget()))return`${t.getTarget()} \u5DF2\u7ECF\u5728\u73A9\u800D\u4E86\u3002`;const r=await t.initialize(this.config,this.bots);return r||(this.profiles.set(t.getTarget(),t),this.logger.info(`Started play from ${t.bot.sid} to ${t.getTarget()}`),"\u51C6\u5907\u597D\u73A9\u800D\u4E86\u5462\u3002")}async stop(t){const r=this.profiles.get(t);return r?(this.endPlay(r),"\u505C\u6B62\u73A9\u800D\u4E86\u5462\u3002"):`${t} \u6CA1\u6709\u5728\u73A9\u800D\u3002`}async finish(t){const r=this.profiles.get(t);return r?(this.endPlay(r),await r.fail(),"\u73A9\u574F\u4E86\u5462\u3002"):`${t} \u6CA1\u6709\u5728\u73A9\u800D\u3002`}status(t){const r=Array.from(this.profiles.values()).filter(n=>n.bot===t);return`${r.map(n=>n.describe())}\u6211\u4E00\u5171\u5728\u548C ${r.length} \u4E2A\u4EBA\u73A9\u800D\u3002`}},"EdgingPlugin");f([(0,s.Inject)(),c("design:type",Array)],_.prototype,"bots",void 0),f([(0,s.Inject)(),c("design:type",Function)],_.prototype,"pics",void 0),f([(0,s.InjectLogger)(),c("design:type",b.Logger)],_.prototype,"logger",void 0),f([(0,s.OnPrivate)(),(0,s.UseMiddleware)(),c("design:type",Function),c("design:paramtypes",[b.Session,Function]),c("design:returntype",Promise)],_.prototype,"checkFinish",null),_=f([(0,s.Provide)("edging",{immediate:!0}),(0,s.DefinePlugin)({name:"edging",schema:v.EdgingPluginConfig})],_),p.default=_},573:(h,p,l)=>{Object.defineProperty(p,"__esModule",{value:!0}),p.dynamicInterval=p.sendPriv=void 0;const S=l(126);function f(c,a,v){return c.sendPrivateMessage?c.sendPrivateMessage(a,v):c.sendMessage(`@${a}`,v)}P(f,"sendPriv"),p.sendPriv=f;function g(c,a,v,s){return c.setTimeout(()=>{a(),g(c,a,v,s)},S.Random.int(v,s))}P(g,"dynamicInterval"),p.dynamicInterval=g},126:h=>{h.exports=require("koishi")},856:h=>{h.exports=require("koishi-thirdeye")},245:h=>{h.exports=require("moment")},147:h=>{h.exports=require("fs")},17:h=>{h.exports=require("path")}},O={};function M(h){var p=O[h];if(p!==void 0)return p.exports;var l=O[h]={exports:{}};return I[h].call(l.exports,l,l.exports,M),l.exports}P(M,"__webpack_require__");var $=M(607),R=exports;for(var C in $)R[C]=$[C];$.__esModule&&Object.defineProperty(R,"__esModule",{value:!0})})();
//# sourceMappingURL=index.js.map