Commit 16185c34 authored by nanahira's avatar nanahira

rework with multi instance

parent 34428473
......@@ -10,7 +10,6 @@ const app = new App({
port: 14514,
host: 'localhost',
prefix: '.',
});
// Console and sandbox
......
......@@ -9,8 +9,8 @@
"version": "2.0.2",
"license": "MIT",
"dependencies": {
"koishi-target-def": "^1.0.4",
"koishi-thirdeye": "^10.0.5",
"koishi-target-def": "^1.0.5",
"koishi-thirdeye": "^10.0.8",
"node-schedule": "^2.1.0"
},
"devDependencies": {
......@@ -1073,13 +1073,13 @@
}
},
"node_modules/@koishijs/core": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.1.tgz",
"integrity": "sha512-2HzdUi76YSLFZYpPXcmEwwVyHQLPF8BL0GJR6PuCYdOYfHykzwA5NmqOGx29EyeuYzs8sS6yr25IQktxMYqsdg==",
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.2.tgz",
"integrity": "sha512-GM0497Bd5BDPXuzJMppJik+Zb55bbLTX14aJmkFgPW9maU9oz3ZZRW+EeMpFK449fPG83P2UsIulJHNbD5L/hQ==",
"peer": true,
"dependencies": {
"@koishijs/utils": "^5.4.1",
"cosmotype": "^1.0.6",
"@koishijs/utils": "^5.4.2",
"cosmotype": "^1.0.9",
"fastest-levenshtein": "^1.0.12",
"ns-require": "^1.1.2"
},
......@@ -1160,9 +1160,9 @@
"peer": true
},
"node_modules/@koishijs/utils": {
"version": "5.4.1",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.1.tgz",
"integrity": "sha512-M3L9KqtdTyCzQK1LxTtp7QCucc75IA2vRwnYJA1gXMAxPC3SoEjUVmsk/oNFBSknmfADdViI5mFJtUXMCpJW1g==",
"version": "5.4.2",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.2.tgz",
"integrity": "sha512-cUwrqVQqTM/F2DZO9APEDlfz2mrUe5L7b2pYmhaTi8Pggk9kT78hvBx4fnLWrHiFV3BPuikghqZSfkladFcugw==",
"peer": true,
"dependencies": {
"@koishijs/segment": "^1.1.1",
......@@ -5272,14 +5272,14 @@
}
},
"node_modules/koishi": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.1.tgz",
"integrity": "sha512-9NtcXrIKLN8AG9zEf3GzweSiHQubew0AWfjaci0grIxy760T77D8iLVsDbVhzmGSnc26WOR2q3CMFdRmE2igVA==",
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.2.tgz",
"integrity": "sha512-vgjKxSJxAOsFwBnO097d8Oo8AC0jTvdfrsAHgu8u1d3DDJkHzUr8vQEjcJjvAp411Ev5VIvswFeSpeTVWDv6VA==",
"peer": true,
"dependencies": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.6.1",
"@koishijs/utils": "^5.4.1",
"@koishijs/core": "^4.6.2",
"@koishijs/utils": "^5.4.2",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^8.5.3",
......@@ -5338,21 +5338,21 @@
}
},
"node_modules/koishi-target-def": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/koishi-target-def/-/koishi-target-def-1.0.4.tgz",
"integrity": "sha512-LV8vEEP5AKCiO1U45Vk8fyEMuo/AMVA3kTjmXPUvNZaEzmKe9wO/h8SzYSDoyjMu8Fap1L5bU8WB93zerLOg1Q==",
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/koishi-target-def/-/koishi-target-def-1.0.5.tgz",
"integrity": "sha512-4H6HnlnE2I0uyCFuX2yYNGORHENelTSE+ZHgiGkpjzIawIMiC7YsV1nuy9yIpFU+DaG6bIFgkAnkoDKtDRkJ5A==",
"dependencies": {
"lodash": "^4.17.21"
},
"peerDependencies": {
"koishi": "^4.4.2",
"schemastery-gen": "^3.1.3"
"koishi": "^4.6.2",
"schemastery-gen": "^3.1.6"
}
},
"node_modules/koishi-thirdeye": {
"version": "10.0.5",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.5.tgz",
"integrity": "sha512-GzPNyJCnUNklHefi0vc0nArV+dEh1QuTddom9MQ6Sawa6nbjbnZNWKFY08eQxNR6CR87tYK93WtO+URwcBbEew==",
"version": "10.0.8",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.8.tgz",
"integrity": "sha512-osmkYVNzcv2T2ItKFVLvCYOPEyuVbyZ6jdG7uybA2lLt4Y8zCxWHZFhq0ck1UaahZs5oFIrCfpTSdjxDv83UcQ==",
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -5360,11 +5360,11 @@
"koishi-decorators": "^2.0.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.4",
"schemastery-gen": "^3.1.6",
"typed-reflector": "^1.0.10"
},
"peerDependencies": {
"koishi": "^4.6.0",
"koishi": "^4.6.1",
"schemastery": "^3.1.0"
}
},
......@@ -6518,16 +6518,16 @@
}
},
"node_modules/schemastery-gen": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.4.tgz",
"integrity": "sha512-zhM5A2p/SdPKe8ouObCYSOA3k5I1CpbOfSvv8DWp7kCbVnSgfOMSBCB4Yue3/D81+MAE2re93nSt7k/Tolz3mQ==",
"version": "3.1.6",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.6.tgz",
"integrity": "sha512-UiYsiuvj830k2IzznWx/+Ed9+j1+XwMZ1zfctpy6+rHe6Se0IrulBsh2XDmI2uDslFtd6HfjIi5BJksbVvXQ/w==",
"dependencies": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"typed-reflector": "^1.0.10"
},
"peerDependencies": {
"schemastery": "^3.3.2"
"schemastery": "^3.3.3"
}
},
"node_modules/semver": {
......@@ -8630,13 +8630,13 @@
}
},
"@koishijs/core": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.1.tgz",
"integrity": "sha512-2HzdUi76YSLFZYpPXcmEwwVyHQLPF8BL0GJR6PuCYdOYfHykzwA5NmqOGx29EyeuYzs8sS6yr25IQktxMYqsdg==",
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.6.2.tgz",
"integrity": "sha512-GM0497Bd5BDPXuzJMppJik+Zb55bbLTX14aJmkFgPW9maU9oz3ZZRW+EeMpFK449fPG83P2UsIulJHNbD5L/hQ==",
"peer": true,
"requires": {
"@koishijs/utils": "^5.4.1",
"cosmotype": "^1.0.6",
"@koishijs/utils": "^5.4.2",
"cosmotype": "^1.0.9",
"fastest-levenshtein": "^1.0.12",
"ns-require": "^1.1.2"
}
......@@ -8701,9 +8701,9 @@
"peer": true
},
"@koishijs/utils": {
"version": "5.4.1",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.1.tgz",
"integrity": "sha512-M3L9KqtdTyCzQK1LxTtp7QCucc75IA2vRwnYJA1gXMAxPC3SoEjUVmsk/oNFBSknmfADdViI5mFJtUXMCpJW1g==",
"version": "5.4.2",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.4.2.tgz",
"integrity": "sha512-cUwrqVQqTM/F2DZO9APEDlfz2mrUe5L7b2pYmhaTi8Pggk9kT78hvBx4fnLWrHiFV3BPuikghqZSfkladFcugw==",
"peer": true,
"requires": {
"@koishijs/segment": "^1.1.1",
......@@ -11879,14 +11879,14 @@
}
},
"koishi": {
"version": "4.6.1",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.1.tgz",
"integrity": "sha512-9NtcXrIKLN8AG9zEf3GzweSiHQubew0AWfjaci0grIxy760T77D8iLVsDbVhzmGSnc26WOR2q3CMFdRmE2igVA==",
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.6.2.tgz",
"integrity": "sha512-vgjKxSJxAOsFwBnO097d8Oo8AC0jTvdfrsAHgu8u1d3DDJkHzUr8vQEjcJjvAp411Ev5VIvswFeSpeTVWDv6VA==",
"peer": true,
"requires": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.6.1",
"@koishijs/utils": "^5.4.1",
"@koishijs/core": "^4.6.2",
"@koishijs/utils": "^5.4.2",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^8.5.3",
......@@ -11932,17 +11932,17 @@
}
},
"koishi-target-def": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/koishi-target-def/-/koishi-target-def-1.0.4.tgz",
"integrity": "sha512-LV8vEEP5AKCiO1U45Vk8fyEMuo/AMVA3kTjmXPUvNZaEzmKe9wO/h8SzYSDoyjMu8Fap1L5bU8WB93zerLOg1Q==",
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/koishi-target-def/-/koishi-target-def-1.0.5.tgz",
"integrity": "sha512-4H6HnlnE2I0uyCFuX2yYNGORHENelTSE+ZHgiGkpjzIawIMiC7YsV1nuy9yIpFU+DaG6bIFgkAnkoDKtDRkJ5A==",
"requires": {
"lodash": "^4.17.21"
}
},
"koishi-thirdeye": {
"version": "10.0.5",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.5.tgz",
"integrity": "sha512-GzPNyJCnUNklHefi0vc0nArV+dEh1QuTddom9MQ6Sawa6nbjbnZNWKFY08eQxNR6CR87tYK93WtO+URwcBbEew==",
"version": "10.0.8",
"resolved": "https://registry.npmjs.org/koishi-thirdeye/-/koishi-thirdeye-10.0.8.tgz",
"integrity": "sha512-osmkYVNzcv2T2ItKFVLvCYOPEyuVbyZ6jdG7uybA2lLt4Y8zCxWHZFhq0ck1UaahZs5oFIrCfpTSdjxDv83UcQ==",
"requires": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -11950,7 +11950,7 @@
"koishi-decorators": "^2.0.0",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.4",
"schemastery-gen": "^3.1.6",
"typed-reflector": "^1.0.10"
}
},
......@@ -12794,9 +12794,9 @@
}
},
"schemastery-gen": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.4.tgz",
"integrity": "sha512-zhM5A2p/SdPKe8ouObCYSOA3k5I1CpbOfSvv8DWp7kCbVnSgfOMSBCB4Yue3/D81+MAE2re93nSt7k/Tolz3mQ==",
"version": "3.1.6",
"resolved": "https://registry.npmjs.org/schemastery-gen/-/schemastery-gen-3.1.6.tgz",
"integrity": "sha512-UiYsiuvj830k2IzznWx/+Ed9+j1+XwMZ1zfctpy6+rHe6Se0IrulBsh2XDmI2uDslFtd6HfjIi5BJksbVvXQ/w==",
"requires": {
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
......
......@@ -44,8 +44,8 @@
"testEnvironment": "node"
},
"dependencies": {
"koishi-target-def": "^1.0.4",
"koishi-thirdeye": "^10.0.5",
"koishi-target-def": "^1.0.5",
"koishi-thirdeye": "^10.0.8",
"node-schedule": "^2.1.0"
},
"peerDependencies": {
......
......@@ -3,7 +3,7 @@ import { SendTarget } from 'koishi-target-def';
import { SchemaProperty, RegisterSchema } from 'koishi-thirdeye';
@RegisterSchema()
export class Instance {
export class AutoPicPluginConfig {
@SchemaProperty({ required: true })
cron: string;
......@@ -31,13 +31,3 @@ export class Instance {
})
targets: SendTarget[];
}
@RegisterSchema()
export class AutoPicPluginConfig {
constructor(config: AutoPicPluginConfigLike) {}
@SchemaProperty({ type: Instance })
instances: Instance[];
}
export type AutoPicPluginConfigLike = Partial<AutoPicPluginConfig>;
// import 'source-map-support/register';
import { AutoPicPluginConfig, Instance } from './config';
import { AutoPicPluginConfig } from './config';
import {
DefinePlugin,
BasePlugin,
Inject,
InjectLogger,
LifecycleEvents,
MultiInstancePlugin,
} from 'koishi-thirdeye';
import PicsContainer from 'koishi-plugin-pics';
import { Adapter, Logger, segment } from 'koishi';
import { Adapter, Logger } from 'koishi';
import { Job, scheduleJob } from 'node-schedule';
export * from './config';
@DefinePlugin({ name: 'autopic', schema: AutoPicPluginConfig })
export default class AutoPicPlugin
export class AutoPicInstancePlugin
extends BasePlugin<AutoPicPluginConfig>
implements LifecycleEvents
{
......@@ -26,19 +27,22 @@ export default class AutoPicPlugin
@InjectLogger()
private logger: Logger;
async runInstance(instance: Instance) {
const pic = await this.pics.randomPic(instance.tags, instance.sourceTags);
async run() {
const pic = await this.pics.randomPic(
this.config.tags,
this.config.sourceTags,
);
if (!pic) {
this.logger.warn(
`no pic found for ${instance.tags?.join(',') || '*'} from ${
instance.sourceTags?.join(',') || '*'
`no pic found for ${this.config.tags?.join(',') || '*'} from ${
this.config.sourceTags?.join(',') || '*'
}`,
);
return;
}
const message = await this.pics.getSegment(pic.url);
await Promise.all(
instance.targets.map(async (target) => {
this.config.targets.map(async (target) => {
try {
this.logger.debug(`Sending image ${pic.url} from ${target.bot}.`);
let targetMessage = message;
......@@ -60,19 +64,21 @@ export default class AutoPicPlugin
);
}
jobs: Job[] = [];
private job: Job;
onConnect() {
this.jobs = this.config.instances.map((instance) => {
if (instance.immediate) {
this.runInstance(instance).then();
}
return scheduleJob(instance.cron, () => this.runInstance(instance));
});
if (this.config.immediate) {
this.run().then();
}
this.job = scheduleJob(this.config.cron, () => this.run());
}
onDisconnect() {
this.jobs.forEach((job) => job.cancel());
this.jobs = [];
this.job.cancel();
}
}
@DefinePlugin()
export default class AutoPicPlugin extends MultiInstancePlugin(
AutoPicInstancePlugin,
) {}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment