Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan committed Feb 18, 2024
2 parents d8757ce + bc2998c commit e6bc346
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 60 deletions.
18 changes: 13 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Context, Service } from 'koishi'

import { Config } from '.'

import type { SentencesParams } from 'koishi-plugin-hitokoto-sentences'

declare module 'koishi' {
interface Context {
hitokoto: HitokotoApi
Expand All @@ -17,15 +19,21 @@ export interface HitokotoParams {
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<HitokotoRet> {
const resp = await this.ctx.http.get<HitokotoRet>(this._apiUrl, {
params: this.buildSearchParams(params),
})
const sentences = this.ctx.get('sentences')
let resp: HitokotoRet
if (this.config.sentences && sentences) {
resp = sentences.getSentences(params as SentencesParams)
} else {
resp = await this.ctx.http.get<HitokotoRet>(this._apiUrl, {
params: this.buildSearchParams(params),
})
}
return {
...resp,
// the `from_who` field may be null.
Expand Down
57 changes: 57 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Context, Quester } from 'koishi'

import { Config } from './config'

export const inject = ['hitokoto']

export async function apply(ctx: Context, config: Config = {}): Promise<void> {
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)
}
})

ctx.command('hitokoto.types').action(async ({ session }) => {
return session?.text('.list', [
Object.entries(ctx.hitokoto.types)
.map(([type, desc]) => `${type} - ${desc}`)
.join('\n'),
])
})
}
58 changes: 3 additions & 55 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Context, Quester } from 'koishi'
import type { SentencesParams } from 'koishi-plugin-hitokoto-sentences'
import { Context } from 'koishi'

import { HitokotoApi } from './api'
import * as Command from './command'
import { Config } from './config'
import i18n from './i18n'

Expand All @@ -17,57 +17,5 @@ export async function apply(ctx: Context, config: Config = {}): Promise<void> {
Object.entries(i18n).forEach(([key, value]) => ctx.i18n.define(key, value))

ctx.plugin(HitokotoApi, config)

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 =
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')
}
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'),
])
})
ctx.plugin(Command, config)
}

0 comments on commit e6bc346

Please sign in to comment.