Commit ff4a1119 authored by Maiko Sinkyaet Tan's avatar Maiko Sinkyaet Tan

Merge branch 'master' into docs/renew-readme

parents 4f32aecc b5c5b30e
Pipeline #11779 passed with stage
in 46 seconds
......@@ -27,6 +27,6 @@ app.plugin(DatabasePlugin);
app.plugin(ExtrasInDev);
// Target plugin
app.plugin(TargetPlugin, {} as any);
app.plugin(TargetPlugin, { useBase64: true });
app.start();
{
"name": "koishi-plugin-pics",
"version": "8.7.3",
"version": "8.7.9",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "koishi-plugin-pics",
"version": "8.7.3",
"version": "8.7.9",
"license": "MIT",
"dependencies": {
"koishi-thirdeye": "^9.2.18",
......@@ -16,7 +16,7 @@
"@koishijs/plugin-cache-lru": "^1.0.0-rc.0",
"@koishijs/plugin-console": "^3.2.4",
"@koishijs/plugin-database-memory": "^1.1.0",
"@koishijs/plugin-sandbox": "^1.0.2",
"@koishijs/plugin-sandbox": "^1.1.0",
"@types/jest": "^27.4.0",
"@types/lodash": "^4.14.176",
"@types/node": "^16.11.4",
......@@ -37,7 +37,7 @@
"ws": "^8.3.0"
},
"peerDependencies": {
"koishi": "^4.5.0"
"koishi": "^4.5.2"
}
},
"node_modules/@ampproject/remapping": {
......@@ -1058,13 +1058,13 @@
}
},
"node_modules/@koishijs/core": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.5.0.tgz",
"integrity": "sha512-+kppTq/FcJcZf0bESunfxCAcP3xyLUK2lNaHUzP/oKJCD5L2oyGRFjl1JskRJnwwSSscUKYv409ZWCHPqJ4plA==",
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.5.2.tgz",
"integrity": "sha512-ETp//1GsWFBRr4AL4YhZ6NCB4kuhleo+J/KmnGJ90oDignV6k4xtl9fjRLPWsW2IIb2i3BRUHtwSzE9PQTl5+A==",
"peer": true,
"dependencies": {
"@koishijs/orm": "^1.0.0",
"@koishijs/utils": "^5.2.1",
"@koishijs/orm": "^1.0.1",
"@koishijs/utils": "^5.2.2",
"fastest-levenshtein": "^1.0.12"
},
"engines": {
......@@ -1072,11 +1072,11 @@
}
},
"node_modules/@koishijs/orm": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/orm/-/orm-1.0.0.tgz",
"integrity": "sha512-uWSOVCF2THR/S4PPS5FsvsYNhWcPIHO6pXVlSckV5GmJMDQ4zAppSi4s6MwvBHPRX5CMjicK/T2huUlEA/srOQ==",
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@koishijs/orm/-/orm-1.0.1.tgz",
"integrity": "sha512-RZL3XdJ/DMfUstaaoHuhIm4cGAY4VwPiUcDeIsNhZqWT/qS4HhS0pxlEVxtJdDS3138Ou8bjMchHmVZNo2/TIw==",
"dependencies": {
"@koishijs/utils": "^5.2.1"
"@koishijs/utils": "^5.2.2"
},
"engines": {
"node": ">=12.0.0"
......@@ -1121,12 +1121,12 @@
}
},
"node_modules/@koishijs/plugin-sandbox": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.0.2.tgz",
"integrity": "sha512-vOZ4A8At8KJ0Bk5KmBThRiUztFRStg/PKqW2KSPcdj1DWJ4zGIcBNS0+XshH7n2SUWgSWUOaBTdl7kcWtCkAGw==",
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.1.0.tgz",
"integrity": "sha512-y0eJcgBMvGhCYjfVP1IsFvHkmiubAJHWdMlbnkss7eZ39nt0nzApfHAubITIEPBr7A0c7gqfC4fUUw5Z3UldXg==",
"dev": true,
"peerDependencies": {
"koishi": "^4.3.1"
"koishi": "^4.5.2"
}
},
"node_modules/@koishijs/segment": {
......@@ -1135,12 +1135,12 @@
"integrity": "sha512-HmLRKJiIX++U+ow+RP0BVGwgAFzDRqwHusUzjoZcZdcnG8yudTsvB6MXnzBMnBL7k9LBxUlfB5P8ukkM1roZFQ=="
},
"node_modules/@koishijs/utils": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.2.1.tgz",
"integrity": "sha512-Yu+QzmpStHAx5ZknSZhp9Zv3rfy6M4b1tuU3Wh8D3gTgZPeo+gqh07X7GyN7SA76Nr472rN8+hDqq5rafuA9iQ==",
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.2.2.tgz",
"integrity": "sha512-Dm3xNxCjWviEGRNzI+23kbd/6urTe+QBmsuBEr44HYDQXxntqf3KSJtEVL8Fua5g3+R6gOk/zzFIneWK4qzP+g==",
"dependencies": {
"@koishijs/segment": "^1.1.1",
"schemastery": "^3.1.1",
"schemastery": "^3.2.0",
"supports-color": "^8.1.1"
}
},
......@@ -5185,14 +5185,14 @@
}
},
"node_modules/koishi": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.5.0.tgz",
"integrity": "sha512-1VI3Hpm304myRjx7PoV21t8E5ckB4IYFhaE6uUKVIpBBOK25zWCI1X/RwXhxqRGflL8BEpegIP0Y7Be208J+kw==",
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.5.2.tgz",
"integrity": "sha512-5dHLzlBNJeZkjznBQWwYwd/rN06hcw74a0La91VCnCuXAXoGpMeUg2OdemNAtshRl3rtmYXe0UlPw3UZ+m3SEQ==",
"peer": true,
"dependencies": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.5.0",
"@koishijs/utils": "^5.2.1",
"@koishijs/core": "^4.5.2",
"@koishijs/utils": "^5.2.2",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^8.5.3",
......@@ -6382,9 +6382,9 @@
}
},
"node_modules/schemastery": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.1.1.tgz",
"integrity": "sha512-TdbphUvailcX1FqxSq3sqlxdFx/HhBD7TXliq1r4Su7BTChRQimHvrauK/Q75jLVCYaJPACDU92s0JTovmVrtA=="
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.2.0.tgz",
"integrity": "sha512-ZVhQ07wllG9saIuSpJhK996b3X9g9YdqVzGsKedJG9lfNtRnBDu+UiPmMDfvTMRVdK7IvMJgyS3025j/UV4cHA=="
},
"node_modules/schemastery-gen": {
"version": "3.1.2",
......@@ -8492,22 +8492,22 @@
}
},
"@koishijs/core": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.5.0.tgz",
"integrity": "sha512-+kppTq/FcJcZf0bESunfxCAcP3xyLUK2lNaHUzP/oKJCD5L2oyGRFjl1JskRJnwwSSscUKYv409ZWCHPqJ4plA==",
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.5.2.tgz",
"integrity": "sha512-ETp//1GsWFBRr4AL4YhZ6NCB4kuhleo+J/KmnGJ90oDignV6k4xtl9fjRLPWsW2IIb2i3BRUHtwSzE9PQTl5+A==",
"peer": true,
"requires": {
"@koishijs/orm": "^1.0.0",
"@koishijs/utils": "^5.2.1",
"@koishijs/orm": "^1.0.1",
"@koishijs/utils": "^5.2.2",
"fastest-levenshtein": "^1.0.12"
}
},
"@koishijs/orm": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/orm/-/orm-1.0.0.tgz",
"integrity": "sha512-uWSOVCF2THR/S4PPS5FsvsYNhWcPIHO6pXVlSckV5GmJMDQ4zAppSi4s6MwvBHPRX5CMjicK/T2huUlEA/srOQ==",
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/@koishijs/orm/-/orm-1.0.1.tgz",
"integrity": "sha512-RZL3XdJ/DMfUstaaoHuhIm4cGAY4VwPiUcDeIsNhZqWT/qS4HhS0pxlEVxtJdDS3138Ou8bjMchHmVZNo2/TIw==",
"requires": {
"@koishijs/utils": "^5.2.1"
"@koishijs/utils": "^5.2.2"
}
},
"@koishijs/plugin-cache-lru": {
......@@ -8539,9 +8539,9 @@
}
},
"@koishijs/plugin-sandbox": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.0.2.tgz",
"integrity": "sha512-vOZ4A8At8KJ0Bk5KmBThRiUztFRStg/PKqW2KSPcdj1DWJ4zGIcBNS0+XshH7n2SUWgSWUOaBTdl7kcWtCkAGw==",
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-sandbox/-/plugin-sandbox-1.1.0.tgz",
"integrity": "sha512-y0eJcgBMvGhCYjfVP1IsFvHkmiubAJHWdMlbnkss7eZ39nt0nzApfHAubITIEPBr7A0c7gqfC4fUUw5Z3UldXg==",
"dev": true,
"requires": {}
},
......@@ -8551,12 +8551,12 @@
"integrity": "sha512-HmLRKJiIX++U+ow+RP0BVGwgAFzDRqwHusUzjoZcZdcnG8yudTsvB6MXnzBMnBL7k9LBxUlfB5P8ukkM1roZFQ=="
},
"@koishijs/utils": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.2.1.tgz",
"integrity": "sha512-Yu+QzmpStHAx5ZknSZhp9Zv3rfy6M4b1tuU3Wh8D3gTgZPeo+gqh07X7GyN7SA76Nr472rN8+hDqq5rafuA9iQ==",
"version": "5.2.2",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.2.2.tgz",
"integrity": "sha512-Dm3xNxCjWviEGRNzI+23kbd/6urTe+QBmsuBEr44HYDQXxntqf3KSJtEVL8Fua5g3+R6gOk/zzFIneWK4qzP+g==",
"requires": {
"@koishijs/segment": "^1.1.1",
"schemastery": "^3.1.1",
"schemastery": "^3.2.0",
"supports-color": "^8.1.1"
},
"dependencies": {
......@@ -11694,14 +11694,14 @@
}
},
"koishi": {
"version": "4.5.0",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.5.0.tgz",
"integrity": "sha512-1VI3Hpm304myRjx7PoV21t8E5ckB4IYFhaE6uUKVIpBBOK25zWCI1X/RwXhxqRGflL8BEpegIP0Y7Be208J+kw==",
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.5.2.tgz",
"integrity": "sha512-5dHLzlBNJeZkjznBQWwYwd/rN06hcw74a0La91VCnCuXAXoGpMeUg2OdemNAtshRl3rtmYXe0UlPw3UZ+m3SEQ==",
"peer": true,
"requires": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.5.0",
"@koishijs/utils": "^5.2.1",
"@koishijs/core": "^4.5.2",
"@koishijs/utils": "^5.2.2",
"@types/koa": "*",
"@types/koa__router": "*",
"@types/ws": "^8.5.3",
......@@ -12584,9 +12584,9 @@
}
},
"schemastery": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.1.1.tgz",
"integrity": "sha512-TdbphUvailcX1FqxSq3sqlxdFx/HhBD7TXliq1r4Su7BTChRQimHvrauK/Q75jLVCYaJPACDU92s0JTovmVrtA=="
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.2.0.tgz",
"integrity": "sha512-ZVhQ07wllG9saIuSpJhK996b3X9g9YdqVzGsKedJG9lfNtRnBDu+UiPmMDfvTMRVdK7IvMJgyS3025j/UV4cHA=="
},
"schemastery-gen": {
"version": "3.1.2",
......
{
"name": "koishi-plugin-pics",
"description": "Koishi 的随机图片插件",
"version": "8.7.3",
"version": "8.7.9",
"main": "dist/index.js",
"types": "dist/src/index.d.ts",
"scripts": {
......@@ -36,7 +36,7 @@
"@koishijs/plugin-cache-lru": "^1.0.0-rc.0",
"@koishijs/plugin-console": "^3.2.4",
"@koishijs/plugin-database-memory": "^1.1.0",
"@koishijs/plugin-sandbox": "^1.0.2",
"@koishijs/plugin-sandbox": "^1.1.0",
"@types/jest": "^27.4.0",
"@types/lodash": "^4.14.176",
"@types/node": "^16.11.4",
......@@ -57,7 +57,7 @@
"ws": "^8.3.0"
},
"peerDependencies": {
"koishi": "^4.5.0"
"koishi": "^4.5.2"
},
"jest": {
"moduleFileExtensions": [
......
// import 'source-map-support/register';
import { DefineSchema, RegisterSchema, SchemaClass } from 'koishi-thirdeye';
import { Quester } from 'koishi';
@RegisterSchema()
export class PicsPluginConfig {
......@@ -7,8 +8,17 @@ export class PicsPluginConfig {
@DefineSchema({ description: '指令名', default: 'pic', hidden: true })
commandName: string;
@DefineSchema({ description: '保留 URL 中的文件名。', default: false })
preseveFilename: boolean;
@DefineSchema({
description: 'Assets 服务可用时,使用 Assets 缓存图片。',
default: true,
})
useAssets: boolean;
@DefineSchema({ description: '使用 Base64 发送图片结果。', default: false })
useBase64: boolean;
@DefineSchema({ type: Quester.createSchema(), default: {} })
httpConfig: Quester.Config;
}
export type PicsPluginConfigLike = Partial<PicsPluginConfig>;
......
// import 'source-map-support/register';
import { Context, Assets, Awaitable, Random, Logger } from 'koishi';
import { Context, Assets, Awaitable, Random, Logger, Bot } from 'koishi';
import { PicSourceInfo, PicsPluginConfig } from './config';
import _ from 'lodash';
import { segment } from 'koishi';
import { segment, Quester } from 'koishi';
import {
BasePlugin,
Caller,
......@@ -75,6 +75,11 @@ export default class PicsContainer
@Inject()
private assets: Assets;
@Inject(true)
private http: Quester;
private _http: Quester;
addSource(source: PicSource, targetCtx?: Context) {
const processingCtx: Context = targetCtx || this.caller;
const dispose = processingCtx.on('dispose', () =>
......@@ -175,13 +180,47 @@ export default class PicsContainer
return this.fetchPicsWithSources(sources, picTags);
}
isOneBotBot(bot?: Bot) {
return (
bot &&
(bot.platform === 'onebot' ||
(bot.platform === 'qqguild' && bot['parentBot']?.platform === 'onebot'))
);
}
async getSegment(url: string, bot?: Bot) {
let useFileHeader = false;
try {
if (this.config.useAssets && this.assets) {
const uploadedUrl = await this.assets.upload(url, undefined);
url = uploadedUrl;
} else if (this.config.useBase64) {
const buf = await this._http.get(url, {
responseType: 'arraybuffer',
});
url = `base64://${buf.toString('base64')}`;
useFileHeader = true;
}
} catch (e) {
this.logger.warn(`Download image ${url} failed: ${e.toString()}`);
}
const isOneBotBot = this.isOneBotBot(bot);
const picData: segment.Data = {
[isOneBotBot && useFileHeader ? 'file' : 'url']: url,
cache: true,
};
return segment('image', picData);
}
async onApply() {
this._http = this.http.extend(this.config.httpConfig);
const ctx = this.ctx;
ctx.i18n.define('zh', `commands.${this.config.commandName}`, {
description: '获取随机图片',
options: {
source: `指定图源,逗号分隔。图源可以用 ${this.config.commandName}.sources 查询。`,
},
usage: `从各个图源中随机获取一张随机图片。图源可以用 ${this.config.commandName}.sources 查询。参数均为可选。`,
messages: {
'not-found': '未找到任何图片。',
},
......@@ -189,6 +228,7 @@ export default class PicsContainer
ctx.i18n.define('en', `commands.${this.config.commandName}`, {
description: 'Get random picture',
options: {},
usage: `Get a random picture from a random sourse. Sources can be queried with command ${this.config.commandName}.sources`,
messages: {
'not-found': 'No pictures found.',
},
......@@ -196,6 +236,7 @@ export default class PicsContainer
ctx.i18n.define('zh', `commands.${this.config.commandName}.sources`, {
description: '查询图源列表',
options: {},
usage: '图源标签可用于图片获取的图源筛选。',
messages: {
list: '图源的列表如下:',
},
......@@ -203,27 +244,18 @@ export default class PicsContainer
ctx.i18n.define('en', `commands.${this.config.commandName}.sources`, {
description: 'Query picture sources',
options: {},
usage: 'Source tags can be used to filter picture sources.',
messages: {
list: 'List of sources:',
},
});
ctx
.command(`${this.config.commandName} [...tags:string]`, '获取随机图片')
.usage(
`从各个图源中随机获取一张随机图片。图源可以用 ${this.config.commandName}.sources 查询。参数均为可选。`,
)
.option(
'source',
`-s <source:string> 指定图源,逗号分隔。图源可以用 ${this.config.commandName}.sources 查询。`,
)
.example(`${this.config.commandName} 获取一张随机图片。`)
.example(`${this.config.commandName} yuyuko 获取一张 yuyuko 标签的图片。`)
.example(
`${this.config.commandName} -s yande 获取一张 yande 图源的图片。`,
)
.example(
`${this.config.commandName} -s yande yuyuko saigyouji 从 yande 图源中获取一张具有 yuyuko 以及 saigyouji 标签的图。`,
)
.command(`${this.config.commandName} [...tags:string]`)
.option('source', `-s <source:string>`)
.example(`${this.config.commandName}`)
.example(`${this.config.commandName} yuyuko`)
.example(`${this.config.commandName} -s yande`)
.example(`${this.config.commandName} -s yande yuyuko saigyouji`)
.action(async (argv, ...picTags) => {
const sourceTags = argv.options.source
? argv.options.source.split(/[ ,+\uFF0C\uFF0B\u3001]/)
......@@ -233,27 +265,16 @@ export default class PicsContainer
if (!result) {
return argv.session.text('.not-found');
}
const picData: segment.Data = {
url: result.url,
cache: true,
};
if (this.config.preseveFilename) {
picData.file = result.url.split('/').pop();
}
let msg = segment('image', picData);
let msg = await this.getSegment(result.url, argv.session.bot);
if (result.description) {
msg += `\n${result.description}`;
}
if (this.assets) {
msg = await this.assets.transform(msg);
}
return msg;
})
.subcommand('.sources [...tags:string]', '查询图源列表')
// .option('source', '-s <source:string> 要查询的图源标签,逗号分隔。')
.usage('图源标签可用于图片获取的图源筛选。')
.example(`${this.config.commandName}.sources 查询全部的图源。`)
.example(`${this.config.commandName}.sources pixiv 查询含有 pixiv 标签的图源。`)
.subcommand('.sources [...tags:string]')
.example(`${this.config.commandName}.sources`)
.example(`${this.config.commandName}.sources pixiv`)
.action(async (argv, ...sourceTags) => {
sourceTags ||= [];
const sources = this.pickAvailableSources(sourceTags, true);
......
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