Commit d21c4229 authored by nanahira's avatar nanahira

dev things

parent 3e057beb
import { Awaitable, Context } from 'koishi';
import { PicResult, PicSource } from '../src';
import { DefinePlugin } from 'koishi-thirdeye';
import { PicResult, PicSourceConfig, PicSourcePlugin } from '../src';
class TestPicsource extends PicSource {
constructor(ctx: Context, public name: string) {
super(ctx);
}
isDefault = true;
@DefinePlugin({ schema: PicSourceConfig })
class TestPicsource extends PicSourcePlugin {
randomPic(picTags: string[]): Awaitable<PicResult> {
return {
url: `https://cdn02.moecube.com:444/images/ygopro-images-${this.name}/${
......@@ -18,8 +16,7 @@ class TestPicsource extends PicSource {
export default class ExtrasInDev {
constructor(ctx: Context) {
ctx.pics.addSource(new TestPicsource(ctx, 'zh-CN'));
ctx.pics.addSource(new TestPicsource(ctx, 'en-US'));
ctx.plugin(TestPicsource, { name: 'zh-CN', isDefault: true });
}
static using = ['pics'] as const;
......
// import 'source-map-support/register';
import { SchemaProperty, RegisterSchema, SchemaClass } from 'koishi-thirdeye';
import {
SchemaProperty,
RegisterSchema,
SchemaClass,
ClassType,
} from 'koishi-thirdeye';
import { Quester } from 'koishi';
import { PicMiddleware } from './index';
import {
Instances,
PicMiddleware,
PicMiddlewareInfo,
PicSourceInfo,
} from './def';
@RegisterSchema()
export class PicsPluginConfig {
......@@ -26,14 +36,6 @@ export type PicsPluginConfigLike = Partial<PicsPluginConfig>;
// For convenience of plugins
export interface PicSourceInfo {
tags?: string[];
weight?: number;
name: string;
description?: string;
isDefault?: boolean;
}
export class PicSourceConfig implements PicSourceInfo {
constructor(config: Partial<PicSourceInfo>) {}
......@@ -60,11 +62,6 @@ export class PicSourceConfig implements PicSourceInfo {
export const PicSourceSchema = SchemaClass(PicSourceConfig);
export interface PicMiddlewareInfo {
name?: string;
prepend?: boolean;
}
export class PicMiddlewareConfig {
constructor(config: PicMiddlewareInfo) {}
@SchemaProperty({ description: '中间件名称。' })
......
import { Awaitable } from 'koishi';
import { ClassType, SchemaProperty } from 'koishi-thirdeye';
export interface PicSourceInfo {
tags?: string[];
weight?: number;
name: string;
description?: string;
isDefault?: boolean;
}
export interface PicResult {
url: string;
description?: string;
}
export type PicNext = (url?: string) => Awaitable<string>;
export interface PicMiddleware extends PicMiddlewareInfo {
use(url: string, next: PicNext): Awaitable<string>;
}
export interface PicMiddlewareInfo {
name?: string;
prepend?: boolean;
}
export interface Instances<T> {
instances: T[];
}
export function ToInstancesConfig<T>(
instanceConfig: ClassType<T>,
): new () => Instances<T> {
const instanceConfigClass = class InstancesConfig {
instances: T[];
};
SchemaProperty({ type: instanceConfig, default: [] })(
instanceConfigClass.prototype,
'instances',
);
return instanceConfigClass;
}
// import 'source-map-support/register';
import { Context, Awaitable, Random, Logger, Bot, remove } from 'koishi';
import { PicSourceConfig, PicSourceInfo, PicsPluginConfig } from './config';
import { Context, Random, Logger, Bot, remove } from 'koishi';
import { PicsPluginConfig } from './config';
import _ from 'lodash';
import { segment, Quester } from 'koishi';
import {
......@@ -8,18 +8,19 @@ import {
Caller,
DefinePlugin,
Inject,
InjectConfig,
InjectLogger,
LifecycleEvents,
PartialDeep,
Provide,
} from 'koishi-thirdeye';
import { AxiosRequestConfig } from 'axios';
import { PicAssetsTransformMiddleware } from './middlewares/assets';
import { PicDownloaderMiddleware } from './middlewares/download';
import { PicMiddleware, PicNext } from './middleware';
import { PicMiddleware, PicNext, PicResult } from './def';
import { PicSource } from './picsource';
export * from './config';
export * from './middleware';
export * from './picsource';
export * from './def';
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
......@@ -30,63 +31,6 @@ declare module 'koishi' {
}
}
export interface PicResult {
url: string;
description?: string;
}
export class PicSource implements PicSourceInfo {
constructor(protected ctx: Context) {}
tags: string[] = [];
weight = 1;
name = 'default';
description = '';
isDefault = false;
randomPic(picTags: string[]): Awaitable<PicResult> {
// For override
throw new Error(`Not implemented`);
}
onStartup(): Awaitable<void> {
return;
}
onShutdown(): Awaitable<void> {
return;
}
getDisplayString() {
let pattern = this.name;
if (this.tags.length) {
pattern += `\t标签: ${this.tags.join(',')}`;
}
if (this.description) {
pattern += `\t${this.description}`;
}
return pattern;
}
}
export class PicSourcePlugin<
C extends PicSourceConfig = PicSourceConfig,
> extends PicSource {
constructor(ctx: Context, config: PartialDeep<C>) {
super(ctx);
}
@InjectConfig()
protected config: C;
@Inject(true)
protected pics: PicsContainer;
@InjectLogger()
protected logger: Logger;
onApply() {
this.config.applyTo(this);
this.pics.addSource(this);
}
}
@Provide('pics', { immediate: true })
@DefinePlugin({ name: 'pics', schema: PicsPluginConfig })
export default class PicsContainer
......
import { Awaitable, Logger } from 'koishi';
import { PicMiddlewareConfig, PicMiddlewareInfo } from './config';
import { PicMiddlewareConfig } from './config';
import {
BasePlugin,
Inject,
......@@ -7,12 +7,7 @@ import {
LifecycleEvents,
} from 'koishi-thirdeye';
import PicsContainer from './index';
export type PicNext = (url?: string) => Awaitable<string>;
export interface PicMiddleware extends PicMiddlewareInfo {
use(url: string, next: PicNext): Awaitable<string>;
}
import { PicMiddleware, PicNext } from './def';
export class PicMiddlewareBase<
C extends PicMiddlewareConfig = PicMiddlewareConfig,
......
import { DefinePlugin, Inject } from 'koishi-thirdeye';
import { Assets } from 'koishi';
import { PicMiddlewareBase, PicNext } from '../middleware';
import { PicMiddlewareBase } from '../middleware';
import { PicMiddlewareConfig } from '../config';
import { PicNext } from '../def';
@DefinePlugin({ schema: PicMiddlewareConfig })
export class PicAssetsTransformMiddleware extends PicMiddlewareBase {
......
import { DefinePlugin } from 'koishi-thirdeye';
import { PicMiddlewareBase, PicNext } from '../middleware';
import { PicMiddlewareBase } from '../middleware';
import { PicMiddlewareConfig } from '../config';
import { PicNext } from '../def';
@DefinePlugin({ schema: PicMiddlewareConfig })
export class PicDownloaderMiddleware extends PicMiddlewareBase {
......
import { Context, Awaitable, Logger } from 'koishi';
import {
PartialDeep,
InjectConfig,
Inject,
InjectLogger,
BasePlugin,
} from 'koishi-thirdeye';
import PicsContainer from '.';
import { PicSourceConfig } from './config';
import { PicSourceInfo, PicResult, Instances } from './def';
export class PicSource implements PicSourceInfo {
constructor(protected ctx: Context) {}
tags: string[] = [];
weight = 1;
name = 'default';
description = '';
isDefault = false;
randomPic(picTags: string[]): Awaitable<PicResult> {
// For override
throw new Error(`Not implemented`);
}
onStartup(): Awaitable<void> {
return;
}
onShutdown(): Awaitable<void> {
return;
}
getDisplayString() {
let pattern = this.name;
if (this.tags.length) {
pattern += `\t标签: ${this.tags.join(',')}`;
}
if (this.description) {
pattern += `\t${this.description}`;
}
return pattern;
}
}
export class PicSourcePlugin<
C extends PicSourceConfig = PicSourceConfig,
> extends PicSource {
constructor(ctx: Context, config: PartialDeep<C>) {
super(ctx);
}
@InjectConfig()
protected config: C;
@Inject(true)
protected pics: PicsContainer;
@InjectLogger()
protected logger: Logger;
onApply() {
this.config.applyTo(this);
this.pics.addSource(this);
}
}
export class MultiPicSourcePlugin<
C extends PicSourceConfig = PicSourceConfig,
> extends BasePlugin<Instances<C>> {
@Inject(true)
protected pics: PicsContainer;
@InjectLogger()
protected logger: Logger;
onApply() {
const { instances } = this.config;
}
}
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