Skip to content
Advertisement

How to have a Discord bot return the number of words in a message

JavaScript

This is the part that is relevant to the question.

If you message the bot “Here are my arguments what do you think about that?”, I want it to return:

JavaScript

I need to figure out the command that counts the number of words in a sentence and change this: message.channel.send(`Arguments: ${}`);

I’m not familiar with Discord.js functions and which one can count a character string. I will look some more but I haven’t found anything yet. The reason I ask is this person put it as an example in his code but he never shows the code of how to return the answer and I’m just curious.

https://discordjs.guide/creating-your-bot/commands-with-user-input.html#basic-arguments

Advertisement

Answer

args is already an array of the words, so you can print the .length property of that. It will return the number of elements (words in this case) in that array.

JavaScript

You can find some comments in the following code to explain it better:

JavaScript

And it seems to be working as expected:

enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement