0
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));

So this is in bot.on('message', async message) =>. I’ve tried using other Node.js libraries, like words-to-numbers and numbers2words.

My idea is that, for example, somebody says “three” and the bot responds with “four”, by converting the message’s number ("three" to 3), adding 1 (4), and then converting it back to a literal number ("four").

I’d be grateful to anybody that will answer this, I’m clueless! =)

Community
  • 1
  • 1

1 Answers1

0

Easier/Simpler method

You can use these two libraries to convert numbers into words vice-versa:

words-to-numbers: https://www.npmjs.com/package/words-to-numbers

numbers-to-words: https://www.npmjs.com/package/number-to-words

The links contain more information on how to use the libraries.


Harder/Longer method

You can make your own function that takes in a number or a worded number and then returns both the number and the worded number. For that, you can (as far as I know) create a couple arrays e.g. ["one", "two", "three"], ["hundred", thousand", "million"] and use RegEx.

I found a similar-asked-post on StackOverflow where you can view the code on how to code your own: Convert digits into words with JavaScript.


By the way, you will get a syntax error because you forgot a parentheses.

You wrote: bot.on("message", async message) => {});

Should be: bot.on("message", async (message) => {}); or bot.on("message, async message => {});


Eray Chumak
  • 156
  • 1
  • 7
  • I was just specifying that I’ve put it in the Message event. Thank you, though. The other library has worked. –  Jul 23 '18 at 11:28