feat: nsfw detection for images attached

This commit is contained in:
2023-03-02 23:07:18 +01:00
parent 1611b5e475
commit 337d739d56
4 changed files with 2329 additions and 114 deletions

63
events/anti-nsfw.ts Normal file
View File

@@ -0,0 +1,63 @@
import { EventType, eventModule } from '@sern/handler';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, Message, TextChannel } from 'discord.js';
import * as tf from '@tensorflow/tfjs-node'
import axios from 'axios';
import { nsfwModel } from '../index.js';
export default eventModule({
type: EventType.Discord,
name: 'messageCreate',
execute(message: Message) {
message.attachments.forEach(async (attachment) => {
switch (attachment.contentType) {
case 'image/png':
break;
case 'image/jpeg':
break;
default:
return;
}
const pic = await axios.get(attachment.url,
{ responseType: 'arraybuffer' }
)
const image = tf.node.decodeImage(pic.data, 3) as tf.Tensor3D
// @ts-ignore
const predictions = await nsfwModel.classify(image)
switch (predictions[0].className) {
case 'Hentai':
case 'Porn':
if (predictions[0].probability > 0.75) {
const embed = new EmbedBuilder()
.setTitle(`Se ha detectado una imagen NSFW en tus adjuntos.`)
.setDescription('Por eso, se ha eliminado tu mensaje.\nPor si es un falso positivo, te vamos a dejar abajo el contenido del mensaje para recuperarlo.')
.setFields(
{ name: 'Contenido del mensaje', value: message.content || '(nada)' },
{ name: 'Tipo', value: predictions[0].className.toString() },
)
.setFooter({ text: 'Esta detección ha sido automatizada.' })
const modLogsEmbed = new EmbedBuilder()
.setAuthor({ name: message.author.username, iconURL: message.author.displayAvatarURL() })
.setTitle(`Se ha detectado una imagen NSFW en los adjuntos de un mensaje.`)
.setDescription('Aquí está toda la información:')
.setFields(
{ name: 'Contenido del mensaje', value: message.content || '(nada)' },
{ name: 'Tipo', value: predictions[0].className.toString() },
)
.setFooter({ text: 'Esta detección ha sido automatizada.' })
const button = new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setLabel('Imagen adjuntada que saltó las alarmas')
.setURL(attachment.url)
.setStyle(ButtonStyle.Link)
)
await message.delete()
await message.author.send({ embeds: [embed], components: [button] })
await (await message.client.channels.fetch(process.env.MODLOGS_CHANNEL!) as TextChannel).send({ embeds: [modLogsEmbed], components: [button] })
}
break;
}
})
},
});

View File

@@ -10,6 +10,8 @@ import birthdays from './util/birthdays.js';
import twitternotifications from './util/twitternotifications.js';
import webserver from './util/web/webserver.js'
import minecraftstatus from './util/minecraftstatus.js';
import * as tf from '@tensorflow/tfjs-node'
import * as nsfw from 'nsfwjs'
// import giveawaychecker from './util/giveawaychecker.js';
let devMode: boolean
@@ -33,6 +35,9 @@ const client = new Client({
],
});
tf.enableProdMode()
export const nsfwModel = await nsfw.load()
mongoose.connect(process.env.MONGODB!).then(() => {
console.log('Connected to MongoDB');
});

2373
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -37,6 +37,7 @@
"@discordjs/voice": "^0.14.0",
"@napi-rs/canvas": "^0.1.30",
"@sern/handler": "^2.5.1",
"@tensorflow/tfjs-node": "^3.20.0",
"axios": "^1.1.3",
"dayjs": "^1.11.6",
"discord-tictactoe": "^4.0.0",
@@ -50,6 +51,7 @@
"got": "^12.5.3",
"libsodium-wrappers": "^0.7.10",
"mongoose": "^6.5.1",
"nsfwjs": "^2.4.2",
"pretty-seconds-spanish": "^2.1.1",
"rockpaperscissors-checker": "^1.2.0",
"set-interval-async": "^3.0.2",