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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ const data = {
MESSAGE_NOT_TAGGED: "```Tag a message or enter a number to proceed.```",
NOT_BLOCK_BOT: "```Bot can not block itself```"
},
calc: {
DESCRIPTION: "Calculates the given mathematical expression.",
EXTENDED_DESCRIPTION:
"```This module can be used to calculate a mathematical expression.\n\nExample usage,```\n*.calc 2+2*",
INVALID_FORMAT: "*Invalid format.*\nApproved Syntax:\n```1. 1 + 2``` \n```2. (1*2) + 2``` \n```3. 4! + 2^2```"
},
carbon: {
DESCRIPTION: "Convert text/code to a carbon image.",
EXTENDED_DESCRIPTION: "```This module can be used to convert text/code into carbon images.\n\nExample Usage,```\n *.carbon <text>* \n *.carbon* ```and reply to a text message.\n\nUse the -t flag after``` *.carbon* ```to get the list of themes availble.\nIn order to specify the theme, use``` *.carbon <text> -t <theme>* .",
Expand Down
30 changes: 30 additions & 0 deletions modules/calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { MessageType } = require("@adiwajshing/baileys");
const Strings = require("../lib/db");
const format = require("python-format-js");
const inputSanitization = require("../sidekick/input-sanitization");
const calc = Strings.calc;
const math = require("mathjs");

module.exports = {
name: "calc",
description: calc.DESCRIPTION,
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

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.

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)) {

await client
.sendMessage(BotsApp.chatId, calc.INVALID_FORMAT, MessageType.text);
return;
}
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

MessageType.text
).catch(err => inputSanitization.handleError(err, client, BotsApp));
} catch (err) {
await inputSanitization.handleError(err, client, BotsApp);
}
},
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"google-tts-api": "^2.0.2",
"got": "^11.8.2",
"jsdom": "^17.0.0",
"mathjs": "^10.1.0",
"ocr-space-api-wrapper": "^1.0.6",
"pg": "^8.7.1",
"python-format-js": "^1.3.9",
Expand Down