feat(bots/discord): add ocrTriggers resp config, embed footer scan mode

This commit is contained in:
PalmDevs
2024-03-29 18:47:55 +07:00
parent b104472e47
commit 744a56a4fd
4 changed files with 37 additions and 12 deletions

View File

@@ -4,6 +4,12 @@ export const MessageScanLabeledResponseReactions = {
delete: '❌',
} as const
export const MessageScanHumanizedMode = {
ocr: 'image recognition',
nlp: 'text analysis',
match: 'pattern matching',
} as const
export const DefaultEmbedColor = '#4E98F0'
export const ReVancedLogoURL =
'https://media.discordapp.net/attachments/1095487869923119144/1115436493050224660/revanced-logo.png'

View File

@@ -23,7 +23,7 @@ on('messageCreate', async (ctx, msg) => {
logger.debug('Response found')
const reply = await msg.reply({
embeds: [createMessageScanResponseEmbed(response)],
embeds: [createMessageScanResponseEmbed(response, label ? 'nlp' : 'match')],
})
if (label)
@@ -52,12 +52,12 @@ on('messageCreate', async (ctx, msg) => {
try {
const { text: content } = await api.client.parseImage(attachment.url)
const { response } = await getResponseFromContent(content, ctx)
const { response } = await getResponseFromContent(content, ctx, true)
if (response) {
logger.debug(`Response found for attachment: ${attachment.url}`)
await msg.reply({
embeds: [createMessageScanResponseEmbed(response)],
embeds: [createMessageScanResponseEmbed(response, 'ocr')],
})
break

View File

@@ -1,4 +1,4 @@
import { DefaultEmbedColor, ReVancedLogoURL } from '$/constants'
import { DefaultEmbedColor, MessageScanHumanizedMode, ReVancedLogoURL } from '$/constants'
import { EmbedBuilder } from 'discord.js'
import type { ConfigMessageScanResponseMessage } from '../../../config.example'
@@ -26,12 +26,20 @@ export const createSuccessEmbed = (title: string, description?: string) =>
false,
)
export const createMessageScanResponseEmbed = (response: ConfigMessageScanResponseMessage) => {
export const createMessageScanResponseEmbed = (
response: ConfigMessageScanResponseMessage,
mode: 'ocr' | 'nlp' | 'match',
) => {
const embed = new EmbedBuilder().setTitle(response.title)
if (response.description) embed.setDescription(response.description)
if (response.fields) embed.addFields(response.fields)
embed.setFooter({
text: `ReVanced • Done via ${MessageScanHumanizedMode[mode]}`,
iconURL: ReVancedLogoURL,
})
return applyCommonStyles(embed)
}

View File

@@ -6,6 +6,7 @@ import { createMessageScanResponseEmbed } from './embeds'
export const getResponseFromContent = async (
content: string,
{ api, logger, config: { messageScan: config } }: typeof import('src/context'),
ocrMode = false,
) => {
if (!config || !config.responses) {
logger.warn('No message scan config found')
@@ -21,12 +22,22 @@ export const getResponseFromContent = async (
const firstLabelIndexes: number[] = []
// Test if all regexes before a label trigger is matched
for (const trigger of config.responses) {
const { triggers, response: resp } = trigger
for (let i = 0; i < config.responses.length; i++) {
const trigger = config.responses[i]!
const { triggers, ocrTriggers, response: resp } = trigger
if (response) break
for (let i = 0; i < triggers.length; i++) {
const trigger = triggers[i]!
if (ocrMode && ocrTriggers)
for (const regex of ocrTriggers)
if (regex.test(content)) {
logger.debug(`Message matched regex (OCR mode): ${regex.source}`)
response = resp
break
}
for (let j = 0; j < triggers.length; j++) {
const trigger = triggers[j]!
if (trigger instanceof RegExp) {
if (trigger.test(content)) {
@@ -35,14 +46,14 @@ export const getResponseFromContent = async (
break
}
} else {
firstLabelIndexes.push(i)
firstLabelIndexes[i] = j
break
}
}
}
// If none of the regexes match, we can search for labels immediately
if (!response) {
if (!response && !ocrMode) {
const scan = await api.client.parseText(content)
if (scan.labels.length) {
const matchedLabel = scan.labels[0]!
@@ -129,7 +140,7 @@ export const handleUserResponseCorrection = async (
if (response.label !== label) {
db.labeledResponses.edit(response.reply, { label, correctedBy: user.id })
await reply.edit({
embeds: [createMessageScanResponseEmbed(correctLabelResponse.response)],
embeds: [createMessageScanResponseEmbed(correctLabelResponse.response, 'nlp')],
})
}