WalshyDev

MC Chat Filtering

Mar 16th, 2021 (edited)
351
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express'),
  2.   app = express();
  3.  
  4. const badWordFilter = ["herobrine", "blobs"];
  5.  
  6. app.use(express.json());
  7.  
  8. /*
  9.   {
  10.     "response": // Boolean - If this is true it goes to "passThrough" (Not filtered!)
  11.     "hashed": // String - If not present, it gets fully filtered (removed) - if this is here then `hashes` is required!
  12.     "hashes": // Json Array - The bad words found
  13.   }
  14.  */
  15.  
  16. app.all('/*', (req, res, next) => {
  17.   console.log(`${req.ip} ${req.method} ${req.originalUrl}`);
  18.   console.log(req.headers);
  19.   console.log(req.body);
  20.  
  21.   next();
  22. });
  23.  
  24. app.post('/v1/join', (req, res) => {
  25.   res.status(200).json({
  26.     response: true
  27.   })
  28. });
  29.  
  30. app.post('/v1/leave', (req, res) => {
  31.   res.status(200).json({
  32.     response: true
  33.   })
  34. });
  35.  
  36. app.post('/v1/chat', (req, res) => {
  37.   const body = req.body;
  38.  
  39.   // Notch is exempt from the chat filter!
  40.   if (body.player_display_name === 'Notch') {
  41.     return res.status(200).json({ response: true });
  42.   }
  43.  
  44.   const text = body.text.toLowerCase();
  45.   let filteredText = text;
  46.  
  47.   const badWords = [];
  48.  
  49.   for (badWord of badWordFilter) {
  50.     if (text.includes(badWord)) {
  51.       badWords.push(badWord);
  52.  
  53.       filteredText = filteredText.replace(badWord, '*'.repeat(badWord.length));
  54.     }
  55.   }
  56.  
  57.   // There's a bad word
  58.   if (badWords.length > 0) {
  59.     res.status(200).json({
  60.       response: false,
  61.       hashed: filteredText,
  62.       hashes: badWords
  63.     })
  64.   } else {
  65.     // No filtering needed
  66.     res.status(200).json({
  67.       response: true
  68.     })
  69.   }
  70. });
  71.  
  72. app.listen(8000, () => console.log('Listening on :8000'));
  73.  
RAW Paste Data