Skip to content

Commit

Permalink
feat: get sentence from plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
SaarChaffee committed Feb 18, 2024
1 parent 9c02520 commit 3142603
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 48 deletions.
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*'"
Expand Down Expand Up @@ -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": "*",
Expand All @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Schema } from 'koishi'

export interface Config {
sentences?: boolean
/**
* @default "https://v1.hitokoto.cn"
*/
Expand All @@ -16,6 +17,7 @@ export interface Config {
}

export const Config: Schema<Config> = 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('一言的最大长度'),
Expand Down
98 changes: 52 additions & 46 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -13,55 +18,56 @@ export async function apply(ctx: Context, config: Config = {}): Promise<void> {

ctx.plugin(HitokotoApi, config)

ctx.inject(['hitokoto'], (ctx) => {
ctx
.command('hitokoto')
.alias('一言')
.option('type', `-t <type:string>`)
.option('minLength', `-l <length:int>`)
.option('maxLength', `-L <length:int>`)
.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 <type:string>`)
.option('minLength', `-l <length:int>`)
.option('maxLength', `-L <length:int>`)
.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'),
])
})
}

0 comments on commit 3142603

Please sign in to comment.