首页 > 解决方案 > 如何从消息中删除字符

问题描述

我想接受一个接受用户输入的命令,假设".result 123".result 是机器人命令,并且只写出它之后的字符串。我试过正则表达式,但没有成功。这是我试过的代码:

注意:一些变量是让我检查命令仍然有效的时间。

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();
    }
}

标签: javascriptdiscord.js

解决方案


有几件事不对

  • 您在 if 中声明了一个函数,这有点奇怪
  • 你需要在正则表达式中转义一个点,

要找到 123 在.result 123你可以做

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


推荐阅读