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]
if (!mimeType) return void logger.warn(`No MIME type for attachment: ${attachment.url}`)
if (
config.attachments.allowedMimeTypes &&
!config.attachments.allowedMimeTypes.includes(mimeType)
) {
if (config.attachments.allowedMimeTypes && !config.attachments.allowedMimeTypes.includes(mimeType)) {
logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${mimeType}`)
continue
}
@@ -86,10 +83,14 @@ withContext(on, 'messageCreate', async (context, msg) => {
if (isTextFile) {
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 {
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) {

View File

@@ -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'>,
flags: { onlyImageTriggers?: boolean; skipApiRequest?: boolean } = {}
flags: { imageTriggersOnly?: boolean; textRegexesOnly?: 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 (flags.onlyImageTriggers) {
if (flags.imageTriggersOnly) {
if (imageTriggers)
for (const regex of imageTriggers)
if (regex.test(content)) {
@@ -39,7 +39,7 @@ export const getResponseFromText = async (
responseConfig = trigger
break
}
} else
} else {
for (let j = 0; j < textTriggers!.length; j++) {
const regex = textTriggers![j]!
@@ -54,20 +54,22 @@ export const getResponseFromText = async (
break
}
}
}
// 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')
const scan = await api.client.parseText(content)
if (scan.labels.length) {
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
const response = responses.find(x => {
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
@@ -87,7 +89,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 && flags.onlyImageTriggers) {
if (!responseConfig.triggers && !flags.imageTriggersOnly) {
logger.debug('No match from NLP, doing after regexes')
for (let i = 0; i < responses.length; i++) {
const {
@@ -108,6 +110,8 @@ export const getResponseFromText = async (
}
}
}
}
}
return responseConfig
}

View File

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