Skip to content

Commit

Permalink
Fix macro resolution (#681)
Browse files Browse the repository at this point in the history
fix: macros validation (replaces every #word to none)
  • Loading branch information
Cyberghxst authored Dec 25, 2024
1 parent 371ef00 commit 13fa104
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/classes/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Command {

this.__client__ = client;

if (hasMacros(client.macros.list(), data.code)) {
if (client.macros.list().length > 0 && hasMacros(client.macros.list(), data.code)) {
data.code = resolveMacros(client.macros.toArray(), data.code);
}

Expand Down
11 changes: 7 additions & 4 deletions src/classes/Macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MacrosManager {
* @returns {RegExp}
*/
function createMacrosPattern(names) {
return new RegExp(`#(${names.join("|")})`, "g");
return new RegExp(`(${names.map(name => `#${name}`).join("|")})`, "g");
};

/**
Expand All @@ -85,17 +85,20 @@ function hasMacros(names, code) {
* @returns {string}
*/
function resolveMacros(macros, code) {
if (macros.length === 0) return code;

const matchedMacros = [...new Set(code.match(createMacrosPattern(macros.map(m => m.name))) ?? [])];
let newCode = code;

for (const matchedMacro of matchedMacros) {
const gotMacro = macros.find(macro => macro.name === matchedMacro.slice(1))
if (!gotMacro) continue;

newCode = newCode.replace(
createMacrosPattern(
[matchedMacro.slice(1)]
),
macros.find(
macro => macro.name === matchedMacro.slice(1)
)?.code || ""
gotMacro.code
);
}

Expand Down

0 comments on commit 13fa104

Please sign in to comment.