Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // add this as a "freestanding" function somewhere in chat.js
- function escapeRegex(str) {
- return (str && typeof str === 'string' ? str : '').replace(/[\\.+*?!=()|[\]{}^$#<>]/g, '\\$&');
- }
- // polyfill the following things into CommandContext in chat.js to avoid refactoring
- // to avoid build errors delete the deprecated functions within the class
- /**
- * @param {string} message
- * @param {string} log
- */
- addModCommand(message, log) {
- if (!log) {
- // only a single message provided - format it with userids by grammatically split-parsing the message
- const {roomMsg, logMsg} = this.splitModCommand(message);
- this.room.addByUser(this.user, roomMsg);
- this.room.modlog(`(${this.room.id}) ${logMsg}`);
- } else {
- this.room.addByUser(this.user, message);
- this.room.modlog(`(${this.room.id}) ${log}`);
- }
- }
- /**
- * @param {string} data
- */
- logModCommand(data) {
- this.room.modlog(`(${this.room.id}) ${logMsg}`);
- }
- /**
- * @param {string} roomMsg
- */
- splitModCommand(roomMsg) {
- const subjectRegex = new RegExp(`^(\\(?)(${this.targetUsername ? escapeRegex(this.targetUsername) + '|' : ''}${this.targetUser ? escapeRegex(this.targetUser.getLastName()) + '|' : ''}${escapeRegex(this.user.name)})\\b`, 'gi');
- const objectRegex = new RegExp(`(by|to|for) (${this.targetUsername ? escapeRegex(this.targetUsername) + '|' : ''}${escapeRegex(this.user.name)})\\b`, 'gi');
- const possessiveRegex = new RegExp(`(${this.targetUsername ? escapeRegex(this.targetUsername) + '|' : ''}${escapeRegex(this.user.name)})'s`, 'gi');
- let logMsg = roomMsg.replace(subjectRegex, m => {
- let prefix = m.startsWith('(') ? '(' : '';
- return prefix + `[${toId(m)}]`;
- }).replace(objectRegex, m => {
- const [preposition, ...username] = m.split(' ');
- return `${preposition} [${toId(username.join(''))}]`;
- }).replace(possessiveRegex, match => `[${match.slice(0, -2)}]'s`);
- return {roomMsg, logMsg};
- }
- /**
- * @param {string} message
- * @param {string} log
- */
- privateModCommand(message, log) {
- if (!log) {
- // only a single message provided - format it with userids by grammatically split-parsing the message
- const {roomMsg, logMsg} = this.splitModCommand(message);
- this.room.sendModCommand(roomMsg);
- this.roomlog(roomMsg);
- this.room.modlog(`(${this.room.id}) ${logMsg}`);
- } else {
- this.room.sendModCommand(message);
- this.roomlog(message);
- this.room.modlog(`(${this.room.id}) ${log}`);
- }
- }
- /**
- * @param {string} data
- */
- logEntry(data) {
- if (this.pmTarget) return;
- this.room.roomlog(data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement