I believe NSFW PFP scanning now works. Please could reviewers test this using either server profiles or isolated alts

pull/42/head
Skyler Grey 3 years ago
parent 96fcc75c5f
commit 7416964f43
Signed by: Minion3665
GPG Key ID: 1AFD10256B3C714D

@ -114,7 +114,7 @@ export async function callback(_client: NucleusClient, message: Message) {
if ( if (
config.filters.images.NSFW && config.filters.images.NSFW &&
!(message.channel instanceof ThreadChannel ? message.channel.parent?.nsfw : message.channel.nsfw) && !(message.channel instanceof ThreadChannel ? message.channel.parent?.nsfw : message.channel.nsfw) &&
(await NSFWCheck(element)) (await NSFWCheck(element.url))
) { ) {
messageException(message.guild.id, message.channel.id, message.id); messageException(message.guild.id, message.channel.id, message.id);
await message.delete(); await message.delete();

@ -30,12 +30,8 @@ const clamscanner = await new ClamScan().init({
} }
}); });
export async function testNSFW(attachment: { export async function testNSFW(url: string): Promise<NSFWSchema> {
url: string; const [fileStream, hash] = await streamAttachment(url);
height: number | null;
width: number | null;
}): Promise<NSFWSchema> {
const [fileStream, hash] = await streamAttachment(attachment.url);
const alreadyHaveCheck = await client.database.scanCache.read(hash); const alreadyHaveCheck = await client.database.scanCache.read(hash);
if (alreadyHaveCheck && "nsfw" in alreadyHaveCheck!) { if (alreadyHaveCheck && "nsfw" in alreadyHaveCheck!) {
return { nsfw: alreadyHaveCheck.nsfw }; return { nsfw: alreadyHaveCheck.nsfw };
@ -49,11 +45,11 @@ export async function testNSFW(attachment: {
resolve(buf); resolve(buf);
}) })
)) as Buffer; )) as Buffer;
const array = new Uint8Array(converted);
const img = tf.node.decodeImage(array) as tf.Tensor3D; const img = tf.node.decodeImage(converted, 3, undefined, false) as tf.Tensor3D;
const predictions = (await nsfw_model.classify(img, 1))[0]!; const predictions = (await nsfw_model.classify(img, 1))[0]!;
img.dispose();
console.log(2, predictions); console.log(2, predictions);
const nsfw = predictions.className === "Hentai" || predictions.className === "Porn"; const nsfw = predictions.className === "Hentai" || predictions.className === "Porn";
@ -155,13 +151,9 @@ export async function LinkCheck(message: Discord.Message): Promise<string[]> {
return detectionsTypes as string[]; return detectionsTypes as string[];
} }
export async function NSFWCheck(element: { export async function NSFWCheck(url: string): Promise<boolean> {
url: string;
height: number | null;
width: number | null;
}): Promise<boolean> {
try { try {
return (await testNSFW(element)).nsfw; return (await testNSFW(url)).nsfw;
} catch (e) { } catch (e) {
console.log(e); console.log(e);
return false; return false;
@ -237,7 +229,7 @@ export async function doMemberChecks(member: Discord.GuildMember): Promise<void>
console.log(4, avatarTextCheck); console.log(4, avatarTextCheck);
// Is the profile picture NSFW // Is the profile picture NSFW
const avatar = member.displayAvatarURL({ extension: "png", size: 1024, forceStatic: true }); const avatar = member.displayAvatarURL({ extension: "png", size: 1024, forceStatic: true });
const avatarCheck = guildData.filters.images.NSFW && (await NSFWCheck({ url: avatar, height: 1024, width: 1024 })); const avatarCheck = guildData.filters.images.NSFW && (await NSFWCheck(avatar));
console.log(5, avatarCheck); console.log(5, avatarCheck);
// Does the username contain an invite // Does the username contain an invite
const inviteCheck = guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.user.username); const inviteCheck = guildData.filters.invite.enabled && /discord\.gg\/[a-zA-Z0-9]+/gi.test(member.user.username);

@ -121,11 +121,9 @@ export default async function (interaction: CommandInteraction | ButtonInteracti
] ]
}); });
if ( if (
await NSFWCheck({ await NSFWCheck(
url: (interaction.member as GuildMember).user.displayAvatarURL({ extension: "png", forceStatic: true }), (interaction.member as GuildMember).user.displayAvatarURL({ extension: "png", forceStatic: true })
height: 1024, )
width: 1024
})
) { ) {
return await interaction.editReply({ return await interaction.editReply({
embeds: [ embeds: [

Loading…
Cancel
Save