Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ jobs:
uses: actions/checkout@v3

- name: Install dependencies
run: npm install
# temporary fix for installing deps for api
run: |
npm install
cd api && npm install

- name: Build code
run: |
cp ~/important/interchat-env .env
npm run build --if-present
cp ~/important/interchat-env .env
npm run build --if-present
cd api && npm run build --if-present

- name: Deploy Commands
run: npm run deploy -- -b
run: npm run register:commands -- -b

- name: Prune Dev-dependencies
run: npm prune --omit=dev
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ tsconfig.tsbuildinfo
# Configuration
.env

# Unit tests
tests/
# Unit test coverage
__tests__/

# Unit test coverage reports
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# InterChat
Code for a growing discord bot which provides a fun inter-server chat!

// subcommand files must have the same name as the subcommand registered on discord
// commands and subcommands must be default exports

## Starting the bot:
1. Install dependencies using `npm install`
2. Make a file called `.env` and fill it out with the appropriate contents mentioned in the env.example file.
Expand Down
3 changes: 3 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules

dist/
42 changes: 42 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import express from 'express';
import { node } from '@tensorflow/tfjs-node';
import { load, predictionType } from 'nsfwjs';

const model = await load();
const app = express();
const port = 3000;

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const analyzeImage = async (url: string): Promise<predictionType[] | null> => {
const imageBuffer = await (await fetch(url)).arrayBuffer();

const imageTensor = await node.decodeImage(Buffer.from(imageBuffer), 3) as any;
const predictions = await model.classify(imageTensor);
imageTensor.dispose();

return predictions;
};

app.get('/nsfw', async (req, res) => {
const url = req.query.url;

if (!url || typeof url !== 'string') return res.status(400).json({ error: 'Missing url query parameter.' });

const regex =
/(?:(?:(?:[A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)(?:(?:\/[+~%/.\w-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[\w]*))?)(?:\.jpg|\.jpeg|\.png)/;
if (!regex.test(url)) {
return res
.status(400)
.send({ error: 'Invalid url parameter. Must be a valid PNG, JPG or JPEG image URL.' });
}

const predictions = await analyzeImage(url);
if (!predictions) return res.status(500).json({ error: 'Something went wrong while analyzing the image.' });
return res.status(200).json(predictions);
});

app.listen(port, () => {
console.log(`API listening on port ${port}`);
});
Loading