NikoD
NikoD

Reputation: 15

How can I remove characters from message

I would like to take a command that takes in an input from the user let's say ".result 123" with .result being the bot command and only writing out the string after it. I've tried regex but haven't been successful. This is the code I tried:

Note: Some variables are for me to check the time for when the command is still valid.

let userinput = new RegExp('.+');
if(message.content === ".result" + userinput) {
    
    function removeCharacters() {
      message.content = str;
      str = str.substr(8);
      console.log(str); 
    }

    if ((currseconds - seconds) <= 1200) {
      message.channel.send('**FINAL RECORDED FPM:**');
      removeCharacters();
    }
}

Upvotes: 0

Views: 145

Answers (2)

mplungjan
mplungjan

Reputation: 177860

There are several things wrong

  • You are declaring a function inside the if, that is a bit weird
  • You need to escape a dot in regex,

To find 123 in .result 123 you could do

const message = { "content": ".result 123" } // test message for this script
const userInput = message.content.match(/\.result (\w+)/)
if (userInput) console.log(userInput[1]); // contains the command

Upvotes: 1

Suhail Malik
Suhail Malik

Reputation: 1

You can try with .split('.result ') in javascript then you will get an array of leftover mdn documentation. For your case -

const input = '.result 1243523'

const splitted = input.split('.result ') // In split the text you want to remove.

// got an array of left over selecting text after your `.result ` removed
const result = splitted[1]

console.log(result) // 1243523

Upvotes: 0

Related Questions