diff --git a/src/index.ts b/src/index.ts index 1b29b25..43252f9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,53 +13,55 @@ export async function apply(ctx: Context, config: Config = {}): Promise { ctx.plugin(HitokotoApi, config) - ctx - .command('hitokoto') - .alias('一言') - .option('type', `-t `) - .option('min-length', `-l `) - .option('max-length', `-L `) - .before(async ({ options, session }) => { - if (options?.['min-length'] && options?.['max-length']) { - if (options['min-length'] > options['max-length']) { - return session?.text('.min_length_gt_max_length') + ctx.inject(['hitokoto'], (ctx) => { + ctx + .command('hitokoto') + .alias('一言') + .option('type', `-t `) + .option('min-length', `-l `) + .option('max-length', `-L `) + .before(async ({ options, session }) => { + if (options?.['min-length'] && options?.['max-length']) { + if (options['min-length'] > options['max-length']) { + 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?.['min-length'] ?? config.minLength, - max_length: options?.['max-length'] ?? 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 (typeof options?.type === 'undefined') { + return } - if (Quester.isAxiosError(error)) { - return session?.text('.request_error', [error.status]) + 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?.['min-length'] ?? config.minLength, + max_length: options?.['max-length'] ?? config.maxLength, } - 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'), - ]) + 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'), + ]) + }) }) }