chore: apply code fixes with biome

This commit is contained in:
PalmDevs
2023-11-28 22:03:41 +07:00
parent c80bd068fa
commit f2d85c32a4
32 changed files with 1384 additions and 1383 deletions

View File

@@ -1,20 +1,20 @@
import type { ClientOperation } from '@revanced/bot-shared'
import type { Wit } from 'node-wit'
import { ClientPacketObject } from '../classes/Client.js'
import type { Config } from '../utils/getConfig.js'
import type { Logger } from '../utils/logger.js'
import type { Worker as TesseractWorker } from 'tesseract.js'
export { default as parseTextEventHandler } from './parseText.js'
export { default as parseImageEventHandler } from './parseImage.js'
export type EventHandler<POp extends ClientOperation> = (
packet: ClientPacketObject<POp>,
context: EventContext
) => void | Promise<void>
export type EventContext = {
witClient: Wit
tesseractWorker: TesseractWorker
logger: Logger
config: Config
}
import type { ClientOperation } from '@revanced/bot-shared'
import type { Wit } from 'node-wit'
import type { Worker as TesseractWorker } from 'tesseract.js'
import { ClientPacketObject } from '../classes/Client.js'
import type { Config } from '../utils/getConfig.js'
import type { Logger } from '../utils/logger.js'
export { default as parseTextEventHandler } from './parseText.js'
export { default as parseImageEventHandler } from './parseImage.js'
export type EventHandler<POp extends ClientOperation> = (
packet: ClientPacketObject<POp>,
context: EventContext,
) => void | Promise<void>
export type EventContext = {
witClient: Wit
tesseractWorker: TesseractWorker
logger: Logger
config: Config
}

View File

@@ -1,63 +1,63 @@
import { ClientOperation, ServerOperation } from '@revanced/bot-shared'
import { AsyncQueue } from '@sapphire/async-queue'
import type { EventHandler } from './index.js'
const queue = new AsyncQueue()
const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
packet,
{ tesseractWorker, logger, config }
) => {
const {
client,
d: { image_url: imageUrl, id },
} = packet
logger.debug(
`Client ${client.id} requested to parse image from URL:`,
imageUrl
)
logger.debug(
`Queue currently has ${queue.remaining}/${config.ocrConcurrentQueues} items in it`
)
if (queue.remaining < config.ocrConcurrentQueues) queue.shift()
await queue.wait()
try {
logger.debug(`Recognizing image from URL for client ${client.id}`)
const { data, jobId } = await tesseractWorker.recognize(imageUrl)
logger.debug(
`Recognized image from URL for client ${client.id} (job ${jobId}):`,
data.text
)
await client.send({
op: ServerOperation.ParsedImage,
d: {
id,
text: data.text,
},
})
} catch {
logger.error(
`Failed to parse image from URL for client ${client.id}:`,
imageUrl
)
await client.send({
op: ServerOperation.ParseImageFailed,
d: {
id,
},
})
} finally {
queue.shift()
logger.debug(
`Finished processing image from URL for client ${client.id}, queue has ${queue.remaining}/${config.ocrConcurrentQueues} remaining items in it`
)
}
}
export default parseImageEventHandler
import { ClientOperation, ServerOperation } from '@revanced/bot-shared'
import { AsyncQueue } from '@sapphire/async-queue'
import type { EventHandler } from './index.js'
const queue = new AsyncQueue()
const parseImageEventHandler: EventHandler<ClientOperation.ParseImage> = async (
packet,
{ tesseractWorker, logger, config },
) => {
const {
client,
d: { image_url: imageUrl, id },
} = packet
logger.debug(
`Client ${client.id} requested to parse image from URL:`,
imageUrl,
)
logger.debug(
`Queue currently has ${queue.remaining}/${config.ocrConcurrentQueues} items in it`,
)
if (queue.remaining < config.ocrConcurrentQueues) queue.shift()
await queue.wait()
try {
logger.debug(`Recognizing image from URL for client ${client.id}`)
const { data, jobId } = await tesseractWorker.recognize(imageUrl)
logger.debug(
`Recognized image from URL for client ${client.id} (job ${jobId}):`,
data.text,
)
await client.send({
op: ServerOperation.ParsedImage,
d: {
id,
text: data.text,
},
})
} catch {
logger.error(
`Failed to parse image from URL for client ${client.id}:`,
imageUrl,
)
await client.send({
op: ServerOperation.ParseImageFailed,
d: {
id,
},
})
} finally {
queue.shift()
logger.debug(
`Finished processing image from URL for client ${client.id}, queue has ${queue.remaining}/${config.ocrConcurrentQueues} remaining items in it`,
)
}
}
export default parseImageEventHandler

View File

@@ -1,43 +1,43 @@
import { ClientOperation, ServerOperation } from '@revanced/bot-shared'
import { inspect as inspectObject } from 'node:util'
import type { EventHandler } from './index.js'
const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (
packet,
{ witClient, logger }
) => {
const {
client,
d: { text, id },
} = packet
logger.debug(`Client ${client.id} requested to parse text:`, text)
try {
const { intents } = await witClient.message(text, {})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const intentsWithoutIds = intents.map(({ id, ...rest }) => rest)
await client.send({
op: ServerOperation.ParsedText,
d: {
id,
labels: intentsWithoutIds,
},
})
} catch (e) {
await client.send({
op: ServerOperation.ParseTextFailed,
d: {
id,
},
})
if (e instanceof Error) logger.error(e.stack ?? e.message)
else logger.error(inspectObject(e))
}
}
export default parseTextEventHandler
import { ClientOperation, ServerOperation } from '@revanced/bot-shared'
import { inspect as inspectObject } from 'node:util'
import type { EventHandler } from './index.js'
const parseTextEventHandler: EventHandler<ClientOperation.ParseText> = async (
packet,
{ witClient, logger },
) => {
const {
client,
d: { text, id },
} = packet
logger.debug(`Client ${client.id} requested to parse text:`, text)
try {
const { intents } = await witClient.message(text, {})
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const intentsWithoutIds = intents.map(({ id, ...rest }) => rest)
await client.send({
op: ServerOperation.ParsedText,
d: {
id,
labels: intentsWithoutIds,
},
})
} catch (e) {
await client.send({
op: ServerOperation.ParseTextFailed,
d: {
id,
},
})
if (e instanceof Error) logger.error(e.stack ?? e.message)
else logger.error(inspectObject(e))
}
}
export default parseTextEventHandler