Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added calc module with regards to #88 #92

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

IsomerX
Copy link

@IsomerX IsomerX commented Jan 18, 2022

User just needs to type .calc (followed by any math equation)
for example:
.calc 2+2

@Prince-Mendiratta Prince-Mendiratta changed the base branch from main to develop January 18, 2022 13:30
modules/calc.js Outdated
try {
client.sendMessage(
BotsApp.chatId,
`output: ${output}`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the argument itself instead of "output". e.g. 5+2 = 7

modules/calc.js Outdated
output = math.evaluate(args[0]);
}
catch(err){
await inputSanitization.invalidFormat(err, client, BotsApp);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle invalid format error using db.js. No need of separate function.

@@ -79,6 +79,14 @@ exports.isMember = async (chatId, groupMembers) => {
}
}

exports.invalidFormat = async(err, client, BotsApp) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required.

modules/calc.js Outdated
async handle(client, chat, BotsApp, args) {
let output;
try{
output = math.evaluate(args[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Handle the case where the user sends multiple expressions in a single command.
  2. Provide a list of operations allowed.

modules/calc.js Outdated
name: "calc",
description: calc.DESCRIPTION,
extendedDescription: calc.EXTENDED_DESCRIPTION,
demo: { isEnabled: true, text: ".calc" },
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a sample expression for demo

demo: { isEnabled: true, text: ".calc 2^4+5!" },
async handle(client, chat, BotsApp, args) {
let output = math.evaluate(args[0]);
if(!isNaN(output)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having ! negates the condition, which equates to if not is not a number, then say invalid syntax. This means that if the output is a number, then say invalid syntax, which is not what we want.

Suggested change
if(!isNaN(output)) {
if(isNaN(output)) {

extendedDescription: calc.EXTENDED_DESCRIPTION,
demo: { isEnabled: true, text: ".calc 2^4+5!" },
async handle(client, chat, BotsApp, args) {
let output = math.evaluate(args[0]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can wrap a try catch block around this function call. Thus, whenever any error happens, the error is handled there and the value of output is set to undefined. You can then handle the error in the catch block, sending a message to WA with the Error Message. Have a look -

image


image

try {
client.sendMessage(
BotsApp.chatId,
`${args[0]} = ${output}`,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can bold out the output using *, looks much better

extendedDescription: calc.EXTENDED_DESCRIPTION,
demo: { isEnabled: true, text: ".calc 2^4+5!" },
async handle(client, chat, BotsApp, args) {
let output = math.evaluate(args[0]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of restricting the user to add their expressions without any spaces (more values in the args array), it's better if we concatenate the full array in a single string before using the evaluate call.

@Prince-Mendiratta
Copy link
Owner

Also, @IsomerX resolve the conversation threads with prashant for the issues you have resolved already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants