├── .gitignore ├── logs └── logfile.log ├── config-example.json ├── src ├── Utils │ ├── MinIO.ts │ └── Utils.ts ├── index.ts ├── WebServer.ts └── Handler │ └── ImageClassificationHandler.ts ├── LICENSE.md ├── package.json ├── README.md └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /config.json 3 | .idea 4 | /logs/ 5 | /dist/ 6 | -------------------------------------------------------------------------------- /logs/logfile.log: -------------------------------------------------------------------------------- 1 | In this folder, you will find the logs generated by the application. The logs are stored in the following format: `log-. -------------------------------------------------------------------------------- /config-example.json: -------------------------------------------------------------------------------- 1 | { 2 | "debug": true, 3 | "webServer": { 4 | "port": 10082 5 | }, 6 | "s3": { 7 | "endPoint": "s3.example.com", 8 | "useSSL": true, 9 | "port": 4242, 10 | "bucket": "media", 11 | "accessKey": "", 12 | "secretKey": "" 13 | }, 14 | "openAI": { 15 | "key": "sk-**********" 16 | }, 17 | "discord": { 18 | "webhook": "https://discord.com/api/webhooks/1234567890/abcdefg" 19 | }, 20 | "status": { 21 | "enabled": false, 22 | "id": "", 23 | "kumaBase": "https://uptime.example.com", 24 | "interval": 60000 25 | } 26 | } -------------------------------------------------------------------------------- /src/Utils/MinIO.ts: -------------------------------------------------------------------------------- 1 | import {Utils, logLevel, logModule} from "./Utils"; 2 | import * as Minio from 'minio' 3 | import {Client} from "minio"; 4 | let s3: Client | null | undefined = null; 5 | 6 | export class MinIO { 7 | private readonly s3: Client; 8 | constructor(config: { s3: { endPoint: any; accessKey: any; secretKey: any; port?: number; useSSL: boolean }; }, noLog = false) { 9 | Utils.log(logLevel.INFO, logModule.MinIO, "Connecting to MinIO"); 10 | this.s3 = new Minio.Client({ 11 | endPoint: config.s3.endPoint, 12 | port: config.s3.useSSL ? undefined : config.s3.port, 13 | region: 'eu-central-1', 14 | useSSL: true, 15 | accessKey: config.s3.accessKey, 16 | secretKey: config.s3.secretKey 17 | }); 18 | s3 = this.s3; 19 | Utils.log(logLevel.SUCCESS, logModule.MinIO, "Connected to MinIO"); 20 | } 21 | 22 | getS3() { 23 | return this.s3; 24 | } 25 | 26 | static getS3(): Client { 27 | return s3; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 GalaxyBot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import {WebServer} from './WebServer'; 2 | import config from "../config.json"; 3 | import {Utils, logLevel, logModule} from "./Utils/Utils"; 4 | import {MinIO} from "./Utils/MinIO"; 5 | import axios from "axios"; 6 | 7 | async function start() { 8 | Utils.log(logLevel.INFO, logModule.GALAXYBOT, "Starting GalaxyBot- NSFW Classifier"); 9 | new MinIO(config); 10 | new WebServer().start(); 11 | } 12 | 13 | start().then(async () => { 14 | // Ready for Pterodactyl JS Egg 15 | console.log("Ready!") 16 | Utils.log(logLevel.SUCCESS, logModule.GALAXYBOT, "GalaxyBot - NSFW Classifier started"); 17 | 18 | if (config.status.enabled) { 19 | await status(); 20 | setInterval(async () => { 21 | await status(); 22 | }, config.status.interval) 23 | } 24 | }).catch((e) => { 25 | Utils.log(logLevel.ERROR, logModule.GALAXYBOT, e); 26 | process.exit(1); 27 | }); 28 | 29 | async function status() { 30 | await axios.get(`${config.status.kumaBase}/api/push/${config.status.id}?status=up&msg=OK&ping=42`).catch((e) => { 31 | Utils.log(logLevel.ERROR, logModule.GALAXYBOT, "Error while sending status to Kuma: ", e); 32 | }) 33 | } 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "image-classifier", 3 | "version": "2.0.5", 4 | "description": "Image classifier using OpenAI Moderation API", 5 | "main": "src/index.ts", 6 | "scripts": { 7 | "dev": "ts-node src/index.ts", 8 | "build": "tsc && node -e \"require('fs').copyFileSync('config.json', 'dist/config.json')\"", 9 | "start": "tsc && node -e \"require('fs').copyFileSync('config.json', 'dist/config.json')\" && node dist/index.js", 10 | "prod": "node dist/index.js" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://github.com/GalaxyBotTeam/Image-Classifier.git" 15 | }, 16 | "author": "GalaxyBot Team", 17 | "license": "MIT", 18 | "bugs": { 19 | "url": "https://github.com/GalaxyBotTeam/Image-Classifier.git/issues" 20 | }, 21 | "homepage": "https://galaxybot.app", 22 | "dependencies": { 23 | "axios": "1.8.3", 24 | "chalk": "^5.4.1", 25 | "cors": "^2.8.5", 26 | "express": "5.0.0", 27 | "minio": "^8.0.5", 28 | "openai": "^4.87.4" 29 | }, 30 | "devDependencies": { 31 | "@types/bun": "latest", 32 | "@types/cors": "^2.8.17", 33 | "@types/express": "^5.0.0", 34 | "@types/minio": "^7.1.1", 35 | "typescript": "^5.8.2", 36 | "ts-node": "^10.9.2" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Utils/Utils.ts: -------------------------------------------------------------------------------- 1 | import chalk from "chalk"; 2 | import fs from 'fs'; 3 | import axios from "axios"; 4 | 5 | export const logLevel = { 6 | SUCCESS: " [ " + chalk.greenBright("SUCCESS") + " ]", // [ SUCCESS ]# 7 | DEBUG: " [ " + chalk.cyan("DEBUG") + " ] ", // [ DEBUG ] # 8 | INFO: " [ " + chalk.blueBright("INFO") + " ] ", // [ INFO ] # 9 | WARN: " [ " + chalk.yellow("WARN") + " ] ", // [ WARN ] # 10 | ERROR: " [ " + chalk.bgRed.bold.white("ERROR") + " ] " // [ ERROR ] # 11 | 12 | } 13 | 14 | const rawLogModules = { 15 | WEBSERVER: chalk.bold.magenta("WebServer"), 16 | GALAXYBOT: chalk.bold.blue("GalaxyBot"), 17 | OPENAI: chalk.bold.cyan("OpenAI"), 18 | MinIO: chalk.bold.red("MinIO"), 19 | WEBHOOK: chalk.bold.yellow("Webhook"), 20 | } 21 | 22 | // Calculate the maximum length of the longest module name (without ANSI color codes) 23 | const maxLength = Math.max( 24 | ...Object.values(rawLogModules).map(m => m.replace(/\x1B\[[0-9;]*m/g, "").length) 25 | ); 26 | 27 | // Bring back typesafety by mapping the rawLogModules to a new object 28 | export const logModule: Record = Object.fromEntries( 29 | Object.entries(rawLogModules).map(([key, value]) => { 30 | const plainTextLength = value.replace(/\x1B\[[0-9;]*m/g, "").length; 31 | const padding = " ".repeat(maxLength - plainTextLength); 32 | return [key, ` [ ${value} ]${padding}`]; 33 | }) 34 | ) as Record; 35 | 36 | 37 | 38 | export class Utils { 39 | static #client = null; 40 | 41 | 42 | /** 43 | * 44 | * @param logLevel {logLevel} The Loglevel with the messages should be logged 45 | * @param logModule {logModule} The LogModule is the Type or the Module where the Log are from 46 | * @param messages {String} Messages can be an Array of messages like console.log (e.g. "message1", "message2", "message3", ...) 47 | * @returns {void} 48 | */ 49 | static log(logLevel: string, logModule: string, ...messages: string[]): void { 50 | let message: string | null = null; 51 | messages.forEach((msg) => { 52 | if (message == null) { 53 | message = msg; 54 | } else { 55 | message += " " + msg; 56 | } 57 | }); 58 | 59 | 60 | let date = new Date().toISOString().toString().split("T")[0]; 61 | let logFile = fs.createWriteStream('logs/logfile-' + date + '.log', {flags: 'a'}); 62 | let logLevel2 = logLevel.toString().replaceAll(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "") 63 | let logModule2 = logModule.toString().replaceAll(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "") 64 | 65 | if (!logModule2.includes("REDIS")) { 66 | logFile.write(Utils.getTimestamp() + logLevel2 + logModule2 + ": " + message + '\n'); 67 | } 68 | console.log(this.getTimestamp() + logLevel + logModule + ":", message) 69 | 70 | 71 | let dateBefore2Weeks = new Date(new Date().getTime() - 1209600000).toISOString().toString().split("T")[0]; 72 | if (fs.existsSync('logs/logfile-' + dateBefore2Weeks + '.log')) { 73 | fs.unlink('logs/logfile-' + dateBefore2Weeks + '.log', (err: any) => { 74 | console.log(err) 75 | }); 76 | } 77 | } 78 | 79 | 80 | static getTimestamp() { 81 | 82 | return new Date().toLocaleString('de-DE', { 83 | timeZone: "Europe/Berlin", 84 | year: 'numeric', 85 | day: '2-digit', 86 | month: '2-digit', 87 | hour: '2-digit', 88 | minute: '2-digit', 89 | second: '2-digit' 90 | })?.replace(".", '-')?.replace(".", '-')?.replace(",", ""); 91 | } 92 | 93 | static sendDiscordWebhook(webhook: any, data: any) { 94 | let options = { 95 | method: 'POST', 96 | url: webhook, 97 | data: data 98 | }; 99 | 100 | axios.request(options).then(function () { 101 | }).catch(function (error: any) { 102 | Utils.log(logLevel.ERROR, logModule.WEBHOOK, "Error sending Discord Webhook: " + error); 103 | }); 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | GalaxyBot 2 | 3 | # GalaxyBot Image Classification 4 | 5 | This project is a TypeScript application that uses the OpenAI Moderation API for image classification. It's designed to detect and handle NSFW (Not Safe For Work) images uploaded to an S3 bucket. 6 | 7 | ## Getting Started 8 | 9 | These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. 10 | 11 | ### Prerequisites 12 | 13 | - Node.js or Bun 14 | - npm (or Bun) 15 | - TypeScript 16 | 17 | ### Installing 18 | 19 | 1. Clone the repository 20 | ```bash 21 | git clone https://github.com/GalaxyBotTeam/Image-Classifier.git 22 | ``` 23 | 24 | 2. Navigate to the project directory 25 | ```bash 26 | cd your-project-directory 27 | ``` 28 | 29 | 3. Install the dependencies 30 | ```bash 31 | npm install 32 | # OR using Bun 33 | bun install 34 | ``` 35 | 36 | ## Usage 37 | 38 | This project consists of two main components: 39 | 40 | 1. `ImageClassificationHandler.ts`: This handles the image classification process. It fetches an image from an S3 bucket, converts it into a TensorFlow tensor, classifies the image using the NSFW.js model, and validates the classification results. 41 | 42 | ### Configuration 43 | The application needs a configuration file to run. Create a `config.json` file in the root directory of the project with the following content: 44 | The content for the `config.json` is defined in the `config-example.json` file. 45 | 46 | We use MinIO as an S3-compatible object storage server. You can use any S3-compatible object storage server by changing the `s3` configuration in the `config.json` file. 47 | 48 | ### Building and Running the application 49 | 50 | #### Using npm 51 | Build the TypeScript files: 52 | ```bash 53 | npm run build 54 | ``` 55 | 56 | Start the application: 57 | ```bash 58 | npm start 59 | ``` 60 | 61 | Development mode (with auto-reloading): 62 | ```bash 63 | npm run dev 64 | ``` 65 | 66 | #### Using Bun 67 | Build the TypeScript files: 68 | ```bash 69 | bun run build 70 | ``` 71 | 72 | Start the application: 73 | ```bash 74 | bun start 75 | ``` 76 | 77 | Development mode (with auto-reloading): 78 | ```bash 79 | bun run dev 80 | ``` 81 | 82 | ### Classifying an image 83 | The application provides a Webserver that listens for POST requests on the `/api/v1/classifyImage` endpoint. To classify an image, send a POST request to the `/api/v1/classifyImage` endpoint with the following payload: 84 | 85 | #### Request 86 | ```json 87 | { 88 | "key": "your-s3-key", //The key of the image in the S3 bucket. The bucket is defined in the config file 89 | "deleteOnClassification": false //Boolean, should the image automaticly deleted if nsfw has been detected? 90 | "metadata": { 91 | "userID": "userID", //Guild ID for Discord Log 92 | "guildID": "guildID" //User ID for Discord Log 93 | } 94 | } 95 | ``` 96 | #### Response: 97 | Returns a JSON object with the classification results. The `flagged` field indicates the classification result. The `categories` and the `scores` field contains the classification probabilities for each category. 98 | In this example, the image is classified as `sexual` with a probability of `0.9849382780471674`. 99 | DeleteImage is true if the image has been deleted from the S3 bucket. 100 | ```json 101 | { 102 | "flagged": true, 103 | "categories": { 104 | "harassment": false, 105 | "harassment/threatening": false, 106 | "sexual": true, 107 | "hate": false, 108 | "hate/threatening": false, 109 | "illicit": false, 110 | "illicit/violent": false, 111 | "self-harm/intent": false, 112 | "self-harm/instructions": false, 113 | "self-harm": false, 114 | "sexual/minors": false, 115 | "violence": false, 116 | "violence/graphic": false 117 | }, 118 | "scores": { 119 | "harassment": 0, 120 | "harassment/threatening": 0, 121 | "sexual": 0.9849382780471674, 122 | "hate": 0, 123 | "hate/threatening": 0, 124 | "illicit": 0, 125 | "illicit/violent": 0, 126 | "self-harm/intent": 0.0002785803623326577, 127 | "self-harm/instructions": 0.0002318797868593433, 128 | "self-harm": 0.004690984132226771, 129 | "sexual/minors": 0, 130 | "violence": 0.12459626487974881, 131 | "violence/graphic": 0.002384378805524485 132 | }, 133 | "deletedImage": true 134 | } 135 | ``` 136 | 137 | ## License 138 | 139 | This project is licensed under the MIT License - see the `LICENSE.md` file for details. 140 | -------------------------------------------------------------------------------- /src/WebServer.ts: -------------------------------------------------------------------------------- 1 | import cors from "cors"; 2 | import {Utils, logLevel, logModule} from "./Utils/Utils"; 3 | import config from "../config.json"; 4 | import {ImageClassificationHandler} from "./Handler/ImageClassificationHandler"; 5 | import {json} from "express"; 6 | import express, {NextFunction, Request, Response, Express} from "express"; 7 | 8 | export type WebServerResponse = { 9 | status: number, 10 | message: string, 11 | data: unknown 12 | } 13 | 14 | export class WebServer { 15 | start() { 16 | Utils.log(logLevel.INFO, logModule.WEBSERVER, "Starting Webserver"); 17 | 18 | const app = express() 19 | const port = config.webServer.port 20 | const imageClassificationHandler = new ImageClassificationHandler(); 21 | 22 | // 23 | // Init Webserver for own requests 24 | // 25 | app.use(express.json()) 26 | app.use(cors(), (req: Request, res: Response, next: NextFunction) => { 27 | res.setHeader("X-Powered-By", "GalaxyBot") 28 | res.setHeader("Access-Control-Allow-Origin", "127.0.0.1"); 29 | res.setHeader("Content-Type", "application/json"); 30 | let reqUri = req.originalUrl.toString().length > 61 ? req.originalUrl.toString().substring(0, 61) + "..." : req.originalUrl 31 | if(config.debug) Utils.log(logLevel.DEBUG, logModule.WEBSERVER, "RequestURI " + reqUri) 32 | next(); 33 | }); 34 | 35 | 36 | // 37 | //Routes 38 | // 39 | 40 | // Body = {key: "key", metaData: {userID: "userID", guildID: "guildID"}, deleteByClassification: true} 41 | // Key must be an object key from the S3 Bucket 42 | // 43 | // ID's in metaData are Discord snowflakes for de Logging Channel 44 | app.post("/api/v1/classifyImage", checkHost, (req: Request, res: Response) => { 45 | // Check if all required parameters are given 46 | if (req.body.key && req.body.metaData.userID && req.body.metaData.guildID && req.body.deleteByClassification != null){ 47 | // Call the ImageClassificationHandler 48 | imageClassificationHandler.classifyImage(req.body.key, req.body.metaData, req.body.deleteByClassification).then((results) => { 49 | res.status(200) 50 | res.write(JSON.stringify(results)) 51 | res.end() 52 | }).catch((error) => { 53 | Utils.log(logLevel.ERROR, logModule.WEBSERVER, "In Webserver", error?.message) 54 | res.status(500) 55 | res.write(JSON.stringify({ 56 | status: 500, 57 | message: "Internal Server Error", 58 | data: { 59 | error: error?.message 60 | } 61 | })) 62 | res.end() 63 | }) 64 | } else { 65 | res.status(400) 66 | res.write(JSON.stringify({ 67 | status: 400, 68 | message: "Bad Request", 69 | data: { 70 | error: "Missing Parameters" 71 | } 72 | })) 73 | res.end() 74 | } 75 | }); 76 | 77 | 78 | //Start the WebServer 79 | app.listen(port, () => { 80 | Utils.log(logLevel.SUCCESS, logModule.WEBSERVER, "WebServer started on port " + port) 81 | }); 82 | 83 | 84 | // 85 | // Middleware 86 | // 87 | /** 88 | * Check the Host Header 89 | * @param req - Request Object from Express 90 | * @param res - Response Object from Express 91 | * @param next - Next Function from Express 92 | * 93 | * We use this function to check if the request is coming from the right host for security reasons you can remove this if you want 94 | */ 95 | function checkHost(req: Request, res: Response, next: NextFunction) { 96 | // Localhost == Testing with Insomnia 97 | // 172.18.0.1 == Bot 98 | if(req.headers.host === `localhost:${config.webServer.port}` || req.headers.host === `172.18.0.1:${config.webServer.port}`) { 99 | next(); 100 | } else { 101 | res.status(403) 102 | res.write('{"error": "Invalid Host"}') 103 | res.end() 104 | } 105 | } 106 | 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/Handler/ImageClassificationHandler.ts: -------------------------------------------------------------------------------- 1 | import {MinIO} from "../Utils/MinIO"; 2 | import config from "../../config.json"; 3 | import openAI from "openai"; 4 | import {Client} from "minio"; 5 | import {logLevel, logModule, Utils} from "../Utils/Utils"; 6 | import {WebServerResponse} from "../WebServer"; 7 | 8 | type metaData = { 9 | userID: string, 10 | guildID: string 11 | } 12 | 13 | type ModerationCategory = 14 | | "harassment" 15 | | "harassment/threatening" 16 | | "sexual" 17 | | "hate" 18 | | "hate/threatening" 19 | | "illicit" 20 | | "illicit/violent" 21 | | "self-harm/intent" 22 | | "self-harm/instructions" 23 | | "self-harm" 24 | | "sexual/minors" 25 | | "violence" 26 | | "violence/graphic"; 27 | 28 | type ModerationCategories = { 29 | [category in ModerationCategory]: boolean; 30 | }; 31 | 32 | type ModerationScores = { 33 | [category in ModerationCategory]: number; 34 | }; 35 | 36 | type ModerationResult = { 37 | flagged: boolean; 38 | categories: ModerationCategories; 39 | scores: ModerationScores; 40 | }; 41 | 42 | export class ImageClassificationHandler { 43 | private readonly bucket: string; 44 | private readonly s3: Client; 45 | private openAI: any; 46 | 47 | 48 | constructor() { 49 | this.s3 = MinIO.getS3(); 50 | this.bucket = config.s3.bucket; 51 | this.openAI = new openAI({ 52 | apiKey: config.openAI.key 53 | }); 54 | 55 | } 56 | 57 | /** 58 | * Classify an image from the S3 Bucket 59 | * @param key - S3 Key of the image 60 | * @param metaData - MetaData from the request (userID, guildID) 61 | * @param deleteImage - Delete the image in S3 if it's NSFW 62 | * @returns {Promise} - Returns the classification results 63 | */ 64 | async classifyImage(key: string, metaData: metaData, deleteImage = false): Promise { 65 | return new Promise(async (resolve, reject) => { 66 | Utils.log(logLevel.INFO, logModule.MinIO, `Downloading Image: ${key}`); 67 | this.s3.getObject(this.bucket, key).then(async (data) => { 68 | const buffer = await this.streamToBuffer(data); 69 | 70 | // Create a DataURI from the buffer depending on the file type 71 | const dataURI = `data:image/${key.split(".")[1]};base64,${buffer.toString('base64')}`; 72 | 73 | Utils.log(logLevel.INFO, logModule.OPENAI, `Classifying Image: ${key}`); 74 | 75 | return await this.openAI.moderations.create({ 76 | model: "omni-moderation-latest", 77 | input: [ 78 | { 79 | type: "image_url", 80 | image_url: { 81 | url: dataURI 82 | } 83 | } 84 | ] 85 | }).then(async (data: any) => { 86 | if (data?.results[0]) { 87 | const predictions = data.results[0]; 88 | Utils.log(logLevel.INFO, logModule.OPENAI, `Image has ${predictions.flagged ? "been flagged" : "not been flagged"} as NSFW`); 89 | 90 | if (predictions.flagged) { 91 | if (deleteImage) { 92 | // Delete the image from the S3 Bucket 93 | this.s3.removeObject(this.bucket, key).then(() => { 94 | Utils.log(logLevel.INFO, logModule.MinIO, `Deleted Image by classification: ${key}`); 95 | }); 96 | } 97 | 98 | this.logToDiscord(metaData, key, { 99 | flagged : predictions.flagged, 100 | categories: predictions.categories, 101 | scores: predictions.category_scores, 102 | }, deleteImage); 103 | } 104 | 105 | resolve({ 106 | status: 200, 107 | message: "Image Classified", 108 | data: { 109 | key: key, 110 | bucket: this.bucket, 111 | flagged: predictions.flagged, 112 | categories: predictions.categories, 113 | scores: predictions.category_scores, 114 | deletedImage: predictions.flagged && deleteImage 115 | } 116 | }) 117 | } else { 118 | reject({ 119 | status: 500, 120 | message: "Error Classifying Image with OpenAI", 121 | data: { 122 | key: key, 123 | bucket: this.bucket, 124 | error: "No results found" 125 | } 126 | }) 127 | } 128 | }).catch((err: any) => { 129 | Utils.log(logLevel.ERROR, logModule.OPENAI, err); 130 | reject({ 131 | status: 500, 132 | message: "Error Classifying Image with OpenAI", 133 | data: { 134 | key: key, 135 | bucket: this.bucket, 136 | error: err?.message 137 | } 138 | }) 139 | }) 140 | }).catch((e) => { 141 | console.log(e) 142 | reject({ 143 | status: 404, 144 | message: "Image not found in S3 Bucket", 145 | data: { 146 | key: key, 147 | bucket: this.bucket, 148 | error: e?.message 149 | } 150 | }); 151 | }); 152 | 153 | }); 154 | 155 | } 156 | 157 | streamToBuffer(stream: any): Promise { 158 | return new Promise((resolve, reject) => { 159 | const chunks: any[] = []; 160 | stream.on('data', (chunk: any) => chunks.push(chunk)); 161 | stream.on('error', reject); 162 | stream.on('end', () => resolve(Buffer.concat(chunks))); 163 | }); 164 | } 165 | 166 | logToDiscord(metaData: metaData, key: string, predictions: ModerationResult, deleteImage: boolean) { 167 | const embedData = { 168 | "content": null, 169 | "embeds": [ 170 | { 171 | "title": "Image has been Flagged", 172 | "description": `Uploaded Image by <@${metaData.userID}> has been flagged with following details`, 173 | "color": null, 174 | "fields": [ 175 | {name: "User", value: `<@${metaData.userID}>`, inline: true}, 176 | { 177 | name: "Guild", 178 | value: `[${metaData.guildID}](https://dash.galaxybot.app/server/${metaData.guildID})`, 179 | inline: true 180 | }, 181 | { 182 | name: "Image", 183 | value: deleteImage ? "*Deleted*" : `[Link](https://s3.galaxybot.app/${this.bucket}/${key})`, 184 | inline: true 185 | }, 186 | { 187 | name: "Action", 188 | value: deleteImage ? "Image has been deleted" : "Image has been kept", 189 | inline: true 190 | }, 191 | ...Object.entries(predictions.scores).map(([name, value]) => ({ 192 | name, 193 | value: Math.floor(value * 100) + "%", 194 | inline: true 195 | }))], 196 | "timestamp": new Date().toISOString(), 197 | "image": { 198 | "url": deleteImage ? null : `https://s3.galaxybot.app/${this.bucket}/${key}` 199 | } 200 | } 201 | ], 202 | "attachments": [] 203 | }; 204 | 205 | // Send the embed to the Discord Webhook 206 | Utils.sendDiscordWebhook(config.discord.webhook, embedData); 207 | } 208 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig to read more about this file */ 4 | 5 | /* Projects */ 6 | // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ 7 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 8 | // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ 9 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ 10 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 11 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 12 | 13 | /* Language and Environment */ 14 | "target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 15 | // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 16 | // "jsx": "preserve", /* Specify what JSX code is generated. */ 17 | // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ 18 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 19 | // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ 20 | // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 21 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ 22 | // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ 23 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 24 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 25 | // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ 26 | 27 | /* Modules */ 28 | "module": "commonjs", /* Specify what module code is generated. */ 29 | "rootDir": "./src", /* Specify the root folder within your source files. */ 30 | // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ 31 | // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ 32 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 33 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 34 | // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ 35 | // "types": [], /* Specify type package names to be included without being referenced in a source file. */ 36 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 37 | // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ 38 | // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ 39 | // "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */ 40 | // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ 41 | // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ 42 | // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ 43 | // "noUncheckedSideEffectImports": true, /* Check side effect imports. */ 44 | "resolveJsonModule": true, /* Enable importing .json files. */ 45 | // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ 46 | // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ 47 | 48 | /* JavaScript Support */ 49 | // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ 50 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 51 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ 52 | 53 | /* Emit */ 54 | // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 55 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 56 | // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 57 | // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ 58 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 59 | // "noEmit": true, /* Disable emitting files from a compilation. */ 60 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ 61 | "outDir": "./dist", /* Specify an output folder for all emitted files. */ 62 | // "removeComments": true, /* Disable emitting comments. */ 63 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 64 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 65 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 66 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 67 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 68 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 69 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 70 | // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ 71 | // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ 72 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 73 | // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ 74 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 75 | 76 | /* Interop Constraints */ 77 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 78 | // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ 79 | // "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */ 80 | // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 81 | "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ 82 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 83 | "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ 84 | 85 | /* Type Checking */ 86 | "strict": true, /* Enable all strict type-checking options. */ 87 | // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ 88 | // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ 89 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 90 | // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ 91 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 92 | // "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */ 93 | // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ 94 | // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ 95 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 96 | // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ 97 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ 98 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 99 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 100 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 101 | // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ 102 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 103 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ 104 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 105 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 106 | 107 | /* Completeness */ 108 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 109 | "skipLibCheck": true /* Skip type checking all .d.ts files. */ 110 | } 111 | } 112 | --------------------------------------------------------------------------------