Home > Mobile >  Trying to use replace() method on files by passing the string not working - JS
Trying to use replace() method on files by passing the string not working - JS

Time:10-09

I'm trying to replace a string, but when i try to do it on a file, it does not work. Or only the last file will work.

Main Code (inside a class method):

const funcs = fs.readdirSync(path.join(__dirname, "../funcs")).filter(file => file.endsWith('.js'));
        
this.functions = this.code.split("$");

const functions = this.functions

for (const func of funcs) {
    for (let x = functions.length - 1; x > 0; x--) {
         let i = 0;
         const res = await require(`../funcs/${func}`)(client, this.code.toString(), this._author);

         console.log(x);
         console.log(res);
         console.log(functions);

         return res;
         i  ;
    }
}

ping.js:

const ping = (client, code, author) => {
    const result = code.split("$[ping]").join(client.ws.ping)

   return result;
}

module.exports = ping;

messageAuthorTag.js:

const Util = require('../utils/util');

const messageAuthorTag = (client, code, author) => {
    if (code === null) return;
    
    const res = code.split("$[message.author.tag]").join(`${author.tag}`);

    return res;
}

module.exports = messageAuthorTag;

Using it : "ping: $[ping] author tag: $[message.author.tag]"

Output: ping: $[ping] author tag: Example#0001

CodePudding user response:

Your issue is that you return res in the middle of a for loop. This means it won't do anything else in the function and will skip the rest of the loops (like running the other functions!)

As an additional note, you could also remove the let i = 0 and i since that doesn't seem to be used for anything.

  • Related