javascript - 如何从消息中删除字符
问题描述
我想接受一个接受用户输入的命令,假设".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();
}
}
解决方案
有几件事不对
- 您在 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
推荐阅读
- python - Pygame:如何旋转表面区域而不使图像闪烁两次?
- syntax - 为什么 Deno 会标记使用 nodejs 不会发生的语法错误?
- node.js - 机器人:在 20 秒内更改在频道中交谈的权限,创建倒计时并在此过程中删除这些消息
- java - eclipse macosc cocoa 64bit 与 mac64 bit.dmg 安装程序之间的区别
- javascript - 在 knex 中,如何将 async/await 函数更改为使用 .then 方法的函数?
- vue.js - 计算函数忽略 if else 语句
- ruby-on-rails - 将带有列的 PDF 转换为 rails 中的文本
- c# - 如何将自定义函数转换为 Entity Framework Core 3.1 的 sql 表达式
- python - 如何使用 python/pytorch 为图像分类器编写“预测”函数
- assembly - 如何使用 gcc 内联汇编将缓冲区移动到数据段中?