feat(bots/discord)!: add more attachment scan options

This commit is contained in:
PalmDevs
2024-08-02 19:04:45 +07:00
parent 6875b32fd0
commit b9d08fff64
2 changed files with 19 additions and 5 deletions

View File

@@ -26,7 +26,11 @@ export type Config = {
messageScan?: { messageScan?: {
scanBots?: boolean scanBots?: boolean
scanOutsideGuilds?: boolean scanOutsideGuilds?: boolean
allowedAttachmentMimeTypes: string[] attachments?: {
scanAttachments?: boolean
allowedMimeTypes?: string[]
maxTextFileSize?: number
}
filter?: { filter?: {
whitelist?: Filter whitelist?: Filter
blacklist?: Filter blacklist?: Filter

View File

@@ -39,7 +39,7 @@ withContext(on, 'messageCreate', async (context, msg) => {
embeds: response.embeds?.map(createMessageScanResponseEmbed), embeds: response.embeds?.map(createMessageScanResponseEmbed),
}) })
if (label) if (label) {
db.insert(responses).values({ db.insert(responses).values({
replyId: reply.id, replyId: reply.id,
channelId: reply.channel.id, channelId: reply.channel.id,
@@ -49,7 +49,6 @@ withContext(on, 'messageCreate', async (context, msg) => {
content: msg.content, content: msg.content,
}) })
if (label) {
for (const reaction of Object.values(MessageScanLabeledResponseReactions)) { for (const reaction of Object.values(MessageScanLabeledResponseReactions)) {
await reply.react(reaction) await reply.react(reaction)
} }
@@ -60,11 +59,22 @@ withContext(on, 'messageCreate', async (context, msg) => {
} }
} }
if (msg.attachments.size > 0) { if (msg.attachments.size > 0 && config.attachments?.scanAttachments) {
logger.debug(`Classifying message attachments for ${msg.id}`) logger.debug(`Classifying message attachments for ${msg.id}`)
for (const attachment of msg.attachments.values()) { for (const attachment of msg.attachments.values()) {
if (attachment.contentType && !config.allowedAttachmentMimeTypes.includes(attachment.contentType)) continue if (
config.attachments.allowedMimeTypes &&
!config.attachments.allowedMimeTypes.includes(attachment.contentType!)
) {
logger.debug(`Disallowed MIME type for attachment: ${attachment.url}, ${attachment.contentType}`)
continue
}
if (attachment.contentType?.startsWith('text/') && attachment.size > (config.attachments.maxTextFileSize ?? 512 * 1000)) {
logger.debug(`Attachment ${attachment.url} is too large be to scanned, size is ${attachment.size}`)
continue
}
try { try {
const { text: content } = await api.client.parseImage(attachment.url) const { text: content } = await api.client.parseImage(attachment.url)