From 1ddd17247819bf2e07f9f81abf097fcf9f01f047 Mon Sep 17 00:00:00 2001 From: Max Litruv Boonzaayer Date: Thu, 4 Apr 2019 13:34:46 +1100 Subject: [PATCH] Added safe images --- package.json | 3 +- plugins/safeimage.js | 82 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 plugins/safeimage.js diff --git a/package.json b/package.json index ab44ce6..cae04b8 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "dependencies": { + "@google-cloud/vision": "^0.25.0", "colors": "^1.3.3", "discord.js": "^11.4.2", "mysql": "^2.16.0", @@ -15,4 +16,4 @@ }, "author": "", "license": "ISC" -} \ No newline at end of file +} diff --git a/plugins/safeimage.js b/plugins/safeimage.js new file mode 100644 index 0000000..29b5b79 --- /dev/null +++ b/plugins/safeimage.js @@ -0,0 +1,82 @@ +module.exports = { + name: "Google Safe Image", + init: (dclient) => { + client = dclient + + client.on('message', msg => { + if (msg.author.bot) return + if (msg.attachments.array().length = 0) return + var img = msg.attachments.first() + if (img.height == undefined) return + if (msg.channel.nsfw) return + + detectSafeSearch(img.proxyURL, results => { + var score = 0; + if (results.safeSearchAnnotation.adult == "VERY_LIKELY" || results.safeSearchAnnotation.adult == "LIKELY") { + score = "adult" + } + if (results.safeSearchAnnotation.racy == "VERY_LIKELY" || results.safeSearchAnnotation.racy == "LIKELY") { + score = "racy" + } + + if (score != 0) { + var stringmsg = `Adult: ${results.safeSearchAnnotation.adult} \n Medical: ${results.safeSearchAnnotation.medical}\n Spoof: ${results.safeSearchAnnotation.spoof}\n Violence: ${results.safeSearchAnnotation.violence}\n Racy: ${results.safeSearchAnnotation.racy}`; + stringmsg += "\n\n Tags: " + for (var i = 0; i < results.labelAnnotations.length; i++) { + stringmsg += results.labelAnnotations[i].description + if (i + 1 < results.labelAnnotations.length) + stringmsg += ", " + } + // msg.reply(stringmsg) + msg.reply("Oi, you can't have that " + score + " " + results.labelAnnotations[0].description.toLowerCase() + " here 😢") + } + }) + + + + }) + } +} + +async function detectSafeSearch(fileName, cb) { + // [START vision_safe_search_detection] + const vision = require('@google-cloud/vision'); + + // Creates a client + const client = new vision.ImageAnnotatorClient({ + projectId: 'causal-galaxy-207206', + keyFilename: 'C:/dev/myproject.json' + }); + + /** + * TODO(developer): Uncomment the following line before running the sample. + */ + // const fileName = 'Local image file, e.g. /path/to/image.png'; + + // Performs safe search detection on the local file + console.log(client.features) + const request = { + + "image": { + "source": { + "imageUri": fileName + } + }, + + features: [ + { + type: "LABEL_DETECTION", + }, + { + type: "SAFE_SEARCH_DETECTION", + } + ] + } + + + + const [result] = await client.annotateImage(request); + console.log(result); + //const detections = result.safeSearchAnnotation; + cb(result); +}