koishi-plugin-booru
Version:
Image service for Koishi
55 lines (54 loc) • 2.08 kB
JavaScript
import { Schema } from 'koishi';
export class ImageSource {
ctx;
config;
static inject = ['booru'];
languages = [];
source;
http;
constructor(ctx, config) {
this.ctx = ctx;
this.config = config;
this.ctx.booru.register(this);
this.http = config.proxyAgent ? ctx.http.extend({ proxyAgent: config.proxyAgent }) : ctx.http;
}
/**
* split query into tags, default implementation is comma-separated.
*
* e.g. `tag1, wordy tag2, UPPER CASED tag3` => `['tag1', 'wordy_tag2', 'upper_cased_tag3']`
*/
tokenize(query) {
return query
.split(',')
.map((x) => x.trim())
.filter(Boolean)
.map((x) => x.toLowerCase().replace(/\s+/g, '_'));
}
}
(function (ImageSource) {
function createSchema(o) {
return Schema.intersect([
Schema.object({
label: Schema.string().default(o.label).description('图源标签,可用于在指令中手动指定图源。'),
weight: Schema.number()
.min(1)
.default(1)
.description('图源权重。在多个符合标签的图源中,将按照各自的权重随机选择。'),
}).description('全局设置'),
Schema.object({
proxyAgent: Schema.string().default(undefined).description('请求图片时使用代理服务器。'),
}).description('请求设置'),
]);
}
ImageSource.createSchema = createSchema;
ImageSource.Config = createSchema({ label: 'default' });
let PreferSize;
(function (PreferSize) {
PreferSize["Original"] = "original";
PreferSize["Large"] = "large";
PreferSize["Medium"] = "medium";
PreferSize["Small"] = "small";
PreferSize["Thumbnail"] = "thumbnail";
})(PreferSize = ImageSource.PreferSize || (ImageSource.PreferSize = {}));
})(ImageSource || (ImageSource = {}));
export const preferSizes = ['thumbnail', 'large', 'medium', 'small', 'original'];