ryu-js / plugins /antiban.js
randydev's picture
Update plugins/antiban.js
a9746de verified
raw
history blame
6.93 kB
import express from 'express';
import { Database } from '../database/database.js';
import { predictCreationDate } from '../lib/create-date.js';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
const AntibanRoutes = express.Router();
const protectedUsers = [6477856957, 1191668125, 1448273246, 1054295664, 6444305696];
AntibanRoutes.get("/api/v1/user/creation-date", authenticateApiKey, apiLimiter, (req, res) => {
const userId = Number(req.query.user_id);
if (!userId || isNaN(userId)) {
return res.status(400).json({ error: "Invalid or missing user_id" });
}
const result = predictCreationDate(userId);
res.json({ user_id: userId, estimated_creation: result });
});
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" });
}
if (existingUserKey.owner === userIdNumber) {
return res.status(200).json({
message: `User ${userIdNumber} cannot be banned because they created the API key`,
is_ban: false
});
}
if (protectedUsers.includes(userIdNumber)) {
return res.status(403).json({
message: `User ${userIdNumber} is a protected admin and cannot be banned`,
is_ban: false
});
}
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.post("/api/v1/user/anti-broadcast", authenticateApiKey, async (req, res) => {
const dbClient = new Database("AkenoXJs");
const collection = dbClient.collection("users_broadcast");
const collectionKey = dbClient.collection("api_keys");
try {
const apiKey = req.headers["x-api-key"];
const userIdString = req.query.user_id;
const WorldString = req.query.text;
const userIdNumber = Number(userIdString);
if (!apiKey) {
return res.status(400).json({ error: "Missing API key in headers" });
}
if (!WorldString) {
return res.status(400).json({ error: "Missing params: text" });
}
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" });
}
if (existingUserKey.owner === userIdNumber) {
return res.status(200).json({
message: `User ${userIdNumber} cannot be broadcast because they created the API key`,
is_broadcast: false
});
}
const existingBroadcast = await collection.findOne({ user_id: userIdNumber });
if (existingBroadcast) {
return res.status(200).json({ message: `User ${userIdNumber} is already broadcasting`, is_broadcast: true });
}
if (WorldString.length > 3096) {
return res.status(400).json({ message: `User ${userIdNumber} is sending a spam broadcast`, is_broadcast: true });
}
await collection.updateOne(
{ user_id: userIdNumber },
{
$set: {
text: WorldString,
updatedAt: new Date(),
owner: existingUserKey.owner,
is_broadcast: true
}
},
{ upsert: true }
);
res.json({ message: `User ${userIdNumber} successfully broadcast`, is_broadcast: true });
} catch (error) {
res.status(500).json({ error: `Internal server error: ${error.message}` });
}
});
AntibanRoutes.get("/api/v1/user/check-broadcast", authenticateApiKey, async (req, res) => {
const dbClient = new Database("AkenoXJs");
const collection = dbClient.collection("users_broadcast");
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 GikesUser = await collection.findOne({ user_id: userIdNumber });
if (GikesUser) {
return res.status(200).json({ message: `User ${userIdNumber} is broadcast`, is_broadcast: true });
} else {
return res.status(200).json({ message: `User ${userIdNumber} is not broadcast`, is_broadcast: false });
}
} 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 };