Files
PluralFlux-infra/src/helpers/messageHelper.js
pieartsy 39a7115803 Converting ES6 back to CJS (#25)
converting back to cjs

Co-authored-by: Aster Fialla <asterfialla@gmail.com>
2026-02-28 14:39:32 -05:00

84 lines
3.3 KiB
JavaScript

const memberHelper = require('./memberHelper.js');
const messageHelper = {};
messageHelper.prefix = "pf;"
/**
* Parses and slices up message arguments, retaining quoted strings.
*
* @param {string} content - The full message content.
* @param {string} commandName - The command name.
* @returns {string[]} An array of arguments.
*/
messageHelper.parseCommandArgs = function(content, commandName) {
const message = content.slice(messageHelper.prefix.length + commandName.length).trim();
return message.match(/\\?.|^$/g).reduce((accumulator, chara) => {
if (chara === '\"' || chara === '\'') {
// checks whether string is within quotes or not
accumulator.quote ^= 1;
} else if (!accumulator.quote && chara === ' '){
// if not currently in quoted string, push empty string to start word
accumulator.array.push('');
} else {
// accumulates characters to the last string in the array and removes escape characters
accumulator.array[accumulator.array.length-1] += chara.replace(/\\(.)/,"$1");
}
return accumulator;
}, {array: ['']}).array // initial array with empty string for the reducer
}
/**
* Parses messages to see if any part of the text matches the tags of any member belonging to an author.
*
* @async
* @param {string} authorId - The author of the message.
* @param {string} content - The full message content
* @param {string | null} [attachmentUrl] - The url for an attachment to the message, if any exists.
* @returns {Promise<{model, string, bool}>} The proxy message object.
*/
messageHelper.parseProxyTags = async function (authorId, content, attachmentUrl = null){
const members = await memberHelper.getMembersByAuthor(authorId);
// If an author has no members, no sense in searching for proxy
if (members.length === 0) {
return;
}
const proxyMessage = {}
members.forEach(member => {
if (member.proxy) {
const splitProxy = member.proxy.split("text");
if(content.startsWith(splitProxy[0]) && content.endsWith(splitProxy[1])) {
proxyMessage.member = member;
proxyMessage.hasAttachment = !!attachmentUrl;
let escapedPrefix = splitProxy[0].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let escapedSuffix = splitProxy[1].replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
escapedPrefix = new RegExp("^" + escapedPrefix);
escapedSuffix = new RegExp(escapedSuffix + "$")
proxyMessage.message = content.replace(escapedPrefix, "").replace(escapedSuffix, "");
}
}
})
return proxyMessage;
}
/**
* Returns a text message that's too long as its text plus a file with the remaining text.
*
* @param {string} text - The text of the message.
* @returns {{text: string, file: Buffer<ArrayBuffer> | undefined}} The text and buffer object
*
*/
messageHelper.returnBufferFromText = function (text) {
if (text.length > 2000) {
const truncated = text.substring(0, 2000);
const restOfText = text.substring(2000);
const file = Buffer.from(restOfText, 'utf-8');
return {text: truncated, file: file}
}
return {text: text, file: undefined}
}
module.exports = messageHelper;