UNPKG

koishi-plugin-greeting

Version:

Koishi plugin for automatically greeting at specific time.

3 lines (2 loc) 8.76 kB
var H=Object.defineProperty;var D=(O,P)=>H(O,"name",{value:P,configurable:!0});(()=>{"use strict";var O={913:function(l,u,p){var c=this&&this.__decorate||function(o,e,a,_){var m=arguments.length,g=m<3?e:_===null?_=Object.getOwnPropertyDescriptor(e,a):_,v;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")g=Reflect.decorate(o,e,a,_);else for(var S=o.length-1;S>=0;S--)(v=o[S])&&(g=(m<3?v(g):m>3?v(e,a,g):v(e,a))||g);return m>3&&g&&Object.defineProperty(e,a,g),g},s=this&&this.__metadata||function(o,e){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(o,e)},R=this&&this.__importDefault||function(o){return o&&o.__esModule?o:{default:o}};Object.defineProperty(u,"__esModule",{value:!0}),u.GreetingPluginConfig=u.Instance=u.Target=u.Rule=u.Pattern=u.Word=void 0;const i=p(856),T=R(p(517)),j=p(126),y=R(p(245)),Y=p(113),b=new j.Logger("greeting");function w(o,e){for(const a of e)a?.salt&&o.update(a.salt);return o}D(w,"updateSalt");function I(o){const e=w((0,Y.createHash)("md5"),o);return parseInt(e.digest("hex"),16)%4294967295}D(I,"hashRandom");let r=D(class{constructor(e){}formWord(){return`${this.word}${(this.patterns||[]).map(e=>e.pickWord()).join("")}`}toEntry(){return[this.formWord(),this.weight||1]}},"Word");c([(0,i.DefineSchema)({description:"\u6587\u672C\u3002",default:""}),s("design:type",String)],r.prototype,"word",void 0),c([(0,i.DefineSchema)({description:"\u6743\u91CD\u3002",default:1}),s("design:type",Number)],r.prototype,"weight",void 0),c([(0,i.DefineSchema)({description:"\u5B50\u8BED\u6BB5\u3002",type:(0,i.SchemaRef)(()=>t),default:[]}),s("design:type",Array)],r.prototype,"patterns",void 0),r=c([(0,i.RegisterSchema)(),s("design:paramtypes",[Object])],r),u.Word=r;let t=D(class{constructor(e){}pickWord(){return j.Random.weightedPick(Object.fromEntries(this.words.map(e=>e.toEntry())))}},"Pattern");c([(0,i.DefineSchema)({description:"\u8BCD\u8BED\u3002\u8BE5\u8BED\u6BB5\u4E2D\u4F1A\u968F\u673A\u60AC\u7740\u5176\u4E2D1\u79CD\u8BCD\u8BED\u51FA\u73B0\u3002",type:(0,i.SchemaRef)(()=>r)}),s("design:type",Array)],t.prototype,"words",void 0),t=c([(0,i.RegisterSchema)(),s("design:paramtypes",[Object])],t),u.Pattern=t;let n=D(class{constructor(e){}getTimeRange(){const e=(0,y.default)().format("YYYY-MM-DD"),a=(0,y.default)(`${e} ${this.fromTime}`),_=(0,y.default)(`${e} ${this.toTime||this.fromTime}`);return[a,_]}shouldSend(e){const a=(0,y.default)(),[_,m]=this.getTimeRange(),g=Math.floor(_.unix()/60),v=Math.floor(m.unix()/60),S=Math.floor(a.unix()/60);if(S<g||S>v)return!1;let M;if(g===v)M=g;else{const A=v-g+1,B=I([this,e,{salt:a.format("YYYY-MM-DD")}])%A;M=g+B}const C=y.default.unix(M*60);return b.debug(`${a.format("YYYY-MM-DD HH:mm")}: Should send ${this.generateText()} to ${e.getDescription()} at ${C.format("YYYY-MM-DD HH:mm")}`),S===M}generateText(){return this.patterns.map(e=>e.pickWord()).join("")}},"Rule");c([(0,i.DefineSchema)({description:"\u6700\u65E9\u51FA\u73B0\u7684\u65F6\u95F4\u3002",default:"00:00"}),s("design:type",String)],n.prototype,"fromTime",void 0),c([(0,i.DefineSchema)({description:"\u6700\u665A\u51FA\u73B0\u7684\u65F6\u95F4\u3002\u9ED8\u8BA4\u4E3A\u4E0E fromTime \u76F8\u540C\u3002"}),s("design:type",String)],n.prototype,"toTime",void 0),c([(0,i.DefineSchema)({description:"\u968F\u673A\u65F6\u95F4\u76D0\u503C\u3002"}),s("design:type",String)],n.prototype,"salt",void 0),c([(0,i.DefineSchema)({description:"\u8BED\u6BB5\u3002",type:t}),s("design:type",Array)],n.prototype,"patterns",void 0),n=c([(0,i.RegisterSchema)(),s("design:paramtypes",[Object])],n),u.Rule=n;let d=D(class{constructor(e){}getDescription(){return this.userId?`User ${this.userId}`:this.channelId?`Channel ${this.channelId}`:this.guildId?`Group ${this.guildId}`:"Unknown target"}get salt(){return this.userId||this.channelId||this.guildId}set salt(e){}send(e,a){return this.userId?e.sendPrivateMessage(this.userId,a):e.sendMessage(this.channelId,a,this.guildId)}},"Target");c([(0,i.DefineSchema)({description:"\u79C1\u804A\u7528\u6237 ID\u3002\u82E5\u5B58\u5728\u6B64\u9879\u5219\u4E3A\u79C1\u804A\u3002"}),s("design:type",String)],d.prototype,"userId",void 0),c([(0,i.DefineSchema)({description:"\u9891\u9053 ID\u3002"}),s("design:type",String)],d.prototype,"channelId",void 0),c([(0,i.DefineSchema)({description:"\u7FA4\u7EC4 ID\u3002"}),s("design:type",String)],d.prototype,"guildId",void 0),d=c([(0,i.RegisterSchema)(),s("design:paramtypes",[Object])],d),u.Target=d;let f=D(class{constructor(e){}getBot(e){return e.find(a=>a.sid===this.from)}async run(e){const a=this.getBot(e);if(a?.status!=="online")return b.info(`Skipping job for ${this.from} because it's not online.`),this;b.debug(`${(0,y.default)().format("YYYY-MM-DD HH:mm")}: Running job for ${this.from}...`);const _=[];for(const m of this.to)for(const g of this.rules)if(g.shouldSend(m)){const v=g.generateText();b.info(`Sending from ${this.from} to ${m.getDescription()}: ${v}`),_.push(m.send(a,v).catch(S=>{b.error(`Failed to send from ${this.from} to ${m.getDescription()}: ${S.message}`)}))}}},"Instance");c([(0,i.DefineSchema)({description:"\u673A\u5668\u4EBA ID\uFF0C\u5E94\u5199\u6210 platform:id \u7684\u5F62\u5F0F\u3002\u4F8B\u5982 onebot:111111111\u3002",required:!0}),s("design:type",String)],f.prototype,"from",void 0),c([(0,i.DefineSchema)({description:"\u76EE\u6807\u5217\u8868\u3002",type:d}),s("design:type",Array)],f.prototype,"to",void 0),c([(0,i.DefineSchema)({description:"\u89C4\u5219\u5217\u8868\u3002",type:n}),s("design:type",Array)],f.prototype,"rules",void 0),f=c([(0,i.RegisterSchema)(),s("design:paramtypes",[Object])],f),u.Instance=f;let h=D(class{constructor(e){}async runInstances(e,a=this.instances){return T.default.compact(await Promise.all(a.map(m=>m.run(e))))}},"GreetingPluginConfig");c([(0,i.DefineSchema)({description:"\u5B9E\u4F8B\u5217\u8868\u3002",type:f}),s("design:type",Array)],h.prototype,"instances",void 0),h=c([(0,i.RegisterSchema)(),s("design:paramtypes",[Object])],h),u.GreetingPluginConfig=h},607:function(l,u,p){var c=this&&this.__createBinding||(Object.create?function(r,t,n,d){d===void 0&&(d=n);var f=Object.getOwnPropertyDescriptor(t,n);(!f||("get"in f?!t.__esModule:f.writable||f.configurable))&&(f={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(r,d,f)}:function(r,t,n,d){d===void 0&&(d=n),r[d]=t[n]}),s=this&&this.__decorate||function(r,t,n,d){var f=arguments.length,h=f<3?t:d===null?d=Object.getOwnPropertyDescriptor(t,n):d,o;if(typeof Reflect=="object"&&typeof Reflect.decorate=="function")h=Reflect.decorate(r,t,n,d);else for(var e=r.length-1;e>=0;e--)(o=r[e])&&(h=(f<3?o(h):f>3?o(t,n,h):o(t,n))||h);return f>3&&h&&Object.defineProperty(t,n,h),h},R=this&&this.__exportStar||function(r,t){for(var n in r)n!=="default"&&!Object.prototype.hasOwnProperty.call(t,n)&&c(t,r,n)},i=this&&this.__metadata||function(r,t){if(typeof Reflect=="object"&&typeof Reflect.metadata=="function")return Reflect.metadata(r,t)},T=this&&this.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};Object.defineProperty(u,"__esModule",{value:!0});const j=p(913),y=p(856),Y=p(126),b=p(344),w=T(p(517));R(p(913),u);let I=D(class extends(0,y.StarterPlugin)(j.GreetingPluginConfig){constructor(){super(...arguments),this.pendingInstances=new Map}addPending(t){const n=this.pendingInstances.get(t.from)||[];n.push(t),this.pendingInstances.set(t.from,n)}async onBotStatusUpdated(t){t.status==="online"&&await this.resolvePending(t.sid)}async resolvePending(t){const n=this.pendingInstances.get(t);if(!!n)return this.pendingInstances.delete(t),this.run(w.default.uniq(n))}async run(t){t||this.pendingInstances.clear();const n=await this.config.runInstances(this.bots,t);for(const d of n)this.addPending(d)}onConnect(){this.job=(0,b.scheduleJob)("0 * * * * *",()=>this.run()),this.job.invoke()}onDisconnect(){this.job&&this.job.cancel()}},"GreetingPlugin");s([(0,y.Inject)(),i("design:type",Array)],I.prototype,"bots",void 0),s([(0,y.UseEvent)("bot-status-updated"),i("design:type",Function),i("design:paramtypes",[Y.Bot]),i("design:returntype",Promise)],I.prototype,"onBotStatusUpdated",null),I=s([(0,y.DefinePlugin)({name:"greeting",schema:j.GreetingPluginConfig})],I),u.default=I},126:l=>{l.exports=require("koishi")},856:l=>{l.exports=require("koishi-thirdeye")},517:l=>{l.exports=require("lodash")},245:l=>{l.exports=require("moment")},344:l=>{l.exports=require("node-schedule")},113:l=>{l.exports=require("crypto")}},P={};function x(l){var u=P[l];if(u!==void 0)return u.exports;var p=P[l]={exports:{}};return O[l].call(p.exports,p,p.exports,x),p.exports}D(x,"__webpack_require__");var $=x(607),G=exports;for(var W in $)G[W]=$[W];$.__esModule&&Object.defineProperty(G,"__esModule",{value:!0})})(); //# sourceMappingURL=index.js.map