From cf2e521205767f7f4b07fbb40cb8ea77b65872ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9C=9E=E9=A3=9B?= Date: Sun, 18 Feb 2024 14:26:54 +0800 Subject: [PATCH] feat: get sentence from plugin (#25) * feat: get sentence from plugin * fix: optional sentences * fix: remove sentences * fix: use sentense for service & fix package conflict * fix format --------- Co-authored-by: Maiko Tan --- package.json | 1 + src/api.ts | 30 +++++++++++++++++------------- src/config.ts | 2 ++ src/index.ts | 4 ++++ 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 741866f..bfe37fd 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "fs-extra": "^11.2.0", "js-yaml": "^4.1.0", "koishi": "^4.16.0", + "koishi-plugin-hitokoto-sentences": "^1.0.393", "mocha": "^10.2.0", "prettier": "^3.1.0", "tsup": "^8.0.1", diff --git a/src/api.ts b/src/api.ts index b4d33a1..c26019c 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,5 @@ import { Context, Service } from 'koishi' +import type { SentencesParams } from 'koishi-plugin-hitokoto-sentences' import { Config } from '.' @@ -8,24 +9,27 @@ declare module 'koishi' { } } -export interface HitokotoParams { - c?: string[] - min_length?: number - max_length?: number -} - export class HitokotoApi extends Service { private _apiUrl: string - constructor(ctx: Context, option: Config) { + constructor( + ctx: Context, + private config: Config, + ) { super(ctx, 'hitokoto', true) - this._apiUrl = option.apiUrl ?? 'https://v1.hitokoto.cn/' + this._apiUrl = config.apiUrl ?? 'https://v1.hitokoto.cn/' } - async getHitokoto(params: HitokotoParams): Promise { - const resp = await this.ctx.http.get(this._apiUrl, { - params: this.buildSearchParams(params), - }) + async getHitokoto(params: SentencesParams): Promise { + const sentences = this.ctx.get('sentences') + let resp: HitokotoRet + if (this.config.sentences && sentences) { + resp = sentences.getSentence(params) + } else { + resp = await this.ctx.http.get(this._apiUrl, { + params: this.buildSearchParams(params), + }) + } return { ...resp, // the `from_who` field may be null. @@ -33,7 +37,7 @@ export class HitokotoApi extends Service { } } - buildSearchParams(params: HitokotoParams): URLSearchParams { + buildSearchParams(params: SentencesParams): URLSearchParams { const searchParams = new URLSearchParams() if (params.c) { params.c.forEach((type) => searchParams.append('c', type)) diff --git a/src/config.ts b/src/config.ts index 3b2bf72..d2f8f00 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,7 @@ import { Schema } from 'koishi' export interface Config { + sentences?: boolean /** * @default "https://v1.hitokoto.cn" */ @@ -16,6 +17,7 @@ export interface Config { } export const Config: Schema = Schema.object({ + sentences: Schema.boolean().description('是否使用本地一言语料库(需要安装 sentences 服务)').default(false), apiUrl: Schema.string().description('获取一言的 API 地址').default('https://v1.hitokoto.cn'), minLength: Schema.number().description('一言的最小长度'), maxLength: Schema.number().description('一言的最大长度'), diff --git a/src/index.ts b/src/index.ts index cf48c04..b0e0e93 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,10 @@ import * as Command from './command' import { Config } from './config' import i18n from './i18n' +export const inject = { + optional: ['sentences'], +} + export const name = 'hitokoto' export { Config, HitokotoApi }