Skip to content

Commit

Permalink
refactor: split command as sub plugin (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikoTan authored Feb 18, 2024
1 parent 9c02520 commit bc2998c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 53 deletions.
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'),
])
})
}
56 changes: 3 additions & 53 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Context, Quester } from 'koishi'
import { Context } from 'koishi'

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

Expand All @@ -12,56 +13,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.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,
}

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

0 comments on commit bc2998c

Please sign in to comment.