fix(bots/discord): filter out text triggers correctly from image-only scans

This commit is contained in:
PalmDevs
2024-09-20 13:49:38 +07:00
parent 7a379a2cae
commit 8c0dd67d03
3 changed files with 57 additions and 55 deletions

View File

@@ -66,10 +66,7 @@ withContext(on, 'messageCreate', async (context, msg) => {
const mimeType = attachment.contentType?.split(';')?.[0] const mimeType = attachment.contentType?.split(';')?.[0]
if (!mimeType) return void logger.warn(`No MIME type for attachment: ${attachment.url}`) if (!mimeType) return void logger.warn(`No MIME type for attachment: ${attachment.url}`)
if ( if (config.attachments.allowedMimeTypes && !config.attachments.allowedMimeTypes.includes(mimeType)) {
config.attachments.allowedMimeTypes &&
!config.attachments.allowedMimeTypes.includes(mimeType)
) {
logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${mimeType}`) logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${mimeType}`)
continue continue
} }
@@ -86,10 +83,14 @@ withContext(on, 'messageCreate', async (context, msg) => {
if (isTextFile) { if (isTextFile) {
const content = await (await fetch(attachment.url)).text() const content = await (await fetch(attachment.url)).text()
response = await getResponseFromText(content, filteredResponses, context, { skipApiRequest: true }).then(it => it.response) response = await getResponseFromText(content, filteredResponses, context, {
textRegexesOnly: true,
}).then(it => it.response)
} else { } else {
const { text: content } = await api.client.parseImage(attachment.url) const { text: content } = await api.client.parseImage(attachment.url)
response = await getResponseFromText(content, filteredResponses, context, { onlyImageTriggers: true }).then(it => it.response) response = await getResponseFromText(content, filteredResponses, context, {
imageTriggersOnly: true,
}).then(it => it.response)
} }
if (response) { if (response) {

View File

@@ -9,7 +9,7 @@ export const getResponseFromText = async (
responses: ConfigMessageScanResponse[], responses: ConfigMessageScanResponse[],
// Just to be safe that we will never use data from the context parameter // Just to be safe that we will never use data from the context parameter
{ api, logger }: Omit<typeof import('src/context'), 'config'>, { api, logger }: Omit<typeof import('src/context'), 'config'>,
flags: { onlyImageTriggers?: boolean; skipApiRequest?: boolean } = {} flags: { imageTriggersOnly?: boolean; textRegexesOnly?: boolean } = {},
): Promise< ): Promise<
Omit<ConfigMessageScanResponse, 'triggers'> & { label?: string; triggers?: ConfigMessageScanResponse['triggers'] } Omit<ConfigMessageScanResponse, 'triggers'> & { label?: string; triggers?: ConfigMessageScanResponse['triggers'] }
> => { > => {
@@ -31,7 +31,7 @@ export const getResponseFromText = async (
triggers: { text: textTriggers, image: imageTriggers }, triggers: { text: textTriggers, image: imageTriggers },
} = trigger } = trigger
if (flags.onlyImageTriggers) { if (flags.imageTriggersOnly) {
if (imageTriggers) if (imageTriggers)
for (const regex of imageTriggers) for (const regex of imageTriggers)
if (regex.test(content)) { if (regex.test(content)) {
@@ -39,7 +39,7 @@ export const getResponseFromText = async (
responseConfig = trigger responseConfig = trigger
break break
} }
} else } else {
for (let j = 0; j < textTriggers!.length; j++) { for (let j = 0; j < textTriggers!.length; j++) {
const regex = textTriggers![j]! const regex = textTriggers![j]!
@@ -54,55 +54,59 @@ export const getResponseFromText = async (
break break
} }
} }
}
// If none of the regexes match, we can search for labels immediately // If none of the regexes match, we can search for labels immediately
if (!responseConfig.triggers && !flags.onlyImageTriggers && !flags.skipApiRequest) { if (!responseConfig.triggers && !flags.textRegexesOnly) {
logger.debug('No match from before regexes, doing NLP') logger.debug('No match from before regexes, doing NLP')
const scan = await api.client.parseText(content) const scan = await api.client.parseText(content)
if (scan.labels.length) { if (scan.labels.length) {
const matchedLabel = scan.labels[0]! const matchedLabel = scan.labels[0]!
logger.debug(`Message matched label with confidence: ${matchedLabel.name}, ${matchedLabel.confidence}`) logger.debug(
`Message matched label with confidence: ${matchedLabel.name}, ${matchedLabel.confidence}`,
)
let trigger: ConfigMessageScanResponseLabelConfig | undefined let trigger: ConfigMessageScanResponseLabelConfig | undefined
const response = responses.find(x => { const response = responses.find(x => {
const config = x.triggers.text!.find( const config = x.triggers.text!.find(
(x): x is ConfigMessageScanResponseLabelConfig => 'label' in x && x.label === matchedLabel.name, (x): x is ConfigMessageScanResponseLabelConfig =>
) 'label' in x && x.label === matchedLabel.name,
if (config) trigger = config )
return config if (config) trigger = config
}) return config
})
if (!response) { if (!response) {
logger.warn(`No response config found for label ${matchedLabel.name}`) logger.warn(`No response config found for label ${matchedLabel.name}`)
// This returns the default value set in line 17, which means no response matched // This returns the default value set in line 17, which means no response matched
return responseConfig return responseConfig
}
if (matchedLabel.confidence >= trigger!.threshold) {
logger.debug('Label confidence is enough')
responseConfig = { ...responseConfig, ...response, label: trigger!.label }
}
}
} }
if (matchedLabel.confidence >= trigger!.threshold) { // If we still don't have a response config, we can match all regexes after the initial label trigger
logger.debug('Label confidence is enough') if (!responseConfig.triggers && !flags.imageTriggersOnly) {
responseConfig = { ...responseConfig, ...response, label: trigger!.label } logger.debug('No match from NLP, doing after regexes')
} for (let i = 0; i < responses.length; i++) {
} const {
} triggers: { text: textTriggers },
} = responses[i]!
const firstLabelIndex = firstLabelIndexes[i] ?? -1
// If we still don't have a response config, we can match all regexes after the initial label trigger for (let j = firstLabelIndex + 1; j < textTriggers!.length; j++) {
if (!responseConfig.triggers && flags.onlyImageTriggers) { const trigger = textTriggers![j]!
logger.debug('No match from NLP, doing after regexes')
for (let i = 0; i < responses.length; i++) {
const {
triggers: { text: textTriggers },
} = responses[i]!
const firstLabelIndex = firstLabelIndexes[i] ?? -1
for (let j = firstLabelIndex + 1; j < textTriggers!.length; j++) { if (trigger instanceof RegExp) {
const trigger = textTriggers![j]! if (trigger.test(content)) {
logger.debug(`Message matched regex (after mode): ${trigger.source}`)
if (trigger instanceof RegExp) { responseConfig = responses[i]!
if (trigger.test(content)) { break
logger.debug(`Message matched regex (after mode): ${trigger.source}`) }
responseConfig = responses[i]! }
break
} }
} }
} }

View File

@@ -23,10 +23,7 @@ export const sendPresetReplyAndLogs = (
]), ]),
) )
export const sendModerationReplyAndLogs = async ( export const sendModerationReplyAndLogs = async (interaction: CommandInteraction | Message, embed: EmbedBuilder) => {
interaction: CommandInteraction | Message,
embed: EmbedBuilder,
) => {
const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch()) const reply = await interaction.reply({ embeds: [embed] }).then(it => it.fetch())
const logChannel = await getLogChannel(interaction.guild!) const logChannel = await getLogChannel(interaction.guild!)
await logChannel?.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] }) await logChannel?.send({ embeds: [applyReferenceToModerationActionEmbed(embed, reply.url)] })