mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(bots/discord): add code to actually scan text files correctly
This commit is contained in:
@@ -59,26 +59,38 @@ withContext(on, 'messageCreate', async (context, msg) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (msg.attachments.size > 0 && config.attachments?.scanAttachments) {
|
||||
if (msg.attachments.size && config.attachments?.scanAttachments) {
|
||||
logger.debug(`Classifying message attachments for ${msg.id}`)
|
||||
|
||||
for (const attachment of msg.attachments.values()) {
|
||||
const mimeType = attachment.contentType?.split(';')?.[0]
|
||||
if (!mimeType) return void logger.warn(`No MIME type for attachment: ${attachment.url}`)
|
||||
|
||||
if (
|
||||
config.attachments.allowedMimeTypes &&
|
||||
!config.attachments.allowedMimeTypes.includes(attachment.contentType!)
|
||||
!config.attachments.allowedMimeTypes.includes(mimeType)
|
||||
) {
|
||||
logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${attachment.contentType}`)
|
||||
logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${mimeType}`)
|
||||
continue
|
||||
}
|
||||
|
||||
if (attachment.contentType?.startsWith('text/') && attachment.size > (config.attachments.maxTextFileSize ?? 512 * 1000)) {
|
||||
const isTextFile = mimeType.startsWith('text/')
|
||||
|
||||
if (isTextFile && attachment.size > (config.attachments.maxTextFileSize ?? 512 * 1000)) {
|
||||
logger.debug(`Attachment ${attachment.url} is too large be to scanned, size is ${attachment.size}`)
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
let response: Awaited<ReturnType<typeof getResponseFromText>>['response'] | undefined
|
||||
|
||||
if (isTextFile) {
|
||||
const content = await (await fetch(attachment.url)).text()
|
||||
response = await getResponseFromText(content, filteredResponses, context, { skipApiRequest: true }).then(it => it.response)
|
||||
} else {
|
||||
const { text: content } = await api.client.parseImage(attachment.url)
|
||||
const { response } = await getResponseFromText(content, filteredResponses, context, true)
|
||||
response = await getResponseFromText(content, filteredResponses, context, { onlyImageTriggers: true }).then(it => it.response)
|
||||
}
|
||||
|
||||
if (response) {
|
||||
logger.debug(`Response found for attachment: ${attachment.url}`)
|
||||
@@ -89,8 +101,8 @@ withContext(on, 'messageCreate', async (context, msg) => {
|
||||
|
||||
break
|
||||
}
|
||||
} catch {
|
||||
logger.error(`Failed to parse image: ${attachment.url}`)
|
||||
} catch (e) {
|
||||
logger.error(`Failed to parse attachment: ${attachment.url}`, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ export const getResponseFromText = async (
|
||||
responses: ConfigMessageScanResponse[],
|
||||
// Just to be safe that we will never use data from the context parameter
|
||||
{ api, logger }: Omit<typeof import('src/context'), 'config'>,
|
||||
ocrMode = false,
|
||||
flags: { onlyImageTriggers?: boolean; skipApiRequest?: boolean } = {}
|
||||
): Promise<
|
||||
Omit<ConfigMessageScanResponse, 'triggers'> & { label?: string; triggers?: ConfigMessageScanResponse['triggers'] }
|
||||
> => {
|
||||
@@ -31,7 +31,7 @@ export const getResponseFromText = async (
|
||||
triggers: { text: textTriggers, image: imageTriggers },
|
||||
} = trigger
|
||||
|
||||
if (ocrMode) {
|
||||
if (flags.onlyImageTriggers) {
|
||||
if (imageTriggers)
|
||||
for (const regex of imageTriggers)
|
||||
if (regex.test(content)) {
|
||||
@@ -57,7 +57,7 @@ export const getResponseFromText = async (
|
||||
}
|
||||
|
||||
// If none of the regexes match, we can search for labels immediately
|
||||
if (!responseConfig.triggers && !ocrMode) {
|
||||
if (!responseConfig.triggers && !flags.onlyImageTriggers && !flags.skipApiRequest) {
|
||||
logger.debug('No match from before regexes, doing NLP')
|
||||
const scan = await api.client.parseText(content)
|
||||
if (scan.labels.length) {
|
||||
@@ -87,7 +87,7 @@ export const getResponseFromText = async (
|
||||
}
|
||||
|
||||
// If we still don't have a response config, we can match all regexes after the initial label trigger
|
||||
if (!responseConfig.triggers && ocrMode) {
|
||||
if (!responseConfig.triggers && flags.onlyImageTriggers) {
|
||||
logger.debug('No match from NLP, doing after regexes')
|
||||
for (let i = 0; i < responses.length; i++) {
|
||||
const {
|
||||
|
||||
Reference in New Issue
Block a user