From 389f95ff722ade2b10acc6e1c66a8d8c10c289b1 Mon Sep 17 00:00:00 2001 From: IsomerX <49413460+IsomerX@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:25:45 +0530 Subject: [PATCH 1/3] Added calc module to do math calculations #88 --- lib/db.js | 6 ++++++ modules/calc.js | 32 ++++++++++++++++++++++++++++++++ package.json | 1 + sidekick/input-sanitization.js | 8 ++++++++ 4 files changed, 47 insertions(+) create mode 100644 modules/calc.js diff --git a/lib/db.js b/lib/db.js index 32dfe3e..4b79afb 100644 --- a/lib/db.js +++ b/lib/db.js @@ -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: "Calculate the given expression.", + EXTENDED_DESCRIPTION: + "```This module can be used to calculate the expression presented.\n\nExample usage,```\n*.calc 2+2*", + ALIVE_MSG: "```💠 BotsApp has been integrated successfully. 💠\n\nUse the ``` *.help* ``` command to get a list of plugins that will make your WhatsApp experience much easier.\n\nIf you are impressed with my service, consider supporting the Bot on GitHub-\n```https://github.com/BotsAppOfficial/BotsApp\n\n```Check out our official website for any new updates.```" + }, 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 * \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 -t * .", diff --git a/modules/calc.js b/modules/calc.js new file mode 100644 index 0000000..1768623 --- /dev/null +++ b/modules/calc.js @@ -0,0 +1,32 @@ +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" }, + async handle(client, chat, BotsApp, args) { + let output; + try{ + output = math.evaluate(args[0]); + } + catch(err){ + await inputSanitization.invalidFormat(err, client, BotsApp); + return; + } + try { + client.sendMessage( + BotsApp.chatId, + `output: ${output}`, + MessageType.text + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + } catch (err) { + await inputSanitization.handleError(err, client, BotsApp); + } + }, +}; diff --git a/package.json b/package.json index feb4e10..9e43840 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/sidekick/input-sanitization.js b/sidekick/input-sanitization.js index 623f670..c00f685 100644 --- a/sidekick/input-sanitization.js +++ b/sidekick/input-sanitization.js @@ -79,6 +79,14 @@ exports.isMember = async (chatId, groupMembers) => { } } +exports.invalidFormat = async(err, client, BotsApp) => { + client.sendMessage( + BotsApp.chatId, + "*Invalid format.*\nApproved Syntax:\n```1. 1 + 2``` \n```2. (1*2) + 2``` \n```3. 4! + 2^2```", + MessageType.text + ); +} + exports.handleError = async(err, client, BotsApp, customMessage = "```Something went wrong. The error has been logged in log chats```") => { console.log(chalk.redBright.bold("[ERROR] " + err)); data = { From 2de4e42da691096bb0c80e904c35c0dc3fd8a01a Mon Sep 17 00:00:00 2001 From: IsomerX <49413460+IsomerX@users.noreply.github.com> Date: Tue, 18 Jan 2022 16:27:30 +0530 Subject: [PATCH 2/3] changed the description for calc --- lib/db.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/db.js b/lib/db.js index 4b79afb..962ed95 100644 --- a/lib/db.js +++ b/lib/db.js @@ -64,7 +64,6 @@ const data = { DESCRIPTION: "Calculate the given expression.", EXTENDED_DESCRIPTION: "```This module can be used to calculate the expression presented.\n\nExample usage,```\n*.calc 2+2*", - ALIVE_MSG: "```💠 BotsApp has been integrated successfully. 💠\n\nUse the ``` *.help* ``` command to get a list of plugins that will make your WhatsApp experience much easier.\n\nIf you are impressed with my service, consider supporting the Bot on GitHub-\n```https://github.com/BotsAppOfficial/BotsApp\n\n```Check out our official website for any new updates.```" }, carbon: { DESCRIPTION: "Convert text/code to a carbon image.", From f7752bd903f465c988d6867e04e832477ec5b96e Mon Sep 17 00:00:00 2001 From: IsomerX <49413460+IsomerX@users.noreply.github.com> Date: Sun, 30 Jan 2022 01:31:09 +0530 Subject: [PATCH 3/3] changes 20.1.22 --- lib/db.js | 5 +++-- modules/calc.js | 16 +++++++--------- sidekick/input-sanitization.js | 8 -------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/db.js b/lib/db.js index 962ed95..59da0d6 100644 --- a/lib/db.js +++ b/lib/db.js @@ -61,9 +61,10 @@ const data = { NOT_BLOCK_BOT: "```Bot can not block itself```" }, calc: { - DESCRIPTION: "Calculate the given expression.", + DESCRIPTION: "Calculates the given mathematical expression.", EXTENDED_DESCRIPTION: - "```This module can be used to calculate the expression presented.\n\nExample usage,```\n*.calc 2+2*", + "```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.", diff --git a/modules/calc.js b/modules/calc.js index 1768623..d3675cd 100644 --- a/modules/calc.js +++ b/modules/calc.js @@ -9,20 +9,18 @@ module.exports = { name: "calc", description: calc.DESCRIPTION, extendedDescription: calc.EXTENDED_DESCRIPTION, - demo: { isEnabled: true, text: ".calc" }, + demo: { isEnabled: true, text: ".calc 2^4+5!" }, async handle(client, chat, BotsApp, args) { - let output; - try{ - output = math.evaluate(args[0]); - } - catch(err){ - await inputSanitization.invalidFormat(err, client, BotsApp); - return; + let output = math.evaluate(args[0]); + if(!isNaN(output)) { + await client + .sendMessage(BotsApp.chatId, calc.INVALID_FORMAT, MessageType.text); + return; } try { client.sendMessage( BotsApp.chatId, - `output: ${output}`, + `${args[0]} = ${output}`, MessageType.text ).catch(err => inputSanitization.handleError(err, client, BotsApp)); } catch (err) { diff --git a/sidekick/input-sanitization.js b/sidekick/input-sanitization.js index c00f685..623f670 100644 --- a/sidekick/input-sanitization.js +++ b/sidekick/input-sanitization.js @@ -79,14 +79,6 @@ exports.isMember = async (chatId, groupMembers) => { } } -exports.invalidFormat = async(err, client, BotsApp) => { - client.sendMessage( - BotsApp.chatId, - "*Invalid format.*\nApproved Syntax:\n```1. 1 + 2``` \n```2. (1*2) + 2``` \n```3. 4! + 2^2```", - MessageType.text - ); -} - exports.handleError = async(err, client, BotsApp, customMessage = "```Something went wrong. The error has been logged in log chats```") => { console.log(chalk.redBright.bold("[ERROR] " + err)); data = {