randydev commited on
Commit
39000f4
·
verified ·
1 Parent(s): 41e528b

Update plugins/antiban.js

Browse files
Files changed (1) hide show
  1. plugins/antiban.js +32 -14
plugins/antiban.js CHANGED
@@ -21,30 +21,48 @@ AntibanRoutes.post("/api/v1/user/ban-user", authenticateApiKey, apiLimiter, asyn
21
  return res.status(400).json({ error: "Invalid or missing user_id" });
22
  }
23
 
24
- const existingUser = await collection.findOne({ user_ban: userIdNumber });
25
  const existingUserKey = await collectionKey.findOne({ key: apiKey });
26
-
27
  if (!existingUserKey) {
28
  return res.status(401).json({ message: "API key not found" });
29
  }
30
 
31
- if (existingUser) {
32
- return res.status(200).json({ message: `User is already banned: ${existingUser.user_ban}`, is_ban: true });
 
33
  }
34
 
35
- const userDocument = {
36
- owner: existingUserKey.owner,
37
- key: existingUserKey.key,
38
- user_ban: userIdNumber,
39
- createdAt: new Date(),
40
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- if (reasonString) {
43
- userDocument.reason = reasonString;
44
  }
45
 
46
- await collection.insertOne(userDocument);
47
- res.json({ message: `User successfully banned: ${userIdNumber}`, is_ban: true });
 
 
 
 
 
48
 
49
  } catch (error) {
50
  res.status(500).json({ error: `Internal server error: ${error.message}` });
 
21
  return res.status(400).json({ error: "Invalid or missing user_id" });
22
  }
23
 
 
24
  const existingUserKey = await collectionKey.findOne({ key: apiKey });
 
25
  if (!existingUserKey) {
26
  return res.status(401).json({ message: "API key not found" });
27
  }
28
 
29
+ const existingBan = await collection.findOne({ user_ban: userIdNumber });
30
+ if (existingBan) {
31
+ return res.status(200).json({ message: `User ${userIdNumber} is already banned`, is_ban: true });
32
  }
33
 
34
+ await collection.updateOne(
35
+ { key: existingUserKey.key },
36
+ { $addToSet: { user_ban: userIdNumber }, $set: { updatedAt: new Date(), owner: existingUserKey.owner } },
37
+ { upsert: true }
38
+ );
39
+
40
+ res.json({ message: `User ${userIdNumber} successfully banned`, is_ban: true });
41
+
42
+ } catch (error) {
43
+ res.status(500).json({ error: `Internal server error: ${error.message}` });
44
+ }
45
+ });
46
+
47
+ AntibanRoutes.get("/api/v1/user/check-ban", authenticateApiKey, async (req, res) => {
48
+ const dbClient = new Database("AkenoXJs");
49
+ const collection = dbClient.collection("ban_users");
50
+
51
+ try {
52
+ const userIdString = req.query.user_id;
53
+ const userIdNumber = Number(userIdString);
54
 
55
+ if (isNaN(userIdNumber)) {
56
+ return res.status(400).json({ error: "Invalid or missing user_id" });
57
  }
58
 
59
+ const bannedUser = await collection.findOne({ user_ban: userIdNumber });
60
+
61
+ if (bannedUser) {
62
+ return res.status(200).json({ message: `User ${userIdNumber} is banned`, is_ban: true });
63
+ } else {
64
+ return res.status(200).json({ message: `User ${userIdNumber} is not banned`, is_ban: false });
65
+ }
66
 
67
  } catch (error) {
68
  res.status(500).json({ error: `Internal server error: ${error.message}` });