将文字数字(三)加 1(四)

Add a literal number (three) by 1 (four)

var T2W = require("numbers2words");
var translator = new T2W("EN_US");
const { wordsToNumbers } = require('words-to-numbers');
let numbr = wordsToNumbers(message.content.toLowerCase());
message.channel.send(translator.toWords(numbr + 1));

所以这是在 bot.on('message', async message) =>。我试过使用其他 Node.js 库,例如 words-to-numbers and numbers2words.

我的想法是,例如,有人说“三”,机器人回复“四”,方法是将消息的编号("three" 转换为 3),添加 1 (4),然后将其转换回文字数字 ("four").

如果有人能回答这个问题,我将不胜感激,我一无所知! =)

Easier/Simpler方法

您可以使用这两个库将数字转换为单词,反之亦然:

单词到数字:https://www.npmjs.com/package/words-to-numbers

数字到单词:https://www.npmjs.com/package/number-to-words

链接包含有关如何使用库的更多信息。


Harder/Longer方法

您可以创建自己的函数来接收数字或带文字的数字,然后 returns 同时接收数字和带文字的数字。为此,您可以 (据我所知) 创建一对数组,例如["one", "two", "three"]["hundred", thousand", "million"] 并使用 RegEx。

我在 Whosebug 上发现了一个类似的问题-post,您可以在其中查看有关如何编写自己的代码的代码:Convert digits into words with JavaScript


顺便说一句,你会得到一个语法错误,因为你忘记了括号。

您写道: bot.on("message", async message) => {});

应该是: bot.on("message", async (message) => {});bot.on("message, async message => {});