Advertisement
sparkychild

Modlog Polyfill

Jan 9th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // add this as a "freestanding" function somewhere in chat.js
  2. function escapeRegex(str) {
  3.     return (str && typeof str === 'string' ? str : '').replace(/[\\.+*?!=()|[\]{}^$#<>]/g, '\\$&');
  4. }
  5.  
  6. // polyfill the following things into CommandContext in chat.js to avoid refactoring
  7. // to avoid build errors delete the deprecated functions within the class
  8.     /**
  9.      * @param {string} message
  10.      * @param {string} log
  11.      */
  12.     addModCommand(message, log) {
  13.         if (!log) {
  14.             // only a single message provided - format it with userids by grammatically split-parsing the message
  15.             const {roomMsg, logMsg} = this.splitModCommand(message);
  16.             this.room.addByUser(this.user, roomMsg);
  17.             this.room.modlog(`(${this.room.id}) ${logMsg}`);
  18.         } else {
  19.             this.room.addByUser(this.user, message);
  20.             this.room.modlog(`(${this.room.id}) ${log}`);
  21.         }
  22.     }
  23.     /**
  24.      * @param {string} data
  25.      */
  26.     logModCommand(data) {
  27.         this.room.modlog(`(${this.room.id}) ${logMsg}`);
  28.     }
  29.     /**
  30.      * @param {string} roomMsg
  31.      */
  32.     splitModCommand(roomMsg) {
  33.         const subjectRegex = new RegExp(`^(\\(?)(${this.targetUsername ? escapeRegex(this.targetUsername) + '|' : ''}${this.targetUser ? escapeRegex(this.targetUser.getLastName()) + '|' : ''}${escapeRegex(this.user.name)})\\b`, 'gi');
  34.         const objectRegex = new RegExp(`(by|to|for) (${this.targetUsername ? escapeRegex(this.targetUsername) + '|' : ''}${escapeRegex(this.user.name)})\\b`, 'gi');
  35.         const possessiveRegex = new RegExp(`(${this.targetUsername ? escapeRegex(this.targetUsername) + '|' : ''}${escapeRegex(this.user.name)})'s`, 'gi');
  36.  
  37.         let logMsg = roomMsg.replace(subjectRegex, m => {
  38.             let prefix = m.startsWith('(') ? '(' : '';
  39.             return prefix + `[${toId(m)}]`;
  40.         }).replace(objectRegex, m => {
  41.             const [preposition, ...username] = m.split(' ');
  42.             return `${preposition} [${toId(username.join(''))}]`;
  43.         }).replace(possessiveRegex, match => `[${match.slice(0, -2)}]'s`);
  44.  
  45.         return {roomMsg, logMsg};
  46.     }
  47.     /**
  48.      * @param {string} message
  49.      * @param {string} log
  50.      */
  51.     privateModCommand(message, log) {
  52.         if (!log) {
  53.             // only a single message provided - format it with userids by grammatically split-parsing the message
  54.             const {roomMsg, logMsg} = this.splitModCommand(message);
  55.             this.room.sendModCommand(roomMsg);
  56.             this.roomlog(roomMsg);
  57.             this.room.modlog(`(${this.room.id}) ${logMsg}`);
  58.         } else {
  59.             this.room.sendModCommand(message);
  60.             this.roomlog(message);
  61.             this.room.modlog(`(${this.room.id}) ${log}`);
  62.         }
  63.     }
  64.     /**
  65.      * @param {string} data
  66.      */
  67.     logEntry(data) {
  68.         if (this.pmTarget) return;
  69.         this.room.roomlog(data);
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement