ryu-js / plugins /antiban.js
randydev's picture
Update plugins/antiban.js
39000f4 verified
raw
history blame
2.7 kB
import express from 'express';
import { Database } from '../database/database.js';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
const AntibanRoutes = express.Router();
AntibanRoutes.post("/api/v1/user/ban-user", authenticateApiKey, apiLimiter, async (req, res) => {
const dbClient = new Database("AkenoXJs");
const collection = dbClient.collection("ban_users");
const collectionKey = dbClient.collection("api_keys");
try {
const apiKey = req.headers["x-api-key"];
const userIdString = req.query.user_id;
const reasonString = req.query.reason;
const userIdNumber = Number(userIdString);
if (!apiKey) {
return res.status(400).json({ error: "Missing API key in headers" });
}
if (isNaN(userIdNumber)) {
return res.status(400).json({ error: "Invalid or missing user_id" });
}
const existingUserKey = await collectionKey.findOne({ key: apiKey });
if (!existingUserKey) {
return res.status(401).json({ message: "API key not found" });
}
const existingBan = await collection.findOne({ user_ban: userIdNumber });
if (existingBan) {
return res.status(200).json({ message: `User ${userIdNumber} is already banned`, is_ban: true });
}
await collection.updateOne(
{ key: existingUserKey.key },
{ $addToSet: { user_ban: userIdNumber }, $set: { updatedAt: new Date(), owner: existingUserKey.owner } },
{ upsert: true }
);
res.json({ message: `User ${userIdNumber} successfully banned`, is_ban: true });
} catch (error) {
res.status(500).json({ error: `Internal server error: ${error.message}` });
}
});
AntibanRoutes.get("/api/v1/user/check-ban", authenticateApiKey, async (req, res) => {
const dbClient = new Database("AkenoXJs");
const collection = dbClient.collection("ban_users");
try {
const userIdString = req.query.user_id;
const userIdNumber = Number(userIdString);
if (isNaN(userIdNumber)) {
return res.status(400).json({ error: "Invalid or missing user_id" });
}
const bannedUser = await collection.findOne({ user_ban: userIdNumber });
if (bannedUser) {
return res.status(200).json({ message: `User ${userIdNumber} is banned`, is_ban: true });
} else {
return res.status(200).json({ message: `User ${userIdNumber} is not banned`, is_ban: false });
}
} catch (error) {
res.status(500).json({ error: `Internal server error: ${error.message}` });
}
});
export { AntibanRoutes };