UNPKG

koishi-plugin-thesaurus

Version:

Koishi.js chat plugin for https://github.com/Kyomotoi/AnimeThesaurus

3 lines (2 loc) 6.65 kB
var F=Object.defineProperty;var D=(S,R)=>F(S,"name",{value:R,configurable:!0});(()=>{"use strict";var S={913:function(o,h,u){var y=this&&this.__decorate||function(m,s,g,P){var l=arguments.length,f=l<3?s:P===null?P=Object.getOwnPropertyDescriptor(s,g):P,b;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")f=Reflect.decorate(m,s,g,P);else for(var w=m.length-1;w>=0;w--)(b=m[w])&&(f=(l<3?b(f):l>3?b(s,g,f):b(s,g))||f);return l>3&&f&&Object.defineProperty(s,g,f),f},v=this&&this.__metadata||function(m,s){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(m,s)};Object.defineProperty(h,"__esModule",{value:!0}),h.ThesaurusPluginConfig=void 0;const d=u(856);let _=D(class{constructor(s){}},"ThesaurusPluginConfig");y([(0,d.DefineSchema)({description:"\u8BCD\u5E93\u672C\u5730\u8DEF\u5F84\u6216 URL\u3002",default:"https://code.mycard.moe/3rdeye/AnimeThesaurus/-/raw/main/data.json"}),v("design:type",String)],_.prototype,"path",void 0),y([(0,d.DefineSchema)({description:"\u804A\u5929\u8D85\u65F6\u65F6\u95F4 ms\u3002",default:6e5,role:"ms"}),v("design:type",Number)],_.prototype,"chatTimeout",void 0),y([(0,d.DefineSchema)({description:"\u8FFD\u6EAF\u4F1A\u8BDD\u957F\u5EA6",default:10}),v("design:type",Number)],_.prototype,"trackingLength",void 0),_=y([(0,d.RegisterSchema)(),v("design:paramtypes",[Object])],_),h.ThesaurusPluginConfig=_},607:function(o,h,u){var y=this&&this.__createBinding||(Object.create?function(r,t,e,a){a===void 0&&(a=e);var i=Object.getOwnPropertyDescriptor(t,e);(!i||("get"in i?!t.__esModule:i.writable||i.configurable))&&(i={enumerable:!0,get:function(){return t[e]}}),Object.defineProperty(r,a,i)}:function(r,t,e,a){a===void 0&&(a=e),r[a]=t[e]}),v=this&&this.__setModuleDefault||(Object.create?function(r,t){Object.defineProperty(r,"default",{enumerable:!0,value:t})}:function(r,t){r.default=t}),d=this&&this.__decorate||function(r,t,e,a){var i=arguments.length,n=i<3?t:a===null?a=Object.getOwnPropertyDescriptor(t,e):a,c;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")n=Reflect.decorate(r,t,e,a);else for(var C=r.length-1;C>=0;C--)(c=r[C])&&(n=(i<3?c(n):i>3?c(t,e,n):c(t,e))||n);return i>3&&n&&Object.defineProperty(t,e,n),n},_=this&&this.__importStar||function(r){if(r&&r.__esModule)return r;var t={};if(r!=null)for(var e in r)e!=="default"&&Object.prototype.hasOwnProperty.call(r,e)&&y(t,r,e);return v(t,r),t},m=this&&this.__exportStar||function(r,t){for(var e in r)e!=="default"&&!Object.prototype.hasOwnProperty.call(t,e)&&y(t,r,e)},s=this&&this.__metadata||function(r,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(r,t)},g=this&&this.__param||function(r,t){return function(e,a){t(e,a,r)}},P=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(h,"__esModule",{value:!0});const l=u(126),f=u(913),b=u(147),w=P(u(517)),p=u(856),T=_(u(867));class j{}D(j,"ChatSession"),d([(0,T.CacheKey)(),s("design:type",String)],j.prototype,"sessionId",void 0),m(u(913),h);let O=D(class extends(0,p.StarterPlugin)(f.ThesaurusPluginConfig){constructor(){super(...arguments),this.wordData={}}async loadWords(){const t=this.config.path;try{t.match(/^https?:\/\//)?this.wordData=await this.ctx.http.get(t):this.wordData=JSON.parse(await b.promises.readFile(t,"utf-8")),this.ctx.logger("thesaurus").info(`Loaded ${Object.keys(this.wordData).length} patterns from source ${t} .`)}catch(e){return this.ctx.logger("thesaurus").error(`Failed to load words from source ${t}: ${e.toString()}`),await(0,l.sleep)(5e3),this.loadWords()}}async getFromCache(t){if(!!t)return this.aragami.get(j,t)}async setToCache(t){return this.aragami.set(j,t)}async clearCacheOf(t){return this.aragami.del(j,t)}async replyWith(t,e){if(!!t)return e.playedWords.push(t),e.playedWords.length>this.config.trackingLength&&e.playedWords.shift(),await this.setToCache(e),t.replace(/你/g,e.name).trim()}tryPatterns(t,e){if(!t.length)return;const a=l.Random.pick(t),i=this.wordData[a].filter(n=>!e.has(n));if(!i.length){const n=t.filter(c=>c!==a);return this.tryPatterns(n,e)}return l.Random.pick(i)}async replyChat(t,e){const a=Object.keys(this.wordData).filter(n=>t.includes(n));if(!a.length){const n=w.default.flatten(Object.values(this.wordData));let c=n.filter(C=>!e.playedWords.includes(C));return c.length||(c=n),this.replyWith(l.Random.pick(c),e)}const i=this.tryPatterns(a,new Set(e.playedWords))||this.tryPatterns(a,new Set);return this.replyWith(i,e)}formatName(t){return t.match(/^[a-zA-Z0-9]/)&&(t=` ${t}`),t.match(/[a-zA-Z0-9]$/)&&(t=`${t} `),t}async replyMessage(t,e){const a=`${t.platform}.${t.selfId}.${t.userId}`,i=await this.getFromCache(a);return i?t.content==="quit"?(await this.clearCacheOf(a),"\u8BB0\u5F97\u4E0B\u6B21\u518D\u627E\u6211\u804A\u5594~"):await this.replyChat(t.content,i):e()}async startChat(t,e,a){const i=`${a.platform}.${a.selfId}.${a.userId}`,n=t||e,c=this.formatName(n);return await this.setToCache({sessionId:i,name:c,playedWords:[]}),`${c}\uFF0C\u5F00\u59CB\u548C\u6211\u804A\u5929\u5427\u3002\u804A\u7D2F\u4E86\u7684\u8BDD\uFF0C\u8F93\u5165 quit \u5C31\u53EF\u4EE5\u7ED3\u675F\u804A\u5929\u54E6\u3002`.trim()}async onConnect(){await this.loadWords()}},"ThesaurusPlugin");d([(0,p.Inject)(!0),s("design:type",T.default)],O.prototype,"aragami",void 0),d([(0,p.UseMiddleware)(),s("design:type",Function),s("design:paramtypes",[l.Session,Function]),s("design:returntype",Promise)],O.prototype,"replyMessage",null),d([(0,p.UseCommand)("startchat","\u5F00\u59CB\u804A\u5929"),(0,p.CommandUsage)("\u804A\u5929\u5F00\u59CB\u540E\uFF0C quit \u505C\u6B62\u804A\u5929\u3002"),(0,p.CommandExample)("startchat --name Shigma \u53EF\u4EE5\u4F7F\u7528 Shigma \u4F5C\u4E3A\u81EA\u5DF1\u7684\u540D\u5B57\u6765\u804A\u5929\u3002"),g(0,(0,p.PutOption)("name","--name <name:string>")),g(1,(0,p.PutUserName)()),g(2,(0,p.PutSession)()),s("design:type",Function),s("design:paramtypes",[String,String,l.Session]),s("design:returntype",Promise)],O.prototype,"startChat",null),O=d([(0,p.DefinePlugin)({name:"thesaurus",schema:f.ThesaurusPluginConfig})],O),h.default=O},126:o=>{o.exports=require("koishi")},867:o=>{o.exports=require("koishi-plugin-cache-aragami")},856:o=>{o.exports=require("koishi-thirdeye")},517:o=>{o.exports=require("lodash")},147:o=>{o.exports=require("fs")}},R={};function W(o){var h=R[o];if(h!==void 0)return h.exports;var u=R[o]={exports:{}};return S[o].call(u.exports,u,u.exports,W),u.exports}D(W,"__webpack_require__");var x=W(607),$=exports;for(var M in x)$[M]=x[M];x.__esModule&&Object.defineProperty($,"__esModule",{value:!0})})(); //# sourceMappingURL=index.js.map