From 3142603380745b3d473f99577c3e0e4f62528f6e Mon Sep 17 00:00:00 2001 From: SaarChaffee Date: Sun, 18 Feb 2024 12:33:51 +0800 Subject: [PATCH] feat: get sentence from plugin --- package.json | 10 ++++-- src/config.ts | 2 ++ src/index.ts | 98 +++++++++++++++++++++++++++------------------------ 3 files changed, 62 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index 741866f..375dfb0 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "build": "yarn build:tsup --minify", "dev": "yarn build:tsup", "build:tsup": "tsup", - "test": "mocha -r tsx -r yml-register --extension .spec.ts ./__tests__", + "clean": "rimraf dist/*", + "test": "mocha -r esbuild-register -r yml-register --extension .spec.ts ./__tests__", "lint": "eslint src/**/*.ts && yarn prettier --check", "format": "yarn prettier --write", "prettier": "prettier '**/*.{js,ts,json,yml,yaml,md}' '!dist/**/*'" @@ -65,7 +66,8 @@ }, "prettier": "@hamster-bot/prettier-config", "peerDependencies": { - "koishi": "^4.16.0" + "koishi": "^4.16.0", + "koishi-plugin-hitokoto-sentences": "^1.0.393" }, "devDependencies": { "@hamster-bot/eslint-config": "*", @@ -81,14 +83,18 @@ "@typescript-eslint/eslint-plugin": "^6.13.1", "@typescript-eslint/parser": "^6.13.1", "chai": "^4.3.10", + "esbuild": "^0.19.8", "esbuild-plugin-yaml": "^0.0.1", + "esbuild-register": "^3.5.0", "eslint": "^8.54.0", "eslint-import-resolver-typescript": "^3.6.1", "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", + "rimraf": "^5.0.5", "tsup": "^8.0.1", "tsx": "^4.7.1", "typescript": "^5.3.3", 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 2be8c39..3bada28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,14 @@ import { Context, Quester } from 'koishi' +import type { SentencesParams } from 'koishi-plugin-hitokoto-sentences' import { HitokotoApi } from './api' import { Config } from './config' import i18n from './i18n' +export const inject = { + optional: ['sentences'], +} + export const name = 'hitokoto' export { Config, HitokotoApi } @@ -13,55 +18,56 @@ export async function apply(ctx: Context, config: Config = {}): Promise { ctx.plugin(HitokotoApi, config) - ctx.inject(['hitokoto'], (ctx) => { - ctx - .command('hitokoto') - .alias('一言') - .option('type', `-t `) - .option('minLength', `-l `) - .option('maxLength', `-L `) - .before(async ({ options, session }) => { - if (options?.minLength && options?.maxLength) { - if (options.minLength > options.maxLength) { - return session?.text('.min_length_gt_max_length') - } - } - if (typeof options?.type === 'undefined') { - return - } - const types = options?.type?.split(',') - if (types.length <= 0 || !types.every((t) => t)) { - return session?.text('.invalid_type', [options.type]) - } - }) - .action(async ({ options, session }) => { - const params = { - c: options?.type?.split(',') ?? config.defaultTypes, - min_length: options?.minLength ?? config.minLength, - max_length: options?.maxLength ?? config.maxLength, + ctx + .command('hitokoto') + .alias('一言') + .option('type', `-t `) + .option('minLength', `-l `) + .option('maxLength', `-L `) + .before(async ({ options, session }) => { + if (options?.minLength && options?.maxLength) { + if (options.minLength > options.maxLength) { + return session?.text('.min_length_gt_max_length') } + } + if (typeof options?.type === 'undefined') { + return + } + const types = options?.type?.split(',') + if (types.length <= 0 || !types.every((t) => t)) { + return session?.text('.invalid_type', [options.type]) + } + }) + .action(async ({ options, session }) => { + const params = { + c: options?.type?.split(',') ?? config.defaultTypes, + min_length: options?.minLength ?? config.minLength, + max_length: options?.maxLength ?? config.maxLength, + } - try { - const resp = await ctx.hitokoto.getHitokoto(params) - return session?.text('.format', resp) - } catch (error) { - const err = error as Error - if (/ETIMEOUT/.test(err.message)) { - return session?.text('.timeout') - } - if (Quester.isAxiosError(error)) { - return session?.text('.request_error', [error.status]) - } - return session?.text('.unknown_error', err) + try { + const resp = + config.sentences && ctx.sentences + ? ctx.sentences.getSentence(params as SentencesParams) + : await ctx.hitokoto.getHitokoto(params) + return session?.text('.format', resp) + } catch (error) { + const err = error as Error + if (/ETIMEOUT/.test(err.message)) { + return session?.text('.timeout') } - }) - - ctx.command('hitokoto.types').action(async ({ session }) => { - return session?.text('.list', [ - Object.entries(ctx.hitokoto.types) - .map(([type, desc]) => `${type} - ${desc}`) - .join('\n'), - ]) + if (Quester.isAxiosError(err)) { + return session?.text('.request_error', [err.status]) + } + return session?.text('.unknown_error', err) + } }) + + ctx.command('hitokoto.types').action(async ({ session }) => { + return session?.text('.list', [ + Object.entries(ctx.hitokoto.types) + .map(([type, desc]) => `${type} - ${desc}`) + .join('\n'), + ]) }) }