Create antiban.js
Browse files- plugins/antiban.js +36 -0
plugins/antiban.js
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import express from 'express';
|
2 |
+
import { Database } from '../database/database.js';
|
3 |
+
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
|
4 |
+
const AntibanRoutes = express.Router();
|
5 |
+
|
6 |
+
AntibanRoutes.get("/api/v1/ai/ban-user", authenticateApiKey, apiLimiter, async (req, res) => {
|
7 |
+
const dbClient = new Database("AkenoXJs");
|
8 |
+
const collection = dbClient.collection("ban_users");
|
9 |
+
try {
|
10 |
+
const userIdString = req.query.user_id;
|
11 |
+
const ReasonString = req.query.reason;
|
12 |
+
const userIdNumber = Number(userIdString);
|
13 |
+
|
14 |
+
if (isNaN(userIdNumber)) {
|
15 |
+
return res.status(400).json({ error: "Invalid or missing user_id" });
|
16 |
+
}
|
17 |
+
|
18 |
+
const existingUser = await collection.findOne({ user_ban: userIdNumber });
|
19 |
+
if (existingUser) {
|
20 |
+
return res.status(200).json({ message: `UserBan is already: ${existingUser.user_ban}`, is_ban: true});
|
21 |
+
}
|
22 |
+
const userDocument = {
|
23 |
+
user_ban: userIdNumber,
|
24 |
+
createdAt: new Date()
|
25 |
+
};
|
26 |
+
if (ReasonString) {
|
27 |
+
userDocument.reason = ReasonString;
|
28 |
+
}
|
29 |
+
await collection.insertOne(userDocument);
|
30 |
+
res.json({ message: `UserBan Successfully banned: ${userIdNumber}`, is_ban: true });
|
31 |
+
} catch (error) {
|
32 |
+
res.status(401).json({ error: error.message });
|
33 |
+
}
|
34 |
+
});
|
35 |
+
|
36 |
+
export { AntibanRoutes };
|