diff --git a/src/classes/Commands.js b/src/classes/Commands.js index d2985b131..1e7e81963 100644 --- a/src/classes/Commands.js +++ b/src/classes/Commands.js @@ -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); } diff --git a/src/classes/Macros.js b/src/classes/Macros.js index c5d9746ff..20ec368da 100644 --- a/src/classes/Macros.js +++ b/src/classes/Macros.js @@ -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"); }; /** @@ -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 ); }