Skip to content

Commit

Permalink
fix: using inject instead using
Browse files Browse the repository at this point in the history
  • Loading branch information
SaarChaffee committed Dec 4, 2023
1 parent 3f36ca1 commit 6507c86
Showing 1 changed file with 46 additions and 44 deletions.
90 changes: 46 additions & 44 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,55 @@ export async function apply(ctx: Context, config: Config = {}): Promise<void> {

ctx.plugin(HitokotoApi, config)

ctx
.command('hitokoto')
.alias('一言')
.option('type', `-t <type:string>`)
.option('min-length', `-l <length:int>`)
.option('max-length', `-L <length:int>`)
.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) => {

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Property 'inject' does not exist on type 'Context'.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Parameter 'ctx' implicitly has an 'any' type.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Property 'inject' does not exist on type 'Context'.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Parameter 'ctx' implicitly has an 'any' type.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Property 'inject' does not exist on type 'Context'.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Parameter 'ctx' implicitly has an 'any' type.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Property 'inject' does not exist on type 'Context'.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Parameter 'ctx' implicitly has an 'any' type.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Property 'inject' does not exist on type 'Context'.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Parameter 'ctx' implicitly has an 'any' type.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Property 'inject' does not exist on type 'Context'.

Check failure on line 16 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Parameter 'ctx' implicitly has an 'any' type.
ctx
.command('hitokoto')
.alias('一言')
.option('type', `-t <type:string>`)
.option('min-length', `-l <length:int>`)
.option('max-length', `-L <length:int>`)
.before(async ({ options, session }) => {

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 23 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'session' implicitly has an 'any' type.
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)) {

Check failure on line 33 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Parameter 't' implicitly has an 'any' type.

Check failure on line 33 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Parameter 't' implicitly has an 'any' type.

Check failure on line 33 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Parameter 't' implicitly has an 'any' type.

Check failure on line 33 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Parameter 't' implicitly has an 'any' type.

Check failure on line 33 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Parameter 't' implicitly has an 'any' type.

Check failure on line 33 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Parameter 't' implicitly has an 'any' type.
return session?.text('.invalid_type', [options.type])
}
})
.action(async ({ options, session }) => {

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'options' implicitly has an 'any' type.

Check failure on line 37 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'session' implicitly has an 'any' type.
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 }) => {

Check failure on line 59 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 59 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 59 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 59 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.12)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 59 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.13)

Binding element 'session' implicitly has an 'any' type.

Check failure on line 59 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (4.11)

Binding element 'session' implicitly has an 'any' type.
return session?.text('.list', [
Object.entries(ctx.hitokoto.types)
.map(([type, desc]) => `${type} - ${desc}`)
.join('\n'),
])
})
})
}

0 comments on commit 6507c86

Please sign in to comment.